# coding=utf8
import json
import os
import random

import itchat
import requests

from wxRobot import globalVars
from pic_extract import picExtract

REPLY_USER_NICKNAME = '冯泽明 付晓蕾 x X 安源 赵航 就是随便改个名'
REPLY_GROUPNAME = '叫啥叫啥！ 聊天机器人测试'

def get_response(msg):
    try:
        # 返回需要解码 [讲笑话的返回值特殊]
        r = requests.post(globalVars.get_apiUrl() + msg).content
        if '笑话' in msg:  # 注意 因为匹配关键词是 '笑话' 所以要用 in 判断是否存在于字符串中
            jsonStr = r.decode('utf-8-sig')
            jsonObj = json.loads(jsonStr)
            text = '《' + jsonObj['title'] + '》\n' + jsonObj['content']
            print(text)
            return text
        else:
            return r.decode('utf-8')
    except (RuntimeError, TypeError, NameError, IOError):
        return  # 将会返回一个None

@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing'])
def bot_reply(msg):
    text = msg['Text'].strip()
    print(msg['User'].NickName + ' 说:' + text)
    defaultReply = ' “' + text + '”,朕已阅! '  # 容错，服务器无响应
    reply = get_response(text)
    # 小胖子
    if msg['User'].NickName in REPLY_USER_NICKNAME:
        if '斗图' in msg['Text'].strip():
            doutu(msg)
        else:
            print('----给[' + msg['User'].NickName + ']的回复:' + (reply or defaultReply))
            return reply or defaultReply

@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
def bot_media_replay(msg):
    # 小胖子
    if msg['User'].NickName in REPLY_USER_NICKNAME:
        try:
            # 这里需要进行一场处理，否则可能因发送的图片为动态图造成程序异常中断
            pic_text = picExtract.get_file_text(msg['Text']())
            print(msg['User'].NickName + '想要斗图!' + pic_text)  # 加一条输出，后台确认发图片消息的人
            itchat.send_msg('你发的图片上写着：“' + pic_text + '”', msg['User'].UserName)
            # 识别图片上的文字，暂未指定处理方案
        except (RuntimeError, TypeError, NameError, IOError):
            print('{!@系统异常：#发送的图片未能处理,图片来自[' + msg['User'].NickName + ']}')
        doutu(msg)

@itchat.msg_register('Text', isGroupChat=True)
def group_reply(msg):
    text = msg['Text'].replace('@晓晓', '').strip()
    print(msg['ActualNickName'] + ' 说:' + text)
    defaultReply = ' “' + text + '”,朕已阅! '  # 容错，服务器无响应
    reply = get_response(text)  # @我的时候消息要特殊处理 && 指定的群进行特殊处理
    if msg['isAt'] or msg['User']['NickName'] in REPLY_GROUPNAME:
        if '斗图' in text:
            doutu(msg)
        else:
            print('----群[' + msg['User']['NickName'] + ']的回复:' + (reply or defaultReply))
            return reply or defaultReply

def maxCount():
    number = 0
    for root, dirs, files in os.walk(globalVars.get_path()):
        # print(root)  # 当前目录路径
        # print(dirs)  # 当前路径下所有子目录
        # print(files) # 当前路径下所有非目录子文件
        for file in files:
            if int(file.split('.')[0]) > number:
                globalVars.set_count(int(file.split('.')[0]))
    return globalVars.get_count()

def doutu(msg):
    fileNumber = str(random.randint(0, globalVars.get_count())).zfill(2)  # zfill是python自带的补前置零函数
    fileName = globalVars.get_path() + '\\' + fileNumber
    if os.path.exists(fileName + '.jpg'):
        fileName += '.jpg'
    else:
        fileName += '.gif'
    print('\t[使用图片路径：' + fileName + ']\n')
    itchat.send_image(fileName, msg['User'].UserName)

if __name__ == '__main__':
    globalVars.set_path(os.path.abspath('.') + '\\doutu\\pictures')
    globalVars.set_count(maxCount())
    # 我们使用热启动,不用每次的都扫码登陆
    itchat.auto_login(hotReload=True)
    itchat.run()


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        map = {}
        output = []
        for i in range(len(nums)):
            map[nums[i]] = i

        for i in range(len(nums)):
            if target-nums[i] in map and map[target - nums[i]] != i:

                first = map.get(nums[i])
                second = map.get(target-nums[i])
                output.append(first)
                output.append(second)

                return output


import streamlit as st

import fpl_opt.selection
import utils

def pick_team(points_data_path, playing_data_path, bucket_name, budget):
    player_df = utils.get_forecast_data(
        points_data_path, playing_data_path, bucket_name
    ).reset_index()
    player_df = player_df.groupby("element").agg(
        {
            "score_pred": "mean",
            "playing_chance": "mean",
            "value": "first",
            "position": "first",
            "team": "first",
            "name": "first",
        }
    )
    decisions, captain_decisions, sub_decisions = fpl_opt.selection.select_team(
        player_df["score_pred"].values,
        player_df["value"].values,
        player_df["position"]
        .replace({"GKP": "GK"})
        .map({"GK": 1, "DEF": 2, "MID": 3, "FWD": 4})
        .values,
        player_df["team"].values,
        playing_chance=player_df["playing_chance"].values,
        sub_factors=[0.15, 0.15, 0.15, 0.05],
        total_budget=budget,
    )
    selection_df = fpl_opt.selection.get_selection_df(
        decisions, captain_decisions, sub_decisions, player_df
    )

    return selection_df

settings = utils.get_settings()

def main():
    budget = float(st.text_input("Budget", "100"))
    selection_df = utils.pick_team(
        settings.points_data_path,
        settings.playing_data_path,
        settings.bucket_name,
        budget,
    )
    st.dataframe(
        selection_df,
        use_container_width=True,
    )

if __name__ == "__main__":
    main()


'''
load tuitiantu dataset filename
'''
from glob import glob
import numpy as np
import os
from os.path import join
import pandas as pd
import random

# read file and labels
labeldict = {"gpc":0, "nongpc":1}

def get_img_lab_test_lvwang(datapath, listname):
    idir = 'gpc'
    datalist = join(datapath, listname)
    if os.path.exists(datalist):
        print('datalist exists')
        return
    else:
        imgpath = join(datapath, idir, "img") # img
        assert os.path.isdir(imgpath) == True
        imglist = glob(join(imgpath, "*.png"))
        lab = labeldict[idir]
        with open(datalist,"w") as f: # append
            for img in imglist:
                mask = img.replace("img", "lab")
                f.write(img+","+ mask+","+str(lab)+"\n")

if __name__=="__main__":
    # 2021.10.21
    datapath = r'.\testvalid'
    get_img_lab_test_lvwang(datapath, "test_list_gpc.txt")


import logging
import math
import numpy as np
import pickle
from abc import ABC, abstractmethod
from collections import defaultdict, Counter
from itertools import chain, islice
from typing import List, Tuple, Union, Iterable as TIterable, Optional, Type

from cacheops import invalidate_model, invalidate_obj
from django.db import transaction
from django.db.models import Sum, Model
from django.utils.decorators import classproperty
from keras.metrics import binary_accuracy
from keras.models import Sequential
from keras.layers import Dense, Dropout

from django_ml_spam_filter.utils import exec_in_parallel
from spam_filter.content_parser import ContentParser
from spam_filter.mail_source import MailContentSource
from spam_filter.models import BayesDictionary, NNStructure

logger = logging.getLogger('default')

class LearningModel(ABC):

    @classmethod
    def train(cls, spam: Optional[MailContentSource] = None, ham: Optional[MailContentSource] = None,
              learning_content: TIterable[Tuple[str, bool]] = None, init: bool = False, batch_size: int = 1000,
              **kwargs):
        """
        Тренирует модель используя источники спам и хам писем MailContentSource или готовое к тренировке содержимое
        learning_content.
        :param spam: MailContentSource со спамом
        :param ham: MailContentSource с хамом
        :param learning_content: Уже готовый к обработке контент. Список с Tuple(сообщение, флаг спама)
        :param init: Если параметр установлен, то удаляет предыдущий словарь и инициализирует новый. Если нет,
        то обучает старый словарь
        :param batch_size: Размер порции обучения
        :param kwargs: Дополнительные именованные параметры для вызова _train
        :return: None
        """
        if not learning_content:
            assert spam or ham, 'One of spam or ham should be provided'

            learning_content = chain(((content, True) for content in spam.get_content()) if spam else (),
                                     ((content, False) for content in ham.get_content()) if ham else ())

        iterator = iter(learning_content)
        first = True

        while True:
            init = True if init and first else False
            content = list(islice(iterator, batch_size))
            cls._train(content, init=init, **kwargs)
            first = False

            if len(content) < batch_size:
                break

    @classmethod
    @abstractmethod
    def _train(cls, *args, **kwargs):
        pass

    @classmethod
    @abstractmethod
    def check_message_for_spam(cls, message: str, **kwargs) -> bool:
        pass

    @classproperty
    @abstractmethod
    def db_model(cls) -> Type[Model]:
        pass

    @classmethod
    def check_for_valid(cls, spam: MailContentSource, ham: MailContentSource, num_msg_to_check: int = 100, **kwargs) \
            -> List[Tuple[str, bool]]:
        """
        Проверяет модель на валидность и возвращает список сообщений, которые неправильно идентифицированы
        как спам или наоборот - хам идентифицирован как спам
        :param spam: MailContentSource со спамом
        :param ham: MailContentSource с хамом
        :param num_msg_to_check: Число сообщений попадающих в выборку
        :return: Список, содержащий ошибочно определенные сообщения. (сообщение, predicted_spam_flag)
        """
        errors = []

        def _check_for_errors(content: TIterable[str], spam_flag: bool):
            for index, msg in enumerate(content):
                logger.info('Proccess record: %d in %s spam dictionary' % (index, spam_flag))

                if spam_flag != cls.check_message_for_spam(msg, **kwargs):
                    errors.append((msg, spam_flag))

        _check_for_errors(spam.get_content(max_items=num_msg_to_check), True)
        _check_for_errors(ham.get_content(max_items=num_msg_to_check), False)

        return errors

class BayesModel(LearningModel):

    @classmethod
    def train(cls, spam: Optional[MailContentSource] = None, ham: Optional[MailContentSource] = None,
              learning_content: TIterable[Tuple[str, bool]] = None, init: bool = False, batch_size: int = 1000,
              min_num_word_appearance: int = 1) -> None:
        """
        Тренируем модель используя источники спам и хам писем MailContentSource
        :param spam: MailContentSource со спамом
        :param ham: MailContentSource с хамом
        :param learning_content: Уже готовый к обработке контент. Список с Tuple(сообщение, флаг спама)
        :param min_num_word_appearance: Минимальное количество встречаемости слова для попадания в словарь
        :param init: Если параметр установлен, то удаляет предыдущий словарь и инициализирует новый. Если нет,
        то обучает старый словарь
        :param batch_size: Размер порции обучения
        :return: None
        """
        return super().train(spam, ham, learning_content=learning_content, init=init, batch_size=batch_size,
                             min_num_word_appearance=min_num_word_appearance)

    @staticmethod
    def _train_parse(index, msg, spam) -> dict:
        dictionary = defaultdict(lambda: [0, 0, 0])
        value_list_index = 1 if spam else 2
        words = set(word for word in ContentParser.parse(msg).split(' ') if len(word) > 2) if msg else {}

        if words:
            logger.info('Proccess message N: %d' % index)
            for word in words:
                dictionary[word][0] += 1
                dictionary[word][value_list_index] += 1

            # Pickle не может задампить defaultdict
            return dict(dictionary)

    @classmethod
    def _train(cls, learning_content: Union[TIterable[Tuple[str, bool]], Tuple[str, bool]],
               min_num_word_appearance: int = 1, init: bool = False) -> None:
        """
        Обучает словарь по сообщениям из learning_content
        :param learning_content: список с Tuple или Tuple внутри которого сообщение и флаг спам или нет
        :param min_num_word_appearance: Минимальное количество встречаемости слова для попадания в словарь
        :param init: Если параметр установлен, то удаляет предыдущий словарь и инициализирует новый. Если нет,
        то обучает старый словарь
        :return: None
        """
        if isinstance(learning_content, tuple):
            learning_content = [learning_content]

        # word - ключ, value[0] - всего, value[1] - появлений в spam, value[2] - появлений в ham
        dictionary = defaultdict(lambda: [0, 0, 0])

        func_args = [((index, msg, spam_flag), {}) for index, (msg, spam_flag) in enumerate(learning_content)]

        results = ((k, v) for item in exec_in_parallel(cls._train_parse, func_args) if item
                   for k, v in item.items())

        for k, v in results:
            dictionary[k] = list(map(lambda x, y: x + y, dictionary[k], v))

        updates = [{
            'word': word,
            'spam_count': values[1],
            'ham_count': values[2]
        } for word, values in dictionary.items() if values[0] >= min_num_word_appearance]

        def on_commit(items, init_flag):
            if init_flag:
                invalidate_model(BayesDictionary)
            else:
                # у запроса aggregate также сбросится кэш при сбрасывании кэша одной записи
                for instance in items:
                    invalidate_obj(instance)

        if updates:
            with transaction.atomic():
                if init:
                    BayesDictionary.objects.all().delete()

                update_words = BayesDictionary.objects \
                    .bulk_update_or_create(updates, key_fields='word', returning='*',
                                           set_functions={'spam_count': '+', 'ham_count': '+'})
                transaction.on_commit(lambda: on_commit(update_words, init))

    @classmethod
    def check_message_for_spam(cls, message: str, return_probability: bool = False, clear_body: bool = False) \
            -> Union[int, bool]:
        """
        Проверяет сообщение на спам
        :param message: Сообщение, которое проверяется на спам
        :param return_probability: вернуть вероятность, вместо флага True/False
        :param clear_body: Было ли содержимое предварительно обработано
        :return: boolean. Спам или нет
        """
        parse_content = message if clear_body else ContentParser.parse(message)
        words = {item for item in parse_content.split(' ')}

        # все запросы к BayesDictionary кэшируются на сутки или до нового обучения словаря.
        aggr_sum = BayesDictionary.objects.aggregate(sum_spam=Sum('spam_count'), sum_ham=Sum('ham_count'))
        qs = BayesDictionary.objects.filter(word__in=words).values_list('word', 'spam_count', 'ham_count')
        word_dict = {item[0]: (item[1], item[2]) for item in qs}

        prob_word_spam = {word: (spam_freq / aggr_sum['sum_spam']) /
                                ((spam_freq / aggr_sum['sum_spam']) + (ham_freq / aggr_sum['sum_ham']))
                          for word, (spam_freq, ham_freq) in word_dict.items()}

        # n - количество учитываемых максимальных отклонений вероятности спамовости от 0.5
        n = 13
        max_prob_words = Counter()

        for word, probability in prob_word_spam.items():
            max_prob_words[word] = math.fabs((0.5 - probability))

        # p = p1p2p3...pn/(p1p2...pn + (1-p1)(1-p2)..(1-pn)), где pi - вероятность, что слово(i)-спам,
        # p - вероятность, что сообщение - спам
        multiple_spam_prob = 1
        multiple_ham_prob = 1

        for word, _ in max_prob_words.most_common(n):
            # Сглаживающая формула Pr(S|W) = (s*Pr(s) + n*Pr(S|W))/s+n
            s = 3  # должно быть s повторений слова в словаре, чтобы повысить к нему доверие
            word_occur = word_dict[word][0] + word_dict[word][1]
            spam_prob = (s * 0.5 + word_occur * prob_word_spam[word]) / (s + word_occur)

            multiple_spam_prob *= spam_prob
            multiple_ham_prob *= 1 - spam_prob

        spam_probability = multiple_spam_prob / (multiple_spam_prob + multiple_ham_prob)

        return spam_probability if return_probability else spam_probability > 0.9

    @classproperty
    def db_model(cls) -> Type[Model]:
        """
        Возвращает класс django.db.models, в котором хранятся данные модели
        :return: django.db.models class
        """
        return BayesDictionary

class NN(LearningModel):
    num_metrics = 7

    @classmethod
    def _update_model(cls, model: Sequential):
        weights = pickle.dumps(model.get_weights())
        NNStructure.objects.update_or_create(defaults={'weights': weights})

    @classmethod
    def _get_model(cls, init: bool = False) -> Sequential:
        """
        Пытается получить модель из БД или инициализирует новую
        :param init: Если False - попытаться получить модель из БД
        :return: Объект Sequential нейросети
        """
        model = Sequential()
        model.add(Dense(128, activation='relu', input_shape=(cls.num_metrics,)))
        model.add(Dropout(0.5))
        model.add(Dense(64, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(1, activation='sigmoid'))
        model.summary()
        model.compile(loss='binary_crossentropy',
                      optimizer='adam',
                      metrics=['acc', binary_accuracy])

        if not init:
            weights = pickle.dumps(model.get_weights())
            nn_struct, created = NNStructure.objects.get_or_create(weights=weights)

            if not created:
                model.set_weights(pickle.loads(nn_struct.weights))

        return model

    @staticmethod
    def _train_parse(index, msg, spam_flag) -> Optional[Tuple]:
        prepared_data = ContentParser.prepare_for_pnn(msg)

        if prepared_data:
            parsed_body, msg_info = prepared_data
            bayes_prob = BayesModel.check_message_for_spam(parsed_body, return_probability=True, clear_body=True)
            logger.info('Proccessed message N: %d' % index)

            return (*msg_info, bayes_prob), spam_flag

    @classmethod
    def _train(cls, learning_content: Union[TIterable[Tuple[str, bool]], Tuple[str, bool]], init: bool = False):
        """
        Обучает нейронную сеть используя learning_content
        :param learning_content: список с Tuple или Tuple внутри которого сообщение и флаг спам или нет
        :param init: Если параметр установлен, то удаляет предыдущий словарь и инициализирует новый. Если нет,
        то обучает старый словарь
        :return: None
        """
        if isinstance(learning_content, tuple):
            learning_content = [learning_content]

        func_args = [((index, msg, spam_flag), {}) for index, (msg, spam_flag) in enumerate(learning_content)]

        results = (item for item in exec_in_parallel(cls._train_parse, func_args, need_db_refresh=True) if item)

        # X - вход. Пустой np.array, который динамически заполнится далее
        x = np.empty((0, cls.num_metrics), dtype=np.float64)
        y = np.empty((0, 1), dtype=np.float64)
        num_msgs = 0

        for x_inp, y_inp in results:
            x = np.append(x, np.array([x_inp]), axis=0)
            y = np.append(y, np.array([y_inp], dtype=np.float64))
            num_msgs += 1

        y = y.reshape(-1, 1)

        epochs = num_msgs * 3
        model = cls._get_model(init=init)
        model.fit(x, y, batch_size=num_msgs, epochs=epochs, verbose=1, shuffle=True, validation_split=0.2)
        cls._update_model(model)

    @classmethod
    def check_for_valid(cls, spam: MailContentSource, ham: MailContentSource, num_msg_to_check: int = 100,
                        **kwargs) -> List[Tuple[str, bool]]:
        nn = cls._get_model()
        return super().check_for_valid(spam, ham, num_msg_to_check=num_msg_to_check, nn=nn)

    @classmethod
    def check_message_for_spam(cls, message: str, nn: Sequential = None) -> bool:
        """
        Проверяет сообщение на спам
        :param message: Сообщение
        :param nn: Опционально, экземпляр модели нейросети, чтобы не получать ее каждый раз из БД
        :return: boolean. Сообщение спам или нет
        """
        prepared_data = ContentParser.prepare_for_pnn(message)

        if prepared_data:
            nn = nn or cls._get_model()
            parsed_body, msg_info = prepared_data
            bayes_prob = BayesModel.check_message_for_spam(parsed_body, return_probability=True, clear_body=True)
            result = nn.predict(np.array([(*msg_info, bayes_prob)]))

            # При сравнении возвращается nparray([[boolean]]). Приведем к bool
            return bool(result > 0.6)
        else:
            return False

    @classproperty
    def db_model(cls) -> Type[Model]:
        """
        Возвращает класс django.db.models, в котором хранятся данные модели
        :return: django.db.models class
        """
        return NNStructure


import jax
import jax.numpy as jnp

import flax.nn as nn

from vit_jax import configs

class IdentityLayer(nn.Module):
  """Identity layer, convenient for giving a name to an array."""

  def apply(self, x):
    return x

class AddPositionEmbs(nn.Module):
  """Adds (optionally learned) positional embeddings to the inputs."""

  def apply(self, inputs, inputs_positions=None, posemb_init=None):
    """Applies AddPositionEmbs module.

    By default this layer uses a fixed sinusoidal embedding table. If a
    learned position embedding is desired, pass an initializer to
    posemb_init.

    Args:
      inputs: input data.
      inputs_positions: input position indices for packed sequences.
      posemb_init: positional embedding initializer.

    Returns:
      output: `(bs, timesteps, in_dim)`
    """
    # inputs.shape is (batch_size, seq_len, emb_dim).
    assert inputs.ndim == 3, ('Number of dimensions should be 3,'
                              ' but it is: %d' % inputs.ndim)
    pos_emb_shape = (1, inputs.shape[1], inputs.shape[2])
    pe = self.param('pos_embedding', pos_emb_shape, posemb_init)
    if inputs_positions is None:
      # Normal unpacked case:
      return inputs + pe
    else:
      # For packed data we need to use known position indices:
      return inputs + jnp.take(pe[0], inputs_positions, axis=0)

class MlpBlock(nn.Module):
  """Transformer MLP / feed-forward block."""

  def apply(self,
            inputs,
            mlp_dim,
            dtype=jnp.float32,
            out_dim=None,
            dropout_rate=0.1,
            deterministic=True,
            kernel_init=nn.initializers.xavier_uniform(),
            bias_init=nn.initializers.normal(stddev=1e-6)):
    """Applies Transformer MlpBlock module."""
    actual_out_dim = inputs.shape[-1] if out_dim is None else out_dim
    x = nn.Dense(
        inputs,
        mlp_dim,
        dtype=dtype,
        kernel_init=kernel_init,
        bias_init=bias_init)
    x = nn.gelu(x)
    x = nn.dropout(x, rate=dropout_rate, deterministic=deterministic)
    output = nn.Dense(
        x,
        actual_out_dim,
        dtype=dtype,
        kernel_init=kernel_init,
        bias_init=bias_init)
    output = nn.dropout(output, rate=dropout_rate, deterministic=deterministic)
    return output

class Encoder1DBlock(nn.Module):
  """Transformer encoder layer."""

  def apply(self,
            inputs,
            mlp_dim,
            dtype=jnp.float32,
            dropout_rate=0.1,
            attention_dropout_rate=0.1,
            deterministic=True,
            **attention_kwargs):
    """Applies Encoder1DBlock module.

    Args:
      inputs: input data.
      mlp_dim: dimension of the mlp on top of attention block.
      dtype: the dtype of the computation (default: float32).
      dropout_rate: dropout rate.
      attention_dropout_rate: dropout for attention heads.
      deterministic: bool, deterministic or not (to apply dropout).
      **attention_kwargs: kwargs passed to nn.SelfAttention

    Returns:
      output after transformer encoder block.
    """

    # Attention block.
    assert inputs.ndim == 3
    x = nn.LayerNorm(inputs, dtype=dtype)
    x = nn.SelfAttention(
        x,
        dtype=dtype,
        inputs_kv=x,
        attention_axis=(1,),
        causal_mask=False,
        kernel_init=nn.initializers.xavier_uniform(),
        broadcast_dropout=False,
        deterministic=deterministic,
        dropout_rate=attention_dropout_rate,
        **attention_kwargs)
    x = nn.dropout(x, rate=dropout_rate, deterministic=deterministic)
    x = x + inputs

    # MLP block.
    y = nn.LayerNorm(x, dtype=dtype)
    y = MlpBlock(
        y,
        mlp_dim=mlp_dim,
        dtype=dtype,
        dropout_rate=dropout_rate,
        deterministic=deterministic)

    return x + y

class Encoder(nn.Module):
  """Transformer Model Encoder for sequence to sequence translation."""

  def apply(self,
            inputs,
            num_layers,
            mlp_dim,
            inputs_positions=None,
            dropout_rate=0.1,
            train=False,
            **attention_kwargs):
    """Applies Transformer model on the inputs.

    Args:
      inputs: input data
      num_layers: number of layers
      mlp_dim: dimension of the mlp on top of attention block
      inputs_positions: input subsequence positions for packed examples.
      dropout_rate: dropout rate
      train: if it is training,
      **attention_kwargs: kwargs passed to nn.SelfAttention

    Returns:
      output of a transformer encoder.
    """
    assert inputs.ndim == 3  # (batch, len, emb)

    x = AddPositionEmbs(
        inputs,
        inputs_positions=inputs_positions,
        posemb_init=nn.initializers.normal(stddev=0.02),  # from BERT.
        name='posembed_input')
    x = nn.dropout(x, rate=dropout_rate, deterministic=not train)

    # Input Encoder
    for lyr in range(num_layers):
      x = Encoder1DBlock(
          x,
          mlp_dim=mlp_dim,
          dropout_rate=dropout_rate,
          deterministic=not train,
          name=f'encoderblock_{lyr}',
          **attention_kwargs)
    encoded = nn.LayerNorm(x, name='encoder_norm')

    return encoded

class VisionTransformer(nn.Module):
  """VisionTransformer."""

  def apply(self,
            x,
            num_classes=1000,
            train=False,
            resnet=None,
            patches=None,
            hidden_size=None,
            transformer=None,
            representation_size=None,
            classifier='gap'):

    n, h, w, c = x.shape

    # Embed the grid or patches of the grid.
    fh, fw = patches.size
    gh, gw = h // fh, w // fw
    if hidden_size:  # We can merge s2d+emb into a single conv; it's the same.
      x = nn.Conv(
          x,
          hidden_size, (fh, fw),
          strides=(fh, fw),
          padding='VALID',
          name='embedding')
    else:
      # This path often results in excessive padding.
      x = jnp.reshape(x, [n, gh, fh, gw, fw, c])
      x = jnp.transpose(x, [0, 1, 3, 2, 4, 5])
      x = jnp.reshape(x, [n, gh, gw, -1])
    # Here, x is a grid of embeddings.

    # (Possibly partial) Transformer.
    if transformer is not None:
      n, h, w, c = x.shape
      x = jnp.reshape(x, [n, h * w, c])

      # If we want to add a class token, add it here.
      if classifier == 'token':
        cls = self.param('cls', (1, 1, c), nn.initializers.zeros)
        cls = jnp.tile(cls, [n, 1, 1])
        x = jnp.concatenate([cls, x], axis=1)

      x = Encoder(x, train=train, name='Transformer', **transformer)

    if classifier == 'token':
      x = x[:, 0]
    elif classifier == 'gap':
      x = jnp.mean(x, axis=list(range(1, x.ndim - 1)))  # (1,) or (1,2)

    if representation_size is not None:
      x = nn.Dense(x, representation_size, name='pre_logits')
      x = nn.tanh(x)
    else:
      x = IdentityLayer(x, name='pre_logits')

    x = nn.Dense(x, num_classes, name='head', kernel_init=nn.initializers.zeros)
    return x

CONFIGS = {
    'ViT-B_16': configs.get_b16_config(),
    'ViT-B_32': configs.get_b32_config(),
    'ViT-L_16': configs.get_l16_config(),
    'ViT-L_32': configs.get_l32_config(),
    'testing': configs.get_testing(),
}
KNOWN_MODELS = {
    name: VisionTransformer.partial(**config)
    for name, config in CONFIGS.items()
}


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

class PRED(nn.Module):
    """  pred-coder """
    def __init__(self, look_back, look_forward , data_shape, concatTime=False):
        super(PRED, self).__init__()

        self.concatTime = concatTime

        self.channel_in = look_back + 2  if concatTime else look_back
        self.look_forward = look_forward
        self.data_shape = data_shape
        self.total_node = np.prod (data_shape)

        self.conv1 = nn.Conv2d(self.channel_in , 32, 3, padding=1)
        self.conv2 = nn.Conv2d(32, 64, 2 , padding=1)
        self.conv3 = nn.Conv2d(64, 128, 2 , padding=1)
        self.conv4 = nn.Conv2d(128, 256, 2 , padding=1)
        # self.conv5 = nn.Conv2d(256, 512, 2 , padding=1)

        self.pool2d_2 = nn.MaxPool2d(2)

        self.dense = nn.Linear( 2304 , self.total_node   )
        # self.dense2 = nn.Linear( self.total_node , self.look_forward * self.total_node   )

        self.convOut = nn.Conv2d( 1, look_forward, 3 , padding=1)
        self.convOut2 = nn.Conv2d( look_forward, look_forward, 3 , padding=1)

    def forward(self, x, x1):

        if self.concatTime :
            # x1 = F.pad(x1,(0,139))
            x1 = x1.unsqueeze(-1).repeat(1,1, 1,self.total_node)
            x1 = x1.reshape( x1.size(0), -1, self.data_shape[-2], self.data_shape[-1] )

            x = torch.cat( (x,x1), dim =-3)

        x1 = x1.view(x1.size(0),-1)

        x = F.relu(self.conv1(x))
        x = self.pool2d_2(x)

        x = F.relu(self.conv2(x))
        x = self.pool2d_2(x)

        x = F.relu(self.conv3(x))
        x = self.pool2d_2(x)

        x = F.relu(self.conv4(x))
        x = self.pool2d_2(x)

        # x = F.relu(self.conv5(x))
        # x = self.pool2d_2(x)

        x = x.view(x.size(0), -1)

        out = self.dense(x)
        # print(out.size())
        # out =  self.dense2(out)
        out = out.reshape ( x.size(0), 1, self.data_shape[-2], self.data_shape[-1] )
        # out = F.relu(self.deconv4(out))
        # out = F.relu(self.deconv5(out))
        out = (self.convOut(out))
        out = F.relu(self.convOut2(out))

        return out


#! /usr/bin/python3.6

# -*- coding: utf-8 -*-

import dill
import functools
import os
import sys
import time

import numpy as np

from indexed import IndexedOrderedDict

from PIL import Image, ImageTk

import multiprocessing as mp
from multiprocessing import Process, Pipe, Lock

sys.path.append("../math_numbers")
import prime_numbers_fun

PATH_ROOT_DIR = os.path.abspath(os.path.dirname(sys.argv[0]))+"/"

np.set_printoptions(threshold=sys.maxsize)

if __name__=='__main__':
    a = 5
    c = 7
    n = 16
    x = (a*np.arange(0, n)+c)%n
    print("x: {}".format(x))

    l = []
    i = 0
    for _ in range(0, n):
        i = (a*i+c)%n
        l.append(i)
    print("l: {}".format(l))

    sys.exit(0)

    n = 256

    x = np.arange(0, n)
    v1 = np.tile(x, n).reshape((n, n))

    v2 = np.dstack((v1, v1.T)).reshape((n**2, 2))
    arr_a, arr_c = v2.T

    x_i = np.zeros((n**2, ), dtype=np.int)

    lst_A = []
    for i in range(0, n):
        x_i = (arr_a*x_i+arr_c)%n
        lst_A.append(x_i)

    A = np.array(lst_A).T

    idxs = [np.unique(r).shape[0]==n for r in A]

    arr_a_c = v2[idxs]
    lst_a_c = sorted(list(map(tuple, arr_a_c.tolist())))
    # print("lst_a_c: {}".format(lst_a_c))

    unique_a = np.unique(arr_a_c[:, 0])
    unique_c = np.unique(arr_a_c[:, 1])
    print("unique_a: {}".format(unique_a.tolist()))
    print("unique_c: {}".format(unique_c.tolist()))


def pivot(my_list, pivot_index, end_index):
    """Rearrange the array and return the swap index
        (which had the pivot value?)
    """
    swap_index = pivot_index
    for i in range(pivot_index + 1, end_index + 1):
        if my_list[i] < my_list[pivot_index]:
            swap_index += 1
            swap(my_list, swap_index, i)
    swap(my_list, pivot_index, swap_index)
    return swap_index

def swap(my_lists, index1, index2):
    my_list[index1],  my_list[index2] = my_list[index2], my_list[index1]

def quick_sort_helper(my_list, left, right):
    if left < right:
        pivot_index = pivot(my_list, left, right)
        quick_sort_helper(my_list, left, pivot_index - 1)
        quick_sort_helper(my_list, pivot_index + 1, right)
    return my_list

def quick_sort(my_list):
    return quick_sort_helper(my_list, 0, len(my_list) - 1)

if __name__ == "__main__":
    my_list = [4,6,1,7,3,2,5]
    print(quick_sort(my_list))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 24 16:21:25 2023

@author: johnmorgan
"""

# Description seems to be off, no solution to check

def find_median(nums1, nums2):
    if len(nums1) >= len(nums2):
        long = nums1
        short = nums2
    else:
        long = nums2
        short = nums1
    if len(long) % 2 == 0:
        left_long = len(long) // 2 - 1
    else:
        left_long = len(long) // 2
    right_long = left_long + 1
    if len(short) % 2 == 0:
        left_short = len(short) // 2 - 1
    else:
        left_short = len(short) // 2
    right_short = left_short + 1
    while not ((long[left_long] < short[right_short] and \
               short[left_short] < long[right_long]) or \
               (left_long == 0 or left_short == 0)):
        if long[left_long] > short[right_short]:
            left_long = left_long // 2
            right_long = left_long + 1
        else:
            left_short = left_short // 2
            right_short = left_short + 1
    print(max(long[left_long], short[left_short]))
    print(min(short[right_short], long[right_long]))
    return (max(long[left_long], short[left_short]) + \
            min(short[right_short], long[right_long])) / 2

nums1 = [1,5,8]
nums2 = [4,7,9]
print(find_median(nums1, nums2))

# nums1 = [1,2]
# nums2 = [3,4]
# print(find_median(nums1, nums2))

# nums1 = [2,4,6,8,10,12]
# nums2 = [1,3,5,7,9,11]
# print(find_median(nums1, nums2))

# nums1 = [5,7,8,13,17,22,29]
# nums2 = [30,32,39,41]
# print(find_median(nums1, nums2))


import cv2
from cv2 import VideoWriter, VideoWriter_fourcc
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.cm as cm
from collections import Counter
import glob
import numpy as np
import math
import operator
from scipy.linalg import svd
import scipy.stats as ss
import matplotlib.pyplot as plt
import scipy.linalg as la
from matplotlib.image import imread
from enum import Enum

class Sex(Enum):
    male, female = range(2)

class Smoker(Enum):
    no, yes = range(2)

class Region(Enum):
    southwest, southeast, northwest, northeast = range(4)

np.random.seed(0)
imp_data = np.genfromtxt('insurance.csv', delimiter=',', encoding='utf8', dtype=np.str)
# dataN = data.astype(np.float)

# Separates header and data
feature_name, data = np.vsplit(imp_data, [1])

# x, y = np.hsplit(data, [-1])

n = len(data)

# Rows shuffled
np.random.shuffle(data)

# Calculates array index for splitting
spltIdx = int((2/3)*n)

# Training-validation data split
data_train, data_test = data[:spltIdx,:], data[spltIdx:,:]

x_tr, y_tr = np.hsplit(data_train, [-1])

def preprocessCat(dataMain):
    '''Changes categorical features to enumerated ones'''

    data = np.copy(dataMain)

    for i in range(len(data)):
        for s in Sex:
            if s.name == data[i][1]:
                data[i][1] = s.value
                break

        for sm in Smoker:
            if sm.name == data[i][4]:
                data[i][4] = sm.value
                break

        for r in Region:
            if r.name == data[i][5]:
                data[i][5] = r.value
                break

    return data

def lse(x,y, addBias):
    '''Calculates weights using LSE'''

    dataX = x.astype(np.float64)
    dataY = y.astype(np.float64)

    if addBias:
        biasF = np.ones(((len(dataX)), 1))
        X = np.hstack((biasF, dataX))
    else:
        X = np.array(dataX)

    dotX = (1/len(x)) * np.linalg.inv(np.dot(X.T,X))
    dotY = np.dot(X.T,dataY)

    w = np.dot(dotX, dotY)

    return w

def rmse(w, x, y):
    '''Calculates RMSE'''

    x = x.astype(np.float64)
    y = y.astype(np.float64)

    Y = np.zeros(shape=((len(y)),1))

    for j in range(len(y)):
        Y[j] =  (w[1] * x[j][0]) + (w[2] * x[j][1]) + (w[3] * x[j][2]) + (w[4] * x[j][3]) + (w[5] * x[j][4]) + (w[6] * x[j][5]) + w[0]

    return np.sqrt(np.mean((Y-y)**2))

# Change categorical features to enumerated ones (NO BIAS)
x_tr_preC = preprocessCat(data_train)
w_tr_preC = lse(x_tr_preC, y_tr, False)
rmse_tr_preC = rmse(w_tr_preC, x_tr_preC, y_tr)

# for j in range(len(y_train)):
#     Y[j] = w_t_preC[1]*x[j] + w[0])

print(w_tr_preC)
print(rmse_tr_preC)


# -*- coding: utf-8 -*-
from .cipher import Cipher
""" implementacao da cifra Railfence """

class Railfence(Cipher):
    ''' Cifra railfence '''
    def gen_railfence(self, tam, key):
        '''
        Retorna um lista de inteiros com a posicao da linha que o caracter
        do texto ira ocupar, variando de 0 ate key - 1.
        '''
        j = 0
        inc = 0
        idx = []
        for i in range(tam):
            if j == key - 1:
                inc = -1
            elif j == 0:
                inc = 1
            idx.append(j)
            j += inc
        return idx

    def encrypt(self, texto, key):
        '''
        Retorna o texto plano cifrado na cifra rail fence com a chave key.
        '''
        tabela = []
        for i in range(key):
            tabela.append([])
        i, idx = 0, 1
        texto = self.format_str(texto)
        for ch in texto:
            tabela[i].append(ch)
            if i == key - 1:
                idx = -1
            elif i < 1:
                idx = 1
            i += idx
        saida = ''
        for tab in tabela:
            saida += ''.join(tab)
        return saida

    def decrypt(self, texto, key):
        '''
        Retorna o texto plano para um texto cifrado com a cifra railfence
        com a chave key.
        '''
        texto = self.format_str(texto)
        tam = len(texto)
        # gera lista com os indices do railfence
        idx = self.gen_railfence(tam, key)
        # ordena a lista de indices do railfence
        idx_sorted = sorted(idx)
        # faz uma busca em idx e idx_sorted, verificando os indices
        # e colocando os caracters de acordo com a posicao no railfence
        texto_plano = ''
        for i in range(tam):
            for j in range(tam):
                if idx[i] == idx_sorted[j] and idx[i] > -1:
                    texto_plano += texto[j]
                    idx[i] = -1
                    idx_sorted[j] = -1
        return texto_plano.lower()


'''
# comment

# 가장 긴 증가하는 부분 수열의 결과를 뒤집으면 됨

'''

n = int(input())
li = list(map(int, input().split()))[::-1]

dp = [1] * n

for i in range(0, n - 1 + 1):
    for j in range(i + 1, n - 1 + 1):
        if li[i] < li[j]:
            dp[j] = max(dp[j], dp[i] + 1)

print(max(dp))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 13 16:04:07 2018

@author: GiselleTavares
"""

low = 0
high = 100
guessNumber = int((high-low)/2)
indicate = ''

print("Please think of a number between 0 and 100!")

while indicate != 'c':
    print("Is your secret number " + str(int(guessNumber)) + "? ")
    indicate = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ").lower()

    if indicate == 'l':
        low = int(guessNumber)
    elif indicate == 'h':
        high = int(guessNumber)
    elif indicate == 'c':
        print("Game over. Your secret number was: " + str(int(guessNumber)))
        break;
    else:
        print("Sorry, I did not understand your input.")
    guessNumber = int(high+low)/2


import time
from functools import wraps

def fn_timer(function):
    @wraps(function)
    def function_timer(*args, **kwargs):
        t0 = time.time()
        result = function(*args, **kwargs)
        t1 = time.time()
        print("Time usage of function '{}' is {:.5f}s".format(function.__name__, t1 - t0))
        return result
    return function_timer

@fn_timer
def say_hello(name):
    print('Hello {}'.format(name))

if __name__ == '__main__':
    say_hello('Vincent')


import warnings

import torch
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import ConvModule, xavier_init
from mmcv.runner import auto_fp16
from .darts import genotypes as gt

from ..builder import NECKS

from .darts.search_cell import SearchCell

@NECKS.register_module()
class AttentionDarts(nn.Module):
    """ SearchCNN controller supporting multi-gpu """

    def __init__(self,
                 in_channels,
                 out_channels,
                 num_outs,
                 start_level=0,
                 end_level=-1,
                 add_extra_convs=False,
                 extra_convs_on_inputs=True,
                 relu_before_extra_convs=False,
                 no_norm_on_lateral=False,
                 conv_cfg=None,
                 norm_cfg=None,
                 act_cfg=None,
                 upsample_cfg=dict(mode='nearest'),
                 reduction_ratio=16,
                 kernel_size=7):
        super(AttentionDarts, self).__init__()

        # initialize architect parameters: alphas
        n_ops = len(gt.PRIMITIVES)

        self.alpha = nn.ParameterList()
        for i in range(4):
            self.alpha.append(nn.Parameter(1e-3 * torch.rand(n_ops)))

        # setup alphas list
        self._alphas = []
        for n, p in self.named_parameters():
            if 'alpha' in n:
                self._alphas.append((n, p))

        self.net = Attention(
            in_channels,
            out_channels,
            num_outs,
            start_level=start_level,
            end_level=end_level,
            add_extra_convs=add_extra_convs,
            extra_convs_on_inputs=extra_convs_on_inputs,
            relu_before_extra_convs=relu_before_extra_convs,
            no_norm_on_lateral=no_norm_on_lateral,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg,
            act_cfg=act_cfg,
            upsample_cfg=upsample_cfg,
            reduction_ratio=reduction_ratio,
            kernel_size=kernel_size
        )

    # default init_weights for conv(msra) and norm in ConvModule
    def init_weights(self):
        """Initialize the weights of FPN module."""
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                xavier_init(m, distribution='uniform')

    def forward(self, x):
        weights = [F.softmax(alpha, dim=-1) for alpha in self.alpha]
        return self.net(x, weights)

    def genotype(self):
        gene_level0 = gt.parse(self.alpha[0])
        gene_level1 = gt.parse(self.alpha[1])
        gene_level2 = gt.parse(self.alpha[2])
        gene_level3 = gt.parse(self.alpha[3])

        return gt.Genotype(
            level0=gene_level0,
            level1=gene_level1,
            level2=gene_level2,
            level3=gene_level3)

    def weights(self):
        return self.net.parameters()

    def alphas(self):
        for n, p in self._alphas:
            yield p

    def named_alphas(self):
        for n, p in self._alphas:
            yield n, p

# from models.search_cells import SearchCell

class Attention(nn.Module):
    r"""Feature Pyramid Network.

    This is an implementation of paper `Feature Pyramid Networks for Object
    Detection <https://arxiv.org/abs/1612.03144>`_.

    Args:
        in_channels (List[int]): Number of input channels per scale.
        out_channels (int): Number of output channels (used at each scale)
        num_outs (int): Number of output scales.
        start_level (int): Index of the start input backbone level used to
            build the feature pyramid. Default: 0.
        end_level (int): Index of the end input backbone level (exclusive) to
            build the feature pyramid. Default: -1, which means the last level.
        add_extra_convs (bool | str): If bool, it decides whether to add conv
            layers on top of the original feature maps. Default to False.
            If True, its actual mode is specified by `extra_convs_on_inputs`.
            If str, it specifies the source feature map of the extra convs.
            Only the following options are allowed

            - 'on_input': Last feat map of neck inputs (i.e. backbone feature).
            - 'on_lateral':  Last feature map after lateral convs.
            - 'on_output': The last output feature map after fpn convs.
        extra_convs_on_inputs (bool, deprecated): Whether to apply extra convs
            on the original feature from the backbone. If True,
            it is equivalent to `add_extra_convs='on_input'`. If False, it is
            equivalent to set `add_extra_convs='on_output'`. Default to True.
        relu_before_extra_convs (bool): Whether to apply relu before the extra
            conv. Default: False.
        no_norm_on_lateral (bool): Whether to apply norm on lateral.
            Default: False.
        conv_cfg (dict): Config dict for convolution layer. Default: None.
        norm_cfg (dict): Config dict for normalization layer. Default: None.
        act_cfg (str): Config dict for activation layer in ConvModule.
            Default: None.
        upsample_cfg (dict): Config dict for interpolate layer.
            Default: `dict(mode='nearest')`

    Example:
        >>> import torch
        >>> in_channels = [2, 3, 5, 7]
        >>> scales = [340, 170, 84, 43]
        >>> inputs = [torch.rand(1, c, s, s)
        ...           for c, s in zip(in_channels, scales)]
        >>> self = Attention(in_channels, 11, len(in_channels)).eval()
        >>> outputs = self.forward(inputs)
        >>> for i in range(len(outputs)):
        ...     print(f'outputs[{i}].shape = {outputs[i].shape}')
        outputs[0].shape = torch.Size([1, 11, 340, 340])
        outputs[1].shape = torch.Size([1, 11, 170, 170])
        outputs[2].shape = torch.Size([1, 11, 84, 84])
        outputs[3].shape = torch.Size([1, 11, 43, 43])
    """

    def __init__(self,
                 in_channels,
                 out_channels,
                 num_outs,
                 start_level=0,
                 end_level=-1,
                 add_extra_convs=False,
                 extra_convs_on_inputs=True,
                 relu_before_extra_convs=False,
                 no_norm_on_lateral=False,
                 conv_cfg=None,
                 norm_cfg=None,
                 act_cfg=None,
                 upsample_cfg=dict(mode='nearest'),
                 reduction_ratio=16,
                 kernel_size=7):
        super(Attention, self).__init__()
        assert isinstance(in_channels, list)
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.num_ins = len(in_channels)
        self.num_outs = num_outs
        self.relu_before_extra_convs = relu_before_extra_convs
        self.no_norm_on_lateral = no_norm_on_lateral
        self.fp16_enabled = False
        self.upsample_cfg = upsample_cfg.copy()

        if end_level == -1:
            self.backbone_end_level = self.num_ins
            assert num_outs >= self.num_ins - start_level
        else:
            # if end_level < inputs, no extra level is allowed
            self.backbone_end_level = end_level
            assert end_level <= len(in_channels)
            assert num_outs == end_level - start_level
        self.start_level = start_level
        self.end_level = end_level

        self.fusion_attentions = nn.ModuleList()
        self.downsample_convs = nn.ModuleList()
        for i in range(self.start_level, self.backbone_end_level):
            d_conv = nn.ModuleList()
            for j in range(self.start_level, self.backbone_end_level):
                temp = []
                for _ in range(i - j):
                    temp.append(ConvModule(
                        out_channels,
                        out_channels,
                        3,
                        stride=2,
                        padding=1,
                        inplace=False))
                d_conv.append(nn.Sequential(*temp))
            self.fusion_attentions.append(
                SearchCell(
                    gate_channels=out_channels * (self.num_ins - start_level),
                    levels=self.num_ins - start_level,
                    reduction_ratio=reduction_ratio,
                    kernel_size=kernel_size
                )
            )
            self.downsample_convs.append(d_conv)

        self.add_extra_convs = add_extra_convs
        assert isinstance(add_extra_convs, (str, bool))
        if isinstance(add_extra_convs, str):
            # Extra_convs_source choices: 'on_input', 'on_lateral', 'on_output'
            assert add_extra_convs in ('on_input', 'on_lateral', 'on_output')
        elif add_extra_convs:  # True
            if extra_convs_on_inputs:
                # TODO: deprecate `extra_convs_on_inputs`
                warnings.simplefilter('once')
                warnings.warn(
                    '"extra_convs_on_inputs" will be deprecated in v2.9.0,'
                    'Please use "add_extra_convs"', DeprecationWarning)
                self.add_extra_convs = 'on_input'
            else:
                self.add_extra_convs = 'on_output'

        self.lateral_convs = nn.ModuleList()
        self.fpn_convs = nn.ModuleList()

        for i in range(self.start_level, self.backbone_end_level):
            l_conv = ConvModule(
                in_channels[i],
                out_channels,
                1,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg if not self.no_norm_on_lateral else None,
                act_cfg=act_cfg,
                inplace=False)
            fpn_conv = ConvModule(
                out_channels,
                out_channels,
                3,
                padding=1,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                act_cfg=act_cfg,
                inplace=False)

            self.lateral_convs.append(l_conv)
            self.fpn_convs.append(fpn_conv)

        # add extra conv layers (e.g., RetinaNet)
        extra_levels = num_outs - self.backbone_end_level + self.start_level
        if self.add_extra_convs and extra_levels >= 1:
            for i in range(extra_levels):
                if i == 0 and self.add_extra_convs == 'on_input':
                    in_channels = self.in_channels[self.backbone_end_level - 1]
                else:
                    in_channels = out_channels
                extra_fpn_conv = ConvModule(
                    in_channels,
                    out_channels,
                    3,
                    stride=2,
                    padding=1,
                    conv_cfg=conv_cfg,
                    norm_cfg=norm_cfg,
                    act_cfg=act_cfg,
                    inplace=False)
                self.fpn_convs.append(extra_fpn_conv)

    # default init_weights for conv(msra) and norm in ConvModule
    def init_weights(self):
        """Initialize the weights of FPN module."""
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                xavier_init(m, distribution='uniform')

    @auto_fp16()
    def forward(self, inputs, weights):
        """Forward function."""
        assert len(inputs) == len(self.in_channels)

        # build laterals
        laterals = [
            lateral_conv(inputs[i + self.start_level])
            for i, lateral_conv in enumerate(self.lateral_convs)
        ]

        # build top-down path
        used_backbone_levels = len(laterals)
        outs = []
        for i in range(used_backbone_levels):
            shape = laterals[i].shape[2:]
            samples = []
            samples.extend([self.downsample_convs[i][j](laterals[j]) for j in
                            range(i)])
            samples.append(laterals[i])
            samples.extend([F.interpolate(laterals[j], size=shape, **self.upsample_cfg) for j in
                            range(i + 1, used_backbone_levels)])
            outs.append(self.fusion_attentions[i](samples, weights[i]))

        # build outputs+
        # part 1: from original levels
        outs = [
            self.fpn_convs[i](outs[i]) for i in range(used_backbone_levels)
        ]
        # part 2: add extra levels
        if self.num_outs > len(outs):
            # use max pool to get more levels on top of outputs
            # (e.g., Faster R-CNN, Mask R-CNN)
            if not self.add_extra_convs:
                for i in range(self.num_outs - used_backbone_levels):
                    outs.append(F.max_pool2d(outs[-1], 1, stride=2))
            # add conv layers on top of original feature maps (RetinaNet)
            else:
                if self.add_extra_convs == 'on_input':
                    extra_source = inputs[self.backbone_end_level - 1]
                elif self.add_extra_convs == 'on_lateral':
                    extra_source = laterals[-1]
                elif self.add_extra_convs == 'on_output':
                    extra_source = outs[-1]
                else:
                    raise NotImplementedError
                outs.append(self.fpn_convs[used_backbone_levels](extra_source))
                for i in range(used_backbone_levels + 1, self.num_outs):
                    if self.relu_before_extra_convs:
                        outs.append(self.fpn_convs[i](F.relu(outs[-1])))
                    else:
                        outs.append(self.fpn_convs[i](outs[-1]))
        return tuple(outs)


def partition_objects(n, m):
    """
    function that counts the ways to partition n objects using parts up to n
    :param n: number of objects
    :param m: parts for partition
    :return: number of ways
    """
    if n == 0:
        # only one way to partition zero things
        return 1
    elif m == 0 or n < 0:
        # if we are not allowed to use any parts then we cannot construct a partition
        # it is also not sensible to try and partition a negative quantity of things
        return 0
    else:
        # if e.g. we partition 7 objects in 4 parts, at some point we will have to use
        # up all the parts of 3. hence partition_objects(m, n - 1) is a subset of
        # partition_objects(m, n). finally, the remaining partitions all use m itself as a
        # partition -> we subtract it and add the two values together.
        return partition_objects(n - m, m) + partition_objects(n, m - 1)

assert partition_objects(6, 4) == 9
assert partition_objects(5, 5) == 7
assert partition_objects(5, 2) == 3
assert partition_objects(5, 3) == 5
assert partition_objects(9, 5) == 23

def hanoi(m, start, end):
    global count
    if m == 0:
        return
    open_pole = set(["Pole 1", "Pole 2", "Pole 3"]).difference(set([start, end])).pop()

    hanoi(m - 1, start, open_pole)

    print(f"Move disc {m} from {start} to {end}")
    count += 1

    hanoi(m - 1, open_pole, end)

print("####")
count = 0
hanoi(3, "Pole 1", "Pole 3")
print(count)

print("####")
count = 0
hanoi(4, "Pole 1", "Pole 3")
print(count)

print("####")
count = 0
hanoi(5, "Pole 1", "Pole 3")
print(count)


from django import forms
from .models import Comment, Cocomment

class CommentModelForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['content']
        widgets = {
            'content' : forms.Textarea(attrs={'maxlength' : 500,})
        }
    def clean_content(self):
        content=self.cleaned_data.get('content','')
        bad_words = [
            '씨발', '좆', '씹', '자지', '보지', '개새',
            '씨부랄', '부랄', '씨팔', '니미', '니애미',
            '느금', '니애비', 'ㅈ같', '샹년', '시발', '시바', '씨바',
            '씨8', '시8', '십8',
            ]
        for w in bad_words:
            if w in content:
                content = content.replace(w, '*'*len(w))
        return content

class CocommentModelForm(CommentModelForm):
    pass


# import other python files
import tasks

import interface

def main():
    print("National Parks")
    # import csv file
    parksList = tasks.createListOffParks("national_parks.csv")
    menuDict = interface.getMenuDict()
    userChoice = ""
    # use branching to provide the user with different options
    while userChoice != "Q":
        interface.displayMenu(menuDict)
        userChoice = interface.getUserInput(menuDict)
        if userChoice == "A":
            interface.printAllParks(parksList)
        if userChoice == "B":
            interface.printParksInState(parksList)
        if userChoice == "C":
            interface.printLargestPark(parksList)
        if userChoice == "D":
            interface.printParksForSearch(parksList)
        if userChoice == "E":
            interface.printOldestPark(parksList)
        if userChoice == "F":
            interface.printMostCommonState(parksList)

main()


import numpy as np
import matplotlib.pyplot as plt

# Load data:
c24ord = np.genfromtxt('new_24ord.dat', dtype = 'float', delimiter = '')
c24uno = np.genfromtxt('new_24uno.dat', dtype = 'float', delimiter = '')

c10ord = np.genfromtxt('new_10ord.dat', dtype = 'float', delimiter = '')
c10uno  = np.genfromtxt('new_10uno.dat', dtype = 'float', delimiter = '')

# T = 2.4
cycles_24ord = c24ord[:,5]
cycles_24uno = c24uno[:,5]

accepted_24ord = c24ord[:,6]
accepted_24uno = c24uno[:,6]

# T = 1.0
cycles_10ord = c10ord[:,5]
cycles_10uno = c10uno[:,5]

accepted_10ord = c10ord[:,6]
accepted_10uno = c10uno[:,6]

#plot T = 2.4
plt.figure()
plt.plot((cycles_24ord[:35]), accepted_24ord[:35], label = 'T = 2.4, (ordered)')
plt.plot((cycles_24uno[:35]), accepted_24uno[:35], label = 'T = 2.4 , (unordered)')
plt.legend(loc = 4)
plt.xlabel('Monte Carlo cycles')
plt.ylabel('Accepted states')
plt.savefig('c_num_accepted_24')

#plot T = 1.0
plt.figure()
plt.plot((cycles_10ord), accepted_10ord, label = 'T = 1.0, (orderd)')
plt.plot((cycles_10uno), accepted_10uno, label = 'T = 1.0, (unorderd)')
plt.legend(loc = 4)
plt.xlabel('Monte Carlo cycles')
plt.ylabel('Accepted states')
plt.savefig('c_num_accepted_10')

# accepted states as a function of temperature:
T = np.array([1.0, 2.4])

plt.figure()
plt.bar(T, [np.sum(accepted_10ord), np.sum(accepted_24ord)], align = 'center', log = True, width = 0.2 ,  color = 'r', label = 'orderd')
plt.bar(T,[np.sum(accepted_10uno), np.sum(accepted_24uno)], align = 'edge', log = True, width=0.1 ,  label = 'unorderd')

plt.legend(loc = 2)
plt.xlabel('Temperature T')
plt.ylabel('Accepted states')
plt.savefig('c_T_accepted')


import tensorflow as tf
import numpy as np

#乱数产生训练资料
x1=np.random.random((500,1))  #产生五百笔 0~1的资料。
x2=np.random.random((500,1))+1  #同上，产生完后全部+1，所以会是1~2。
x_train=np.concatenate((x1, x2))  #资料串起来。

y1=np.zeros((500,), dtype=int)  #产生五百笔 0 的资料
y2=np.ones((500,), dtype=int)   #产生五百笔 1
y_train=np.concatenate((y1, y2))  #也是把资料串起来

#建立模型
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(10, activation=tf.nn.relu, input_dim=1), #input_dim=特征数
  tf.keras.layers.Dense(10, activation=tf.nn.relu),
  tf.keras.layers.Dense(2, activation=tf.nn.softmax)  #第一个参数2=答案类别数。
])
"""

# 用像list增加的方式，一个一个加上去。
model.add(tf.keras.layers.Dense(units=10,
                 activation=tf.nn.relu,    #tf.nn.relu
                 input_dim=1))
model.add(tf.keras.layers.Dense(units=10,
                 activation=tf.nn.relu ))  # tf.nn.relu
model.add(tf.keras.layers.Dense(units=2,
                 activation=tf.nn.softmax ))  #tf.nn.softmax

"""
#编译
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

#训练
model.fit(x_train, y_train,
          epochs=10,
          batch_size=50)  #batch_size 如果不塞 他就是自动预设

#不用加上print，直接print过程。
model.summary()

#测试
x_test=np.array([[0.22],[0.31],[1.22],[1.33]])  #准备测试资料
y_test=np.array([0,0,1,1])

score = model.evaluate(x_test, y_test, batch_size=128)  #测试特征、测试答案、单次评估数量。
print("score:",score)   #score: [0.26251232624053955, 1.0]

predict = model.predict(x_test)

print("predict:",predict)   #预测答案几率

predict_class=model.predict_classes(x_test)
print("prdict_classes：\r\n",predict_class)  #预测答案

#预测答案 np.argmax(predict[0])
print("Ans:",np.argmax(predict[0]),np.argmax(predict[1]),np.argmax(predict[2]),np.argmax(predict[3]))


import pandas as pd
from datetime import datetime
import datetime as dt
import numpy as np

# Load the data into a pandas DataFrame
df_UCAD = pd.read_csv("USDCAD5.csv")
df_GU = pd.read_csv("GBPUSD5.csv")
df_EU = pd.read_csv("EURUSD5.csv")
df_UCHF = pd.read_csv("USDCHF5.csv")
df_USEK = pd.read_csv("USDSEK5.csv")
df_UJ = pd.read_csv("USDJPY5.csv")

"""
Forumula for dxy:
USDX = 50.14348112  EURUSD^-0.576  USDJPY^0.136  GBPUSD^-0.119  USDCAD^0.091  USDSEK^0.042  USDCHF^0.036
"""

def exportDate(df, start_date, end_date=None):
    if end_date == None:
        datesDf = df["date"] == start_date
    else:
        datesDf = (df["date"] >= start_date) & (df["date"] <= end_date)
    structuredDf = df[datesDf]

    return structuredDf

def structureMT4Data(df):  # df cleaning and formatting
    df.columns = ["date", "time", "open", "high", "low", "close", "volume"]

    df["date"] = pd.to_datetime(df["date"])
    df["date"] = pd.to_datetime(df["date"], format="%Y%m/%d")
    df["time"] = pd.to_datetime(df["time"], format="%H:%M").dt.time
    return df

dfUCAD = structureMT4Data(df_UCAD)
dfGU = structureMT4Data(df_GU)
dfEU = structureMT4Data(df_EU)
dfCHF = structureMT4Data(df_UCHF)
dfSEK = structureMT4Data(df_USEK)
dfUJ = structureMT4Data(df_UJ)

dfUCAD = exportDate(dfUCAD, "2022-02-01", "2023-02-28")
dfGU = exportDate(dfGU, "2021-02-01", "2023-02-28")
dfEU = exportDate(dfEU, "2021-02-01", "2023-02-28")
dfCHF = exportDate(dfCHF, "2021-02-01", "2023-02-28")
dfSEK = exportDate(dfSEK, "2021-02-01", "2023-02-28")
dfUJ = exportDate(dfUJ, "2021-02-01", "2023-02-28")

# Merge the data frames based on the date and time column
df = pd.merge(dfUCAD, dfGU, on=["date", "time"])
df = pd.merge(df, dfEU, on=["date", "time"])
df = pd.merge(df, dfCHF, on=["date", "time"])
df = pd.merge(df, dfSEK, on=["date", "time"])
df = pd.merge(df, dfUJ, on=["date", "time"])
df.columns = [
    "date",
    "time",
    "UCAD_open",
    "UCAD_high",
    "UCAD_low",
    "UCAD_close",
    "UCAD_volume",
    "GU_open",
    "GU_high",
    "GU_low",
    "GU_close",
    "GU_volume",
    "EU_open",
    "EU_high",
    "EU_low",
    "EU_close",
    "EU_volume",
    "UCHF_open",
    "UCHF_high",
    "UCHF_low",
    "UCHF_close",
    "UCHF_volume",
    "USEK_open",
    "USEK_high",
    "USEK_low",
    "USEK_close",
    "USEK_volume",
    "UJ_open",
    "UJ_high",
    "UJ_low",
    "UJ_close",
    "UJ_volume",
]

print(df)

dxy = pd.DataFrame()
dxy["date"] = df["date"]
dxy["time"] = df["time"]
dxy["open"] = (
    50.14348112
    * df["UCAD_open"] ** 0.091
    * df["GU_open"] ** (-0.119)
    * df["EU_open"] ** (-0.576)
    * df["UJ_open"] ** 0.136
    * df["USEK_open"] ** 0.042
    * df["UCHF_open"] ** 0.036
)
dxy["high"] = (
    50.14348112
    * df["UCAD_high"] ** 0.091
    * df["GU_high"] ** (-0.119)
    * df["EU_high"] ** (-0.576)
    * df["UJ_high"] ** 0.136
    * df["USEK_high"] ** 0.042
    * df["UCHF_high"] ** 0.036
)
dxy["low"] = (
    50.14348112
    * df["UCAD_low"] ** 0.091
    * df["GU_low"] ** (-0.119)
    * df["EU_low"] ** (-0.576)
    * df["UJ_low"] ** 0.136
    * df["USEK_low"] ** 0.042
    * df["UCHF_low"] ** 0.036
)
dxy["close"] = (
    50.14348112
    * df["UCAD_close"] ** 0.091
    * df["GU_close"] ** (-0.119)
    * df["EU_close"] ** (-0.576)
    * df["UJ_close"] ** 0.136
    * df["USEK_close"] ** 0.042
    * df["UCHF_close"] ** 0.036
)
print(dxy)
dxy.to_csv("dxy5.csv", index=False)


import math
import unittest

from igc.modules.base.igc_llm_base_module import LlmModule
from igc.modules.base.igc_llm_metrics_type import MetricType
from igc.modules.base.prompt_types import PromptType

class TestExactMatch(unittest.TestCase):

    def test_exact_match(self):
        predictions = ["This is a test",
                       "The sky is blue",
                       "Movie matrix"]
        targets = ["This is a test",
                   "The sky is blue",
                   "Movie matrix"]

        prompt_types = PromptType.get_all_types()
        expected_results = [1.0, 1.0, 1.0, 1.0, 1.0]

        for prompt_type, expected_result in zip(prompt_types, expected_results):
            result = LlmModule.performance_metric(predictions, targets, MetricType.EXACT_MATCH, prompt_type)
            error_msg = f"Prompt type: {prompt_type.value}, Expected: {expected_result}, Actual: {result}"
            self.assertEqual(expected_result, result, error_msg)

    def test_performance_metric_question(self):
        """
        Test case for performance metric with prompt type "question".

        :return:
        """
        # 3 prediction space before , after and A prefix
        predictions = [
            "A: France",
            "A: eight planets, 146 moons ",
            "A:Jane Austen"
        ]

        expected_answers = [
            "France",
            "eight planets, 146 moons",
            "Jane Austen"
        ]

        expected_result = 1.0
        result = LlmModule.performance_metric(predictions,
                                              expected_answers,
                                              MetricType.EXACT_MATCH,
                                              PromptType.QUESTION,
                                              prefix_to_remove="A:")

        error_msg = f"Prompt type: {PromptType.QUESTION.value}, Expected: {expected_result}, Actual: {result}"
        self.assertEqual(expected_result, result, error_msg)

    def test_question_normalization(self):
        """

        :return:
        """
        predictions = [
            "Q: What is the capital of France?",
            "Q: How many planets are there in the solar system?",
            "Q: Who wrote the novel 'Pride and Prejudice'?"
        ]
        expected_normalized_questions = [
            ": What is the capital of France?",
            ": How many planets are there in the solar system?",
            ": Who wrote the novel 'Pride and Prejudice'?"
        ]

        normalized_questions = [
            LlmModule._normalize(p, "Q") for p in predictions
        ]

        for q, expected_q in zip(normalized_questions, expected_normalized_questions):
            self.assertEqual(expected_q, q)

    def test_rouge_metric_negative(self):
        """

        :return:
        """
        predictions = ["This is a test",
                       "The",
                       "z"]
        targets = ["123",
                   "456",
                   "review"]

        expected_result = 0.0
        result = LlmModule.performance_metric(predictions,
                                              targets,
                                              MetricType.ROUGE,
                                              PromptType.CUSTOM,
                                              callback=LlmModule.compute_rouge_metric)

        error_msg = f"Prompt type: {PromptType.CUSTOM.value}, Expected: {expected_result}, Actual: {result}"
        self.assertEqual(expected_result, result, error_msg)

    def test_rouge_metric_positive(self):
        """

        :return:
        """

        predictions = ["The cat is on the mat"]
        targets = ["The cat is sitting on the mat"]

        expected_result = 0.92
        result = LlmModule.performance_metric(predictions,
                                              targets,
                                              MetricType.ROUGE,
                                              PromptType.CUSTOM,
                                              callback=LlmModule.compute_rouge_metric)

        error_msg = f"Prompt type: {PromptType.CUSTOM.value}, Expected: {expected_result}, Actual: {result}"
        self.assertTrue(math.isclose(expected_result, result, rel_tol=1e-2), error_msg)

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


#Tính BMI và in ra đánh giá
import math
def tinh_bmi(cn,cc):
    bmi=cn/math.pow(cc,2)
    return bmi
def danh_gia(bmi):
	if bmi<18.5:
		return 'Bạn đang ở trạng thái gầy'
	elif 18.5<=bmi<=24.99:
		return 'Bạn đang ở trạng thái bình thường'
	else:
		return'Bạn đang ở trạng thái thừa cân'
cn=int(input("Nhập cân nặng (kg): "))
cc=float(input("Nhập chiều cao (m): "))
bmi=tinh_bmi(cn,cc)
print("BMI = %0.2f"%bmi)
print(danh_gia(bmi))


#불러와서 모델 완성
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
path = 'd:/study_data/_data/cat_dog/Petimages/'
save_path = 'd:/study_data/_save/cat_dog/'

x_train = np.load(save_path + 'cd_x_train.npy')
x_test = np.load(save_path + 'cd_x_test.npy')
y_train = np.load(save_path + 'cd_y_train.npy')
y_test = np.load(save_path + 'cd_y_test.npy')

#2. MODEL
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten

model = Sequential()
model.add(Conv2D(64, (2, 2), input_shape=(200, 200, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

#3. COMPILE
from tensorflow.python.keras.callbacks import EarlyStopping
model.compile(loss = 'binary_crossentropy', optimizer='adam', metrics=['acc'])
Es = EarlyStopping(
    monitor='val_acc',
    patience=20,
    mode='max',
    restore_best_weights=True
)
hist = model.fit(x_train,
                 y_train,
                epochs=50,
                validation_split=0.25,
                )

loss = hist.history['loss']
val_loss = hist.history['val_loss']
acc = hist.history['acc']
val_acc = hist.history['val_acc']

print('loss:', loss[-1])
print('val_loss:', val_loss[-1])
print('acc:', acc[-1])
print('val_acc:', val_acc[-1])

#1. 그림그려
#2. 튜닝 0.95이상
from matplotlib import pyplot as plt
plt.subplot(1,2,1)
plt.plot(range(len(hist.history['loss'])),hist.history['loss'],label='loss')
plt.plot(range(len(hist.history['val_loss'])),hist.history['val_loss'],label='val_loss')
plt.legend()
plt.subplot(1,2,2)
plt.plot(range(len(hist.history['acc'])),hist.history['acc'],label='acc')
plt.plot(range(len(hist.history['val_acc'])),hist.history['val_acc'],label='val_acc')
plt.legend()
plt.show()


import pandas as pd
from sklearn.preprocessing import StandardScaler
from imblearn.under_sampling import RandomUnderSampler
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score, roc_auc_score, roc_curve, auc
import matplotlib.pyplot as plt
import pickle

data = pd.read_csv('creditcard.csv')
data.dropna(inplace=True)

scaler = StandardScaler()

for i in range(28):
    data[f'V{i+1}'] = scaler.fit_transform(data[f'V{i+1}'].values.reshape(-1, 1))

X = data.drop('Class', axis=1)
y = data['Class']

undersampler = RandomUnderSampler(sampling_strategy='auto', random_state=42)
X_resampled, y_resampled = undersampler.fit_resample(X, y)

smote = SMOTE(sampling_strategy='auto', random_state=42)
X_resampled, y_resampled = smote.fit_resample(X_resampled, y_resampled)

# Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.2, random_state=42)

# Create an SVM classifier
svm_classifier = SVC(kernel='linear', C=1.0, random_state=42)

# Train the SVM on the training data
svm_classifier.fit(X_train, y_train)

# Make predictions on the test data
y_pred = svm_classifier.predict(X_test)
filename = 'model.sav'
pickle.dump(svm_classifier, open(filename, 'wb'))

# Calculate accuracy on the test set
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy on the test set: {accuracy}")
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
fpr, tpr, thresholds = roc_curve(y_test, svm_classifier.decision_function(X_test))
roc_auc = auc(fpr, tpr)

# Print the metrics
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1-Score: {f1:.2f}")
print(f"AUC: {roc_auc:.2f}")

# Plot the ROC curve
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()


# Name: Ofir Cohen
# ID: 312255847

###################
##### IMPORTS #####
###################
from evaluator import Evaluator
from trainer import Trainer
from dataset import *
from torch.utils.data import DataLoader, WeightedRandomSampler
from datetime import datetime
from sklearn.metrics import confusion_matrix, f1_score
import torch
import torch.nn as nn
import os
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme()

#######################
###### FUNCTIONS ######
#######################
def init():
    # Create dir for results
    output_dir = create_dirs_if_needed()

    # Create params dict for learning process
    parameters = {
        'train_dataset_path': './data/train.pickle',
        'dev_dataset_path': './output/fixed_dev.pickle',
        'num_classes': label_names().__len__(),
        'batch_size': 15,
        'lr': 1e-2,
        'betas': (0.9, 0.999),
        'epochs': 30,
        'criterion': nn.CrossEntropyLoss(),
        'adversarial_epsilons': [0, .05, .1, .15, .2, .25, .3],
        'epsilon': 0.15,
        'lrs': [1e-4, 1e-2, 1, 1e+2, 1e+4],
        'path': output_dir,
        'path_lrs': os.path.join(output_dir, "lrs/"),
        'path_conf_matrix': os.path.join(output_dir, "confusion_matrix/"),
        'path_plots_nn': os.path.join(output_dir, "plots/training_nn/"),
        'path_plots_adversarial': os.path.join(output_dir, "plots/adversarial/"),
        'pretrained_path': './data/pre_trained.ckpt',
        'fixed_dataset': './output/fixed_train_dataset.pickle',
        'fixed_dataset_dev': './output/fixed_dev_dataset.pickle'
    }

    # Create objects that holds datasets
    torch_train_dataset = get_dataset_as_torch_dataset(path=parameters['train_dataset_path'])
    torch_dev_dataset = get_dataset_as_torch_dataset(path=parameters['dev_dataset_path'])
    parameters['train_size'] = torch_train_dataset.__len__()
    parameters['dev_size'] = torch_dev_dataset.__len__()

    return torch_train_dataset, torch_dev_dataset, parameters
# End function

def evaluate(model, data_loader, parameters, counters_dev, name, create_conf_matrix):
    # Evaluate the given model on dev dataset
    print("Start Evaluate Model")
    evaluator = Evaluator(model, data_loader, parameters['criterion'],
                          counters_dev, parameters['num_classes'], name)
    if create_conf_matrix:
        evaluator.__evaluate__()
        conf_mat = confusion_matrix(evaluator.labels.numpy(), evaluator.predicted.numpy())
        f_score = f1_score(evaluator.labels.numpy(), evaluator.predicted.numpy(), average='macro')
        labels_names = label_names().values()
        plot_confusion_matrix_sns(conf_mat, "{} confusion matrix".format(evaluator.name),
                                  labels_names, parameters['path_conf_matrix'])
        print("Model {}. F1-Score: {}".format(evaluator.name, f_score))
    else:
        evaluator.__get_mislabeled__()

    print("End Evaluate Model")
    print("==============================================================================")
    return evaluator
# End function

def train(model, data_loader, parameters, name, save_best_ckpt):
    # Train the given model and evaluate it
    print("Start Train Model")
    trainer = Trainer(model, data_loader, parameters['criterion'], parameters['lr'], parameters['betas'],
                      parameters['epochs'], parameters['batch_size'], parameters['num_classes'],
                      parameters['epsilon'], name, parameters['path'])
    trainer.__train__(save_best_ckpt)
    if save_best_ckpt:
        # if len(trainer.model_states) > 0:
        #     trainer.model_states = sorted(trainer.model_states, key=lambda x: x['loss'])
        #     torch.save({'model_state_dict': trainer.model_states[0]['state_dict']}, trainer.ckpt)
        # else:
        trainer.model.save(trainer.ckpt)

    plot(trainer.losses, "{} Loss".format(trainer.name), "loss", "epoch", parameters['path_plots_nn'])
    plot(trainer.accuracies, "{} Accuracy".format(trainer.name), "accuracy", "epoch", parameters['path_plots_nn'])
    print("End Train Model")
    print("==============================================================================")

    return trainer
# End function

def inspect_dataset(train_dataset, dev_dataset):
    """
    :param train_dataset:
    :param dev_dataset:
    :return counters_train, counters_dev:
    """
    print("Start Inspecting Dataset")
    train_size = train_dataset.__len__()
    dev_size = dev_dataset.__len__()
    print("Number of train images: {}. Number of dev images: {}".format(train_size, dev_size))
    counters_train = {}
    counters_dev = {}
    for value in label_names().values():
        counters_train[value] = 0
        counters_dev[value] = 0

    for idx in range(train_dataset.__len__()):
        item = train_dataset.__getitem__(idx)
        label = label_names()[item[1]]
        counters_train[label] = counters_train[label] + 1

    for idx in range(dev_dataset.__len__()):
        item = dev_dataset.__getitem__(idx)
        label = label_names()[item[1]]
        counters_dev[label] = counters_dev[label] + 1

    print("Train dataset:")
    train_str = ""
    for cls in counters_train:
        print("Class {}. size: {}. Percentage: {}".format(cls, counters_train[cls], (counters_train[cls] / train_size)))

    print("Dev dataset:")
    for cls in counters_dev:
        print("Class {}. size: {}. Percentage: {}".format(cls, counters_dev[cls], (counters_dev[cls] / dev_size)))

    print("End Inspecting Dataset")
    print("==============================================================================")
    return counters_train, counters_dev
# End function

def create_data_loader(dataset, counters, parameters, init_sampler):
    labels = [label for _, label in dataset.the_list]
    class_weights = [dataset.__len__() / counters[label] for label in label_names().values()]
    weights = [class_weights[labels[i]] for i in range(dataset.__len__())]
    if init_sampler:
        sampler = WeightedRandomSampler(weights=weights, num_samples=dataset.__len__())
        data_loader = DataLoader(dataset=dataset, batch_size=parameters['batch_size'], sampler=sampler)
    else:
        data_loader = DataLoader(dataset=dataset, batch_size=parameters['batch_size'])
    return data_loader
# End function

def plot(values, title, y_label, x_label, path):
    plt.plot(values)
    plt.title(title)
    plt.ylabel(y_label)
    plt.xlabel(x_label)
    plt.legend(['val'], loc='upper left')
    fig_name = '{}/{}.png'.format(path, title.lower().replace(' ', '_'))
    plt.savefig(fig_name)
    plt.clf()
# End function

def plot_confusion_matrix_sns(values, title, classes, path):
    ax = sns.heatmap(values, xticklabels=classes, yticklabels=classes, annot=True,
                     cmap='coolwarm', linewidths=0.5, fmt='d')
    ax.set_ylim(3.0, 0)
    plt.title(title)
    fig_name = '{}/{}.png'.format(path, title.lower().replace(' ', '_'))
    plt.savefig(fig_name)
    plt.clf()
# End function

def create_dirs_if_needed():
    output_dir = './output/model_{}/'.format(current_time())
    if os.path.exists('./output') is not True:
        os.mkdir('./output/')
    if os.path.exists(output_dir) is not True:
        os.mkdir(output_dir)
        os.mkdir(output_dir + "lrs/")
        os.mkdir(output_dir + "confusion_matrix/")
        os.mkdir(output_dir + "plots/")
        os.mkdir(output_dir + "plots/training_nn/")
        os.mkdir(output_dir + "plots/adversarial/")

    return output_dir
# End function

def fix_dataset(evaluator, dataset, path):
    mislabeled = sorted(evaluator.mislabeled, key=lambda x: x['loss'], reverse=True)
    fixed_dataset = []
    for i in range(len(mislabeled)):
        inputs, labels = dataset.__getitem__(mislabeled[i]['index'])
        actual = mislabeled[i]['labels']
        predicted = mislabeled[i]['predicted']
        if label_names()[actual] == 'car' and label_names()[predicted] == 'cat':
            labels = predicted
            fixed_dataset.append((inputs, labels))
        else:
            fixed_dataset.append((inputs, labels))

    with open(path, 'wb') as f:
        pickle.dump(fixed_dataset, f, protocol=pickle.HIGHEST_PROTOCOL)
# End function

def current_time():
    return datetime.now().strftime('%Y_%m_%d-%H_%M_%S')
# End function


from gnuradio import gr, gru
from gnuradio import usrp
from gnuradio import eng_notation
from gnuradio.eng_option import eng_option
from gnuradio.wxgui import stdgui, fftsink, waterfallsink, scopesink, form, slider
from optparse import OptionParser
import wx
import sys

import time
from gnuradio import usrp_multi

def pick_subdevice(u):
    """
    The user didn't specify a subdevice on the command line.
    If there's a daughterboard on A, select A.
    If there's a daughterboard on B, select B.
    Otherwise, select A.
    """
    if u.db(0, 0).dbid() >= 0:       # dbid is < 0 if there's no d'board or a problem
        return (0, 0)
    if u.db(0, 0).dbid() >= 0:
        return (1, 0)
    return (0, 0)

class app_flow_graph(stdgui.gui_flow_graph):
    def __init__(self, frame, panel, vbox, argv):
        stdgui.gui_flow_graph.__init__(self)

        self.frame = frame
        self.panel = panel

        parser = OptionParser(option_class=eng_option)
        parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
                          help="select USRP Rx side A or B (default=first one with a daughterboard)")
        parser.add_option("-d", "--decim", type="int", default=128,
                          help="set fgpa decimation rate to DECIM [default=%default]")
        parser.add_option("-f", "--freq", type="eng_float", default=None,
                          help="set frequency to FREQ", metavar="FREQ")
        parser.add_option("-g", "--gain", type="eng_float", default=None,
                          help="set gain in dB (default is midpoint)")
        #align interval is default not yet enabled in gr.align_on_samplenumbers_ss
        #parser.add_option("-a", "--align_interval", type="int", default=-1,
        #                  help="Align master and slave every so much samples.")

        # width 8 does not work yet with multi_usrp because it interferes with the 32 bit samplecounter
        #parser.add_option("-8", "--width-8", action="store_true", default=False,
        #                  help="Enable 8-bit samples across USB")
        parser.add_option("-m", "--mux", type="intx", default=None,
                          help="set fpga FR_RX_MUX register to MUX")
        parser.add_option("-n", "--frame-decim", type="int", default=1,
                          help="set oscope frame decimation factor to n [default=1]")
        parser.add_option("-N", "--nchan", type="int", default=2,
                          help="set nchannels to NCHAN")
        parser.add_option("-q", "--show-q", action="store_true", default=False,
                          help="show the q value of the complex samples")
        parser.add_option("-s", "--show-counters", action="store_true", default=False,
                          help="show the counters")
        parser.add_option("-v", "--v-scale", type="eng_float", default=1000,
                          help="set oscope initial V/div to SCALE [default=%default]")
        parser.add_option("-t", "--t-scale", type="eng_float", default=49e-6,
                          help="set oscope initial s/div to SCALE [default=50us]")
        parser.add_option("-x", "--master-serialno", type="string", default=None,
                          help="Serial_no of the usrp which should be the MASTER (default= select any)")
        (options, args) = parser.parse_args()
        if len(args) != 0:
            parser.print_help()
            sys.exit(1)

        self.show_debug_info = True

        # build the graph

        #self.u = usrp.source_c(which=options.which_usrp,decim_rate=options.decim)
        if (options.mux is None) | (4==options.nchan):
           init_mux=None #use default mux which is 0x10321032
        else:
           init_mux=options.mux

        init_gain=0.0
        init_freq=0.0
        init_align_interval=-1

        self.multi=usrp_multi.multi_source_align( self,   options.master_serialno, options.decim,
                                                 options.nchan, init_gain, init_freq, init_mux, init_align_interval)
        self.um=self.multi.get_master_usrp()
        self.us=self.multi.get_slave_usrp()

        if options.rx_subdev_spec is None:
            options.rx_subdev_spec = pick_subdevice(self.um)
        if (options.mux==None) and (options.nchan!=4):
          mux=usrp.determine_rx_mux_value(self.um, options.rx_subdev_spec)
          mux= (mux<<8 & 0xffffffff) | (mux & 0xff)
          self.um.set_mux(mux)
          self.us.set_mux(mux)

        # width 8 does not work yet with multi_usrp because it interferes with the 32 bit samplecounter
        #if options.width_8:
        #    width = 8
        #    shift = 8
        #    format = self.um.make_format(width, shift)
        #    r = self.um.set_format(format)
        #    r = self.us.set_format(format)

        # determine the daughterboard subdevice of the first channel we're using
        self.subdevm = usrp.selected_subdev(self.um, options.rx_subdev_spec)
        self.subdevs = usrp.selected_subdev(self.us, options.rx_subdev_spec)

        input_rate = self.um.adc_freq() / self.um.decim_rate()

        self.scope = scopesink.scope_sink_f(self, panel, sample_rate=input_rate,
                                            frame_decim=options.frame_decim,
                                            v_scale=options.v_scale,
                                            t_scale=options.t_scale)
        self.sink_count=0
        self.add_to_scope((self.multi.get_master_source_c(),1),options.show_q)
        self.add_to_scope((self.multi.get_slave_source_c(),1),options.show_q)
        if 4==options.nchan:
          self.add_to_scope((self.multi.get_master_source_c(),2),options.show_q)
          self.add_to_scope((self.multi.get_slave_source_c(),2),options.show_q)

        if options.show_counters:
          self.add_to_scope((self.multi.get_master_source_c(),0),options.show_q)
          self.add_to_scope((self.multi.get_slave_source_c(),0),options.show_q)

        self._build_gui(vbox)

        # set initial values

        if options.gain is None:
            # if no gain was specified, use the mid-point in dB
            g = self.subdevm.gain_range()
            options.gain = float(g[0]+g[1])/2

        if options.freq is None:
            # if no freq was specified, use the mid-point
            r = self.subdevm.freq_range()
            options.freq = float(r[0]+r[1])/2

        self.set_gain(options.gain)

        if self.show_debug_info:
            self.myform['decim'].set_value(self.um.decim_rate())
            self.myform['fs@usb'].set_value(self.um.adc_freq() / self.um.decim_rate())
            self.myform['dbname'].set_value(self.subdevm.name())
            self.myform['baseband'].set_value(0)
            self.myform['ddc'].set_value(0)

        if not(self.set_freq(options.freq)):
            self._set_status_msg("Failed to set initial frequency")

        self.multi.print_db_info()
        self.unsynced=True
        frame.Bind(wx.EVT_IDLE, self.onIdle)

    def add_to_scope(self,source_c,show_q):
        c2f= gr.complex_to_float ()
        self.connect(source_c, c2f)
        self.connect((c2f,0), (self.scope,self.sink_count))
        self.sink_count=self.sink_count+1
        if show_q:
          self.connect((c2f,1), (self.scope,self.sink_count))
          self.sink_count=self.sink_count+1

    def _set_status_msg(self, msg):
        self.frame.GetStatusBar().SetStatusText(msg, 0)

    def _build_gui(self, vbox):

        def _form_set_freq(kv):
            return self.set_freq(kv['freq'])

        vbox.Add(self.scope.win, 10, wx.EXPAND)

        # add control area at the bottom
        self.myform = myform = form.form()
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add((5,0), 0, 0)
        myform['freq'] = form.float_field(
            parent=self.panel, sizer=hbox, label="Center freq", weight=1,
            callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))

        hbox.Add((5,0), 0, 0)
        g = self.subdevm.gain_range()
        myform['gain'] = form.slider_field(parent=self.panel, sizer=hbox, label="Gain",
                                           weight=3,
                                           min=int(g[0]), max=int(g[1]),
                                           callback=self.set_gain)

        hbox.Add((5,0), 0, 0)
        buttonSync = form.button_with_callback(parent=self.panel, label='sync',callback=self.sync_usrps)
        hbox.Add(buttonSync,0,wx.EXPAND)

        hbox.Add((5,0), 0, 0)
        vbox.Add(hbox, 0, wx.EXPAND)

        self._build_subpanel(vbox)

    def _build_subpanel(self, vbox_arg):
        # build a secondary information panel (sometimes hidden)

        # FIXME figure out how to have this be a subpanel that is always
        # created, but has its visibility controlled by foo.Show(True/False)

        if not(self.show_debug_info):
            return

        panel = self.panel
        vbox = vbox_arg
        myform = self.myform

        #panel = wx.Panel(self.panel, -1)
        #vbox = wx.BoxSizer(wx.VERTICAL)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add((5,0), 0)
        myform['decim'] = form.static_float_field(
            parent=panel, sizer=hbox, label="Decim")

        hbox.Add((5,0), 1)
        myform['fs@usb'] = form.static_float_field(
            parent=panel, sizer=hbox, label="Fs@USB")

        hbox.Add((5,0), 1)
        myform['dbname'] = form.static_text_field(
            parent=panel, sizer=hbox)

        hbox.Add((5,0), 1)
        myform['baseband'] = form.static_float_field(
            parent=panel, sizer=hbox, label="Analog BB")

        hbox.Add((5,0), 1)
        myform['ddc'] = form.static_float_field(
            parent=panel, sizer=hbox, label="DDC")

        hbox.Add((5,0), 0)
        vbox.Add(hbox, 0, wx.EXPAND)

    def set_freq(self, target_freq, sync=True):
        """
        Set the center frequency we're interested in for all channels,
        on all rx daughterboards on MASTER and SLAVE.

        @param target_freq: frequency in Hz
        @param sync: sync the usrps after setting the freqs (this will clear any phase differences in the DDCS)
        @rypte: bool

        Tuning is a two step process.  First we ask the front-end to
        tune as close to the desired frequency as it can.  Then we use
        the result of that operation and our target_frequency to
        determine the value for the digital down converter.
        """
        result,r1,r2,r3,r4 = self.multi.tune_all_rx(target_freq)
        if sync:
          self.sync_usrps() #sync master and slave and clear any DDC phase differences
        if r1:
            self.myform['freq'].set_value(target_freq)     # update displayed value
            if self.show_debug_info:
                self.myform['baseband'].set_value(r1.baseband_freq)
                self.myform['ddc'].set_value(r1.dxc_freq)
        return result

    def set_freq_chan0(self, target_freq, sync=True):
        """
        Set the center frequency we're interested in for rx chan 0 only on MASTER and SLAVE.

        @param target_freq: frequency in Hz
        @param sync: sync the usrps after setting the freqs (this will clear any phase differences in the DDCS)
        @rypte: bool

        Tuning is a two step process.  First we ask the front-end to
        tune as close to the desired frequency as it can.  Then we use
        the result of that operation and our target_frequency to
        determine the value for the digital down converter.
        """
        rm = usrp.tune(self.um, 0, self.subdevm, target_freq)
        rs = usrp.tune(self.us, 0, self.subdevs, target_freq)
        r=rm
        if sync:
          self.sync_usrps()  #sync master and slave and clear any DDC phase differences
        if r:
            self.myform['freq'].set_value(target_freq)     # update displayed value
            if self.show_debug_info:
                self.myform['baseband'].set_value(r.baseband_freq)
                self.myform['ddc'].set_value(r.dxc_freq)
            return True

        return False

    def set_gain(self, gain):
        self.myform['gain'].set_value(gain)     # update displayed value
        self.multi.set_gain_all_rx(gain)

    def set_gain_chan0(self, gain):
        self.myform['gain'].set_value(gain)     # update displayed value
        self.subdevm.set_gain(gain)
        self.subdevs.set_gain(gain)

    def onIdle(self,evt):
        if self.unsynced:
          time.sleep(0.5)
          self.unsynced=True
          self.multi.sync()
          self.unsynced=False
          #print 'synced'

    def sync_usrps(self):
        self.multi.sync()

def main ():
    app = stdgui.stdapp(app_flow_graph, "MULTI_USRP O'scope", nstatus=1)
    app.MainLoop()

if __name__ == '__main__':
    main ()


# integración con cuadratura de Gauss de orden 1
import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 1-x**2

a=0
b=1

#cuadratura de grado 2
x=np.array([(1/3)**0.5,-(1/3)**0.5])

w=np.array([1,1])

u=(b-a)*x/2+(a+b)/2 #cambio de variable, la integral que da -1a1
I=(b-a)*np.sum(w*f(u))/2 #evaluacion

print(I)

4 Calculo numérico/C Integral con Cuadratura de Gauss.py


class Solution:
    # 有序两数
    def twoSum(self, nums: list, target: int) -> list:
        i, j = 0, len(nums) - 1
        while i < j:
            s = nums[i] + nums[j]
            if s > target: j -= 1
            elif s < target: i += 1
            else: return [nums[i], nums[j]]
        return []
    # 无序两数
    def twoSum2(self, nums: [int], target: int) -> [int]:
        temp_dict = dict()
        for i, e in enumerate(nums):
            if target - e in temp_dict.keys():
                return [i,temp_dict[target-e]]
            else:
                temp_dict[e]=i

数组/和为s的两个数字.py


class Solution:
    def firstUniqChar(self, s: str) -> int:
        dict = defaultdict(list)
        for i, char in enumerate(s):
            dict[char].append(i)
        for i , value in dict.items():
            if len(value) == 1:
                return value.pop()
        return -1