# 프랙탈 [fractal] : 일부 작은 조각이 전체와 비슷한 형태
    # 나무줄기 , 번개 , 나뭇잎
def drawCircle( x , y , r ) :
    global count
    count += 1  # 카운트 1증가
    # 원형 그리기 [ create_oval : 원형 그리기 함수 ]
    canvas.create_oval( x - r, y - r, x + r, y + r, width=5, outline='black')   # 원형
                    # 왼쪽끝x , 위쪽끝y 오른쪽x , 아래끝y
    # canvas.create_text( x , y-r , text=str(count) ,font=('',30) )     # 문자열 출력
    #if r >= radius/2 :  # 만약에 현재 반지름이 전체반지름 반 보다 크면
    if r >= 5 :
        drawCircle(x-r //2 , y , r//2 )
        drawCircle(x+r //2 , y , r// 2) # 위에 있는 재귀가 모두 종료되기 까지 실행 x
# 1. 원형그리기
from tkinter import *   # GUI관련 함수 제공
window = Tk()       # GUI 객체 생성
canvas = Canvas( window , height=1000 , width=1000 , bg='white')        # GUI 배경
# 원형 그리기
count = 0
radius = 400 # 반지름
wSize = 1000 #
    # 함수 호출( x축 , y축 , 반지름 )
drawCircle( wSize//2 , wSize//2 , radius )
canvas.pack()       # 해당 Canvas를 GUI에 추가
window.mainloop()   # GUI 열기

5일차/예제4_재귀활용.py


#题解见notion
class Solution:
    def minDistance(self, word1: str, word2: str) -> int:

        if not word1:
            return len(word2)
        if not word2:
            return len(word1)

        m,n = len(word1), len(word2)
        f = [[0]*(n+1) for i in range(m+1) ]

        for i in range(m+1): f[i][0] = i
        for j in range(n+1): f[0][j] = j

        for i in range(1,m+1):
            for j in range(1,n+1):
                if word1[i-1] == word2[j-1]:
                    f[i][j] = f[i-1][j-1]
                else:
                    f[i][j] = 1 + min(f[i-1][j], f[i][j-1], f[i-1][j-1])
        return f[i][j]

if __name__ == '__main__':
    word1 = "horse"
    word2 = "ros"
    word1,word2 = "a","b"
    print(Solution().minDistance(word1, word2))


class BinarySearchTreeNode:
    left = None
    right = None

    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return f"{self.__class__.__name__}({self.value})"

    def __str__(self):
        return str(self.value)

class BinarySearchTree:
    def __init__(self, root):
        self.root = root

    def __repr__(self):
        return f"{self.__class__.__name__}({self.root.value})"

    def __str__(self):
        return str(self.root.value)

    @staticmethod
    def tree_in_order_traversal(tree):
        if tree.left is not None:
            BinarySearchTree.tree_in_order_traversal(tree.left)

        print(tree.value, end="-")

        if tree.right is not None:
            BinarySearchTree.tree_in_order_traversal(tree.right)

    def in_order_traversal(self):
        BinarySearchTree.tree_in_order_traversal(self.root)

    @staticmethod
    def tree_pre_order_traversal(tree):
        print(tree.value, end="-")

        if tree.left is not None:
            BinarySearchTree.tree_pre_order_traversal(tree.left)

        if tree.right is not None:
            BinarySearchTree.tree_pre_order_traversal(tree.right)

    def pre_order_traversal(self):
        BinarySearchTree.tree_pre_order_traversal(self.root)

    @staticmethod
    def tree_post_order_traversal(tree):
        if tree.left is not None:
            BinarySearchTree.tree_post_order_traversal(tree.left)

        if tree.right is not None:
            BinarySearchTree.tree_post_order_traversal(tree.right)

        print(tree.value, end="-")

    def post_order_traversal(self):
        BinarySearchTree.tree_post_order_traversal(self.root)

    def tree_print(node, identifier, level):
        element = f"{identifier}{node.value}"
        spaces = 0 if level < 2 else len(element) - 1 + level * 2
        print(f"{element:>{spaces}}")
        if node.left:
            BinarySearchTree.tree_print(node.left, "|-L:", level + 1)
        if node.right:
            BinarySearchTree.tree_print(node.right, "|-R:", level + 1)

    def print(self):
        BinarySearchTree.tree_print(self.root, "", 0)

    @staticmethod
    def binary_tree_search(tree, direction, value):
        sub_tree = getattr(tree, direction)
        if sub_tree.value == value:
            return True, tree, direction
        if sub_tree.value < value and sub_tree.right is not None:
            return BinarySearchTree.binary_tree_search(
                sub_tree,
                "right",
                value,
            )
        elif sub_tree.value > value and sub_tree.left is not None:
            return BinarySearchTree.binary_tree_search(sub_tree, "left", value)

        return False, None, None

    def search(self, value):
        return BinarySearchTree.binary_tree_search(self, "root", value)

    @staticmethod
    def tree_add(tree, direction, value):
        node = getattr(tree, direction)
        if not node:
            setattr(tree, direction, BinarySearchTreeNode(value))
            return

        if node.value < value:
            return BinarySearchTree.tree_add(node, "right", value)
        else:
            return BinarySearchTree.tree_add(node, "left", value)

    def add(self, value):
        return BinarySearchTree.tree_add(self, "root", value)

    @staticmethod
    def tree_max_value(parent, direction):
        node = getattr(parent, direction)
        while node.right:
            parent = node
            node = node.right
            direction = "right"

        return parent, direction, node

    def max_value(self):
        return BinarySearchTree.tree_max_value(self, "root")

    def remove(self, value):
        found, parent, direction = self.search(value)

        if not found:
            return False

        current_node = getattr(parent, direction)
        if not current_node.left:
            setattr(parent, direction, current_node.right)
        elif not current_node.right:
            setattr(parent, direction, current_node.left)
        else:
            parent, direction, successor = BinarySearchTree.tree_max_value(
                current_node, "left"
            )
            current_node.value = successor.value
            setattr(parent, direction, successor.left)
        return True

    @staticmethod
    def rotate_left(parent, direction, node_left, node_right):
        node_left.right = node_right.left
        node_right.left = node_left
        setattr(parent, direction, node_right)

    @staticmethod
    def rotate_right(parent, direction, node_left, node_right):
        node_right.left = node_left.right
        node_left.right = node_right
        setattr(parent, direction, node_left)


from typing import Optional, TYPE_CHECKING

from django.db import models

from django.contrib.auth.models import Group, User

from rest_framework import serializers
from rest_framework.exceptions import NotFound

from processes.exception import UnprocessableEntity

from .uuid_model import UuidModel

if TYPE_CHECKING:
    from .run_environment import RunEnvironment

class NamedWithUuidModel(UuidModel):
    class Meta:
        abstract = True

    name = models.CharField(max_length=200)
    description = models.CharField(max_length=5000, blank=True)
    created_by_user = models.ForeignKey(User, on_delete=models.SET_NULL,
            null=True, editable=False)
    created_by_group = models.ForeignKey(Group, on_delete=models.CASCADE,
            editable=False)

    @classmethod
    def find_by_uuid_or_name(cls, obj_dict,
            required_group: Optional[Group] = None,
            required_run_environment: 'Optional[RunEnvironment]' = None,
            check_conflict: bool = True,
            allowed_run_environment: 'Optional[RunEnvironment]' = None,
            allow_any_run_environment: Optional[bool] = None):
        uuid = obj_dict.get('uuid')
        name = obj_dict.get('name')

        if uuid is not None:
            entity = cls.objects.get(uuid=uuid)

            if check_conflict and (name is not None) and (entity.name != name):
                raise UnprocessableEntity(
                        f"{cls.__name__} {uuid} is named '{entity.name}', not '{name}'")
        else:
            if name is None:
                raise serializers.ValidationError('Neither uuid or name found in request')

            entity = cls.objects.get(name=name, created_by_group=required_group)

        if required_group and (entity.created_by_group != required_group):
            raise NotFound()

        if required_run_environment and (
                entity.run_environment != required_run_environment):
            raise NotFound()

        # So that allowed_run_environment can be omitted if
        # required_run_environment is set
        allowed_run_environment = allowed_run_environment or \
                required_run_environment

        if allow_any_run_environment is None:
            allow_any_run_environment = (allowed_run_environment is None)

        if (not allow_any_run_environment) and \
                hasattr(entity, 'run_environment') and \
                entity.run_environment:

            if allowed_run_environment:
                if entity.run_environment != allowed_run_environment:
                    raise NotFound()
            else:
                raise NotFound()

        return entity

    def __str__(self) -> str:
        return (self.name or 'Unnamed') + ' / ' + str(self.uuid)


# 동빈나 그리디 모험가 길드
# 공포도가 x 인 모험가는 x명 이상의 그룹에 들어가야한다. -> 최대 몇 그룹
# 1. 정렬 후 끊기
# 입력 예제
# 5
# 2 3 2 2 1 답 2

n=int(input())

arr=list(map(int,input().split()))
arr.sort()

team=0
count=0
for i in arr:
    count+=1
    if count>=i:
        team+=1
        count=0
print(team)


from setuptools import setup

with open('requirements.txt') as fp:
    install_requires = fp.read()

setup(
    name='mapped_config',
    packages=['mapped_config'],
    version='2.36',
    description='Mapped config loader for python for secure, easy and modular configuration management',
    author='Alvaro Garcia Gomez',
    author_email='maxpowel@gmail.com',
    url='https://github.com/maxpowel/mapped_config',
    download_url='https://github.com/maxpowel/mapped_config/archive/master.zip',
    keywords=['config', 'configuration', 'yml', 'json'],
    classifiers=['Topic :: Adaptive Technologies', 'Topic :: Software Development', 'Topic :: System',
                 'Topic :: Utilities'],
    install_requires=install_requires
)


import torch
import torch.nn as nn
import numpy as np
from dataset_windows import SatelliteSet, flatten_batch_data, standardize_data

from torchvision.models.segmentation.deeplabv3 import DeepLabHead
from torchvision import models
import torch.nn.functional as F
from tqdm import tqdm
import h5py
from PIL import Image
import matplotlib.pyplot as plt

# def createDeepLabv3(outputchannels=1):
#     """DeepLabv3 class with custom head
#     Args:
#         outputchannels (int, optional): The number of output channels in your dataset masks. Defaults to 1.
#     Returns:
#         model: Returns the DeepLabv3 model with the ResNet101 backbone.
#     """
#     model = models.segmentation.deeplabv3_resnet101(pretrained=True, progress=True)
#     model.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)
#     model.classifier = DeepLabHead(2048, outputchannels)
#     # Set the model in training mode
#     model.eval()
#     return model
#
#
# class MyModel(nn.Module):
#     def __init__(self):
#         super(MyModel, self).__init__()
#
#         image_modules = list(createDeepLabv3(32).children())[:-1]
#         self.layer1 = nn.Conv2d(4, 3, kernel_size=3, stride=1, padding=1)
#         self.model = nn.Sequential(*image_modules)
#
#     def forward(self, tensor):
#         a = self.layer1(tensor)
#         # a = torch.tensor(a, dtype=torch.float64)
#         a = self.model(a.double())
#
#         return a

class ResNet_101(nn.Module):
    """Load pretrained model resnet101"""

    def __init__(self, in_channels=4, conv1_out=64):
        super(ResNet_101, self).__init__()
        backbone = models.resnet101(pretrained=True)

        # Change the input channels from 3 to 4 and set the same weight for NIR as R
        pretrained_conv1 = backbone.conv1.weight.clone()
        self.conv1 = nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.conv1.weight[:, :3] = pretrained_conv1
        self.conv1.weight[:, -1] = pretrained_conv1[:, 0]

        self.bn1 = backbone.bn1
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = backbone.maxpool
        self.layer1 = backbone.layer1
        self.layer2 = backbone.layer2
        self.layer3 = backbone.layer3

    def forward(self, x):
        x = self.relu(self.bn1(self.conv1(x)))
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)

        return x

class ASSP(nn.Module):

    def __init__(self, in_channels, out_channels=256):
        super(ASSP, self).__init__()
        self.relu = nn.ReLU(inplace=True)
        self.conv1 = nn.Conv2d(in_channels, out_channels, 1, padding=0, dilation=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.conv2 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=6,
                               dilation=6, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channels)
        self.conv3 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=12,
                               dilation=12, bias=False)
        self.bn3 = nn.BatchNorm2d(out_channels)
        self.conv4 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=18,
                               dilation=18, bias=False)
        self.bn4 = nn.BatchNorm2d(out_channels)
        self.conv5 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0,
                               dilation=1, bias=False)
        self.bn5 = nn.BatchNorm2d(out_channels)
        self.convf = nn.Conv2d(in_channels=out_channels * 5, out_channels=out_channels, kernel_size=1, stride=1,
                               padding=0, dilation=1, bias=False)
        self.bnf = nn.BatchNorm2d(out_channels)
        self.adapool = nn.AdaptiveAvgPool2d(1)

    def forward(self, x):
        x1 = self.conv1(x)
        x1 = self.bn1(x1)
        x1 = self.relu(x1)
        x2 = self.conv2(x)
        x2 = self.bn2(x2)
        x2 = self.relu(x2)
        x3 = self.conv3(x)
        x3 = self.bn3(x3)
        x3 = self.relu(x3)
        x4 = self.conv4(x)
        x4 = self.bn4(x4)
        x4 = self.relu(x4)
        x5 = self.adapool(x)
        x5 = self.conv5(x5)
        x5 = self.bn5(x5)
        x5 = self.relu(x5)
        x5 = F.interpolate(x5, size=tuple(x4.shape[-2:]), mode='bilinear', align_corners=True)

        x = torch.cat((x1, x2, x3, x4, x5), dim=1)  # channels first
        x = self.convf(x)
        x = self.bnf(x)
        x = self.relu(x)
        return x

class Deeplabv3Resnet101(nn.Module):
    """Consturct Deeplabv3_Resnet101"""

    def __init__(self, nc=2, input_channel=4):
        super(Deeplabv3Resnet101, self).__init__()
        self.nc = nc
        self.backbone = ResNet_101(input_channel)
        self.assp = ASSP(in_channels=1024)
        self.out1 = nn.Sequential(nn.Conv2d(in_channels=256, out_channels=256, kernel_size=1, stride=1), nn.ReLU())
        self.dropout1 = nn.Dropout(0.5)
        self.up4 = nn.Upsample(scale_factor=4)
        self.up2 = nn.Upsample(scale_factor=2)

        self.conv1x1 = nn.Sequential(nn.Conv2d(1024, 256, 1, bias=False), nn.ReLU())
        self.conv3x3 = nn.Sequential(nn.Conv2d(512, self.nc, 1), nn.ReLU())
        self.dec_conv = nn.Sequential(nn.Conv2d(256, 256, 3, padding=1), nn.ReLU())

    def forward(self, x):
        x = self.backbone(x)
        out1 = self.assp(x)
        out1 = self.out1(out1)
        out1 = self.dropout1(out1)
        out1 = self.up4(out1)
        # print(out1.shape)

        dec = self.conv1x1(x)
        dec = self.dec_conv(dec)
        dec = self.up4(dec)
        concat = torch.cat((out1, dec), dim=1)
        out = self.conv3x3(concat)
        out = self.up4(out)
        return out

if __name__ == "__main__":
    # input_tensor = torch.rand(4, 4, 128, 128)  # batch_size,input_channel,input_h,input_w
    # print(input_tensor)
    # model = Deeplabv3Resnet101(nc=32, input_channel=4)
    # out = model(input_tensor)
    # print(out.shape)

    # Parameters for loading data
    TRAIN_PATH = '../data/data_test_rgbReduced_delBlankRotations_Standardized.hdf5'
    IMAGE_NUM = 2
    WINDOWSIZE = 224
    # Sampling data
    SAMPLE = True
    SAMPLESIZE = 0.5
    # Batch size for train dataloader
    BATCH_SIZE = 128
    # Output features
    OUTPUT_FEATURES = 32

    # Set seed, so that results can be reproduced
    np.random.seed(2021)

    # Loading data
    print("Loading data...")
    train_dset = SatelliteSet(TRAIN_PATH, IMAGE_NUM, WINDOWSIZE)
    print(f"Original dataset contains {len(train_dset)} samples")

    # Sampling
    if SAMPLE:
        print("Sampling...")
        #sampling = np.random.randint(len(train_dset), size=round(len(train_dset) * SAMPLESIZE)
        length = round(len(train_dset) * SAMPLESIZE)
        sampling = list(range(1*length, 2*length))
        train_dset = torch.utils.data.Subset(train_dset, sampling)
    print(f"TRAIN dataset contains {len(train_dset)} windows")

    # Create dataloader
    train_loader = torch.utils.data.DataLoader(train_dset,
                                               batch_size=BATCH_SIZE,
                                               num_workers=0,
                                               shuffle=False)

    # Initialize models
    print("Loading model...")
    model = Deeplabv3Resnet101(nc=OUTPUT_FEATURES, input_channel=4)

    with h5py.File("P:\pf\pfstud\II_jingyli\data_test_features_c32_pic2.hdf5", "a") as f:
        x_features = np.zeros((len(train_dset), OUTPUT_FEATURES, WINDOWSIZE, WINDOWSIZE))
        y_gt = np.zeros((len(train_dset), WINDOWSIZE, WINDOWSIZE))
        i = 0
        for x, y in tqdm(train_loader):
            print(x.shape)
            print(y.shape)
            x_out = model(x)
            del x
            print(x_out.shape)
            x_out = x_out.detach().numpy()
            x_features[i*BATCH_SIZE:(i+1)*BATCH_SIZE] = x_out
            del x_out
            y = y.detach().numpy()
            y_gt[i*BATCH_SIZE:(i+1)*BATCH_SIZE] = y
            del y
            i += 1

        _ = f.create_dataset("Features", data=x_features)
        del _
        _ = f.create_dataset("GT", data=y_gt)
        del _

    # img = Image.open('../1.jpg')
    #
    # import torchvision.transforms as T
    #
    # trf = T.Compose([T.Resize(256),
    #                  T.CenterCrop(224),
    #                  T.ToTensor()])
    # inp = trf(img).unsqueeze(0)
    # print(inp.shape)
    #
    # model = createDeepLabv3(24)
    # out = model(inp)["out"]
    # print(out.shape)


import numpy as np
from DigitalFilter import Filter
import cv2
from Utilities import show_image

class Sharpening(Filter):
    __kernel = None

    def __init__(self):
        size = 3
        self.set_height(size)
        self.set_width(size)
        self.make_kernel()

    def make_kernel(self):
        self.__kernel = np.ones((self.get_height(), self.get_width()))
        row_of_middle_pixel_inside_kernel = int(self.get_height() / 2)
        column_of_middle_pixel_inside_kernel = int(self.get_width() / 2)

        self.__kernel[row_of_middle_pixel_inside_kernel, column_of_middle_pixel_inside_kernel] = -8

    def make_convolution(self, image, output):
        laplacian_mask = self.apply_derivative(image)
        output = image - laplacian_mask
        output = self.delete_outliers(output)

        return output.astype(np.uint8)

    def apply_derivative(self, image):
        laplacian_mask = np.zeros(image.shape)
        starting_row = int(self.get_height() / 2)
        starting_column = int(self.get_width() / 2)
        ending_row = image.shape[0] - self.get_down_side_frame()
        ending_column = image.shape[1] - self.get_right_side_frame()
        for i in range(starting_row, ending_row):
            for j in range(starting_column, ending_column):
                convolution = 0
                for q in range(i - self.get_up_side_frame(), i - self.get_up_side_frame() + self.get_height()):
                    for r in range(j - self.get_left_side_frame(), j - self.get_left_side_frame() + self.get_width()):
                        row_kernel = q - i + self.get_up_side_frame()
                        column_kernel = r - j + self.get_left_side_frame()
                        convolution = convolution + (self.__kernel[row_kernel, column_kernel] * image[q, r])
                laplacian_mask[i, j] = convolution
        return laplacian_mask

    def image_is_in_gray_scale(self, image):
        return len(image.shape) == 2

    def delete_outliers(self, output):
        for i in range(output.shape[0]):
            for j in range(output.shape[1]):
                if not self.image_is_in_gray_scale(output):
                    if output[i][j][0] < 0:
                        output[i][j][0] = 0
                    elif output[i][j][0] > 254:
                        output[i][j][0] = 254

                    if output[i][j][1] < 0:
                        output[i][j][1] = 0
                    elif output[i][j][1] > 254:
                        output[i][j][1] = 254

                    if output[i][j][2] < 0:
                        output[i][j][2] = 0
                    elif output[i][j][2] > 254:
                        output[i][j][2] = 254
                else:
                    if output[i][j] < 0:
                        output[i][j] = 0
                    elif output[i][j] > 254:
                        output[i][j] = 254
        return output


from env import *
# import os
import re
import sys
import argparse
from os.path import join
from tools import *
import logging
from core.api import set_api_logger
from core.chat import ChatBot, Turn, set_chat_logger
import gradio as gr
from prompts.dialogue import *

args: argparse.Namespace = None
bot: ChatBot = None

# Global Hyper Parameters
no_long_term_memory = False
naive_memory = False
embed_summary = False

translation_map = {}

def summarize_embed_one_turn(bot: ChatBot, dialogue_text, dialogue_text_with_index):
    global embed_summary
    lang2template = {
        LANG_EN: en_turn_summarization_prompt,
        LANG_ZH: zh_turn_summarization_prompt
    }

    tmp = choose_language_template(lang2template, dialogue_text)
    input_text = tmp.format(input=dialogue_text)
    logger.info(f'turn summarization input_text: \n\n{input_text}')
    # 如果原文很短，保留原文即可
    summarization = dialogue_text_with_index
    if get_token_count_davinci(input_text) > 300:
        logger.info(f'current turn text token count > 300, summarize !\n\n')
        summarization = bot.ask(input_text)
        logger.info(f'Summarization is:\n\n{summarization}\n\n')
    else:
        logger.info(f'Raw content is short, keep raw content as summarization:\n\n{summarization}\n\n')
    if embed_summary:
        embedding = bot.vectorize(summarization)
    else:
        embedding = bot.vectorize(dialogue_text_with_index)
    return summarization, embedding

def get_concat_input(user_str, pre_sre, hist_str=None):
    lang2template = {
        LANG_EN: en_no_history_agent_prompt,
        LANG_ZH: zh_no_history_agent_prompt
    }

    templates_no_hist = choose_language_template(lang2template, user_str)

    lang2template = {
        LANG_EN: en_history_agent_prompt,
        LANG_ZH: zh_history_agent_prompt
    }

    templates_hist = choose_language_template(lang2template, user_str)

    if hist_str:
        input_text = templates_hist.format(history_turn_text=hist_str, pre_turn_text=pre_sre, input=user_str)
    else:
        input_text = templates_no_hist.format(pre_turn_text=pre_sre, input=user_str)
    return input_text

def check_key_file(key_file):
    if not os.path.exists(key_file):
        print(f'[{key_file}] not found! Please put your apikey in the txt file.')
        sys.exit(-1)

def get_first_prompt(user_text, model_name):
    if model_name in [ENGINE_TURBO]:
        return user_text
    else:
        lang2template = {
            LANG_EN: en_start_prompt,
            LANG_ZH: zh_start_prompt
        }

        tmp = choose_language_template(lang2template, user_text)
        concat_input = tmp.format(input=user_text)
        return concat_input

def check_string_format(input_str):
    input_str = input_str.strip()
    if 'filename' not in input_str or 'dial_id' not in input_str:
        return False
    filename = dial_id = False
    for item in input_str.split('; '):
        if 'filename' in item:
            if item.split(': ')[1]:
                filename = True
        elif 'dial_id' in item:
            if item.split(': ')[1]:
                dial_id = True
    return filename and dial_id

def extract_values(input_str):
    if 'filename' not in input_str or 'dial_id' not in input_str:
        return False
    filename, dial_id = None, None
    for item in input_str.split('; '):
        if 'filename' in item:
            filename = item.split(': ')[1]
        elif 'dial_id' in item:
            dial_id = item.split(': ')[1]
    return filename, dial_id

def replace_code(s: str) -> str:
    start_index = s.find("```")
    end_index = s.rfind("```")
    if start_index != -1 and end_index != -1:
        end_index = min(end_index+3, len(s)-1)
        s = s[:start_index] + "Ommit Code Here ..." + s[end_index:]
    return s

def load_history_dialogue(filename, dial_id):
    data = load_json_file(filename)
    for item in data:
        if dial_id == item['id']:
            return item['dialogue']
    raise ValueError('Invalid dial_id: {dial_id}')

def initialize_bot_and_dial(dialogues, dial_id):
    history = []
    turn_idx = 0
    history.append(('请输入待标注的对话ID', dial_id))
    total = len(dialogues) // 2
    for i in range(0, len(dialogues), 2):
        turn_idx += 1
        if i+1 < len(dialogues):
            user_text = dialogues[i]
            user_text_display = user_text
            if translation_map and translation_map.get(user_text, None):
                zh_text = translation_map.get(user_text)
                zh_text = replace_code(zh_text)
                user_text_display += f"\n\n{zh_text}"
                user_text_display = user_text_display.replace('__', 'prefix_')

            # user_text = dialogues[i].replace('\\n', '\n')

            assistant_text = dialogues[i+1]
            assistant_text_display = assistant_text
            if translation_map and translation_map.get(assistant_text, None):
                zh_text = translation_map.get(assistant_text)
                zh_text = replace_code(zh_text)
                assistant_text_display += f"\n\n{zh_text}"
                assistant_text_display = assistant_text_display.replace('__', 'prefix_')

            # assistant_text = dialogues[i+1].replace('\\n', '\n')

            cur = (replace_newline(user_text_display), replace_newline(assistant_text_display))
            # cur = (user_text_display, assistant_text_display)
            history.append(cur)

            cur_text_without_index = '用户：{}\n\n助手：{}'.format(user_text, assistant_text)
            cur_text_with_index = '[第{}轮]\n\n用户：{}\n\n助手：{}'.format(turn_idx, user_text, assistant_text)

            if detect_language(user_text+assistant_text) == LANG_EN:
                cur_text_without_index = 'User: {}\n\nAssistant: {}'.format(user_text, assistant_text)
                cur_text_with_index = '[Turn {}]\n\nUser: {}\n\nAssistant: {}'.format(turn_idx, user_text, assistant_text)

            print(f"loading progress : {turn_idx} / {total}, {cur_text_with_index[:200]} ...\n")

            summary, embedding = summarize_embed_one_turn(bot, cur_text_without_index, cur_text_with_index)

            cur_turn = Turn(user_input=user_text, system_response=assistant_text, user_sys_text=cur_text_with_index, summ=summary, embedding=embedding)

            bot.add_turn_history(turn = cur_turn)

    return history

def my_chatbot(user_input, history):
    global no_long_term_memory
    global naive_memory
    history = history or []

    user_input = user_input.strip()

    my_history = list(sum(history, ()))

    COMMAND_RETURN = '命令已成功执行！'

    if user_input in ['清空', 'reset']:
        # history.append((user_input, COMMAND_RETURN))
        history = []
        bot.clear_history()
        logger.info(f'[User Command]: {user_input} {COMMAND_RETURN}')
        return history, history
    elif user_input in ['导出', 'export']:
        # history.append((user_input, COMMAND_RETURN))
        bot.export_history()
        logger.info(f'[User Command]: {user_input} {COMMAND_RETURN}')
        return history, history
    elif user_input in ['回退', '回滚', 'roll back']:
        history.pop()
        bot.roll_back()
        logger.info(f'[User Command]: {user_input} {COMMAND_RETURN}')
        return history, history
    elif check_string_format(user_input):
        filename, dial_id = extract_values(user_input)
        dialogues = load_history_dialogue(filename, dial_id)
        history = initialize_bot_and_dial(dialogues, dial_id)
        return history, history

    len_hist = len(bot.history)
    cur_turn_index = len_hist + 1
    if len_hist == 0:
        concat_input = get_first_prompt(user_input, args.model_name)
    else:
        retrieve = None
        if no_long_term_memory:
            pass
        elif cur_turn_index > 2:
            if naive_memory:
                retrieve = bot.get_related_turn(user_input, k=args.similar_top_k, naive=True)
            else:
                retrieve = bot.get_related_turn(user_input, k=args.similar_top_k)
        else:
            pass

        logger.info(f"no_long_term_memory: {no_long_term_memory}")
        logger.info(f"retrieve: \n{retrieve}\n")

        concat_input = get_concat_input(user_input, bot.get_turn_for_previous(), hist_str=retrieve)

    logger.info(f'\n--------------\n[第{cur_turn_index}轮] concat_input:\n\n{concat_input}\n--------------\n')

    try:
        rsp: str = bot.ask(concat_input)
    except Exception as e:
        logger.error(f'ERROR: \n\n{e}')
        rsp = 'System error, please check log file for details.'
        history.append((user_input, rsp))
        return history, history

    system_text = rsp.strip()

    logger.info(f'\n--------------\n[第{cur_turn_index}轮] system_text:\n\n{system_text}\n--------------\n')

    my_history.append(user_input)
    output = system_text
    output_display = replace_newline(output)
    history.append((user_input, output_display))
    return history, history

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    model_choices = [ENGINE_DAVINCI_003, ENGINE_TURBO]
    parser.add_argument("--apikey_file", type=str, default="./config/apikey.txt")
    parser.add_argument("--model_name", type=str, default=ENGINE_DAVINCI_003, choices=model_choices)
    parser.add_argument("--logfile", type=str, default="./logs/load_dialogue_log.txt")
    parser.add_argument("--translation_file", type=str, default=None)
    parser.add_argument("--no_long_term_memory", action='store_true', help='do not use long-term memory, default False')
    parser.add_argument("--naive_memory", action='store_true', help='naive concat topk memory and concate history')
    parser.add_argument("--embed_summary", action='store_true', help='use summary embedding for each turn')
    # embed_summary
    parser.add_argument("--similar_top_k", type=int, default=6)
    args = parser.parse_args()

    check_key_file(args.apikey_file)

    log_path = args.logfile
    makedirs(log_path)
    # 配置日志记录

    logger = logging.getLogger('dialogue_logger')
    logger.setLevel(logging.INFO)

    formatter = logging.Formatter('【%(asctime)s - %(levelname)s】 - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

    file_handler = logging.FileHandler(log_path, encoding='utf-8')
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(formatter)

    logger.addHandler(file_handler)
    set_chat_logger(logger)
    set_api_logger(logger)

    logger.info('\n\n\n')
    logger.info('#################################')
    logger.info('#################################')
    logger.info('#################################')
    logger.info('\n\n\n')
    logger.info(f"args: \n\n{args}\n")

    stamp = datetime2str()
    # print(stamp)

    if args.translation_file:
        translation_map = load_json_file(args.translation_file)

    bot = ChatBot(model_name=args.model_name)
    # whether use scm for history memory
    no_long_term_memory = True if args.no_long_term_memory else False
    naive_memory = True if args.naive_memory else False
    embed_summary = True if args.embed_summary else False

    with gr.Blocks() as demo:
        gr.Markdown(f"
Long Dialogue Chatbot ({args.model_name}) for test
")
        chatbot = gr.Chatbot()
        state = gr.State()
        txt = gr.Textbox(show_label=False, placeholder="Ask me a question and press enter.").style(container=False)
        txt.submit(my_chatbot, inputs=[txt, state], outputs=[chatbot, state])

    demo.launch(share = False)


""" with open("Day25_csv_pandas/weather_data.csv") as data_file:
    data = data_file.readlines() """

""" import csv

with open('Day25_csv_pandas/weather_data.csv') as data_file:
    data = csv.reader(data_file)
    temperatures = []
    for row in data:
        if row[1] != "temp":
            temperatures.append(int(row[1]))
    print(temperatures) """

import pandas as pd

data = pd.read_csv('Day25_csv_pandas/weather_data.csv')
#print(data["temp"])

data_dic = data.to_dict()
#print(data_dic)

temp_list = data["temp"].to_list()
#print(temp_list)

#Calculate de mean
""" med = sum(temp_list) / len(temp_list)
print(med) """
# OR
#print(data["temp"].mean())

#print(data.temp.max())

#Get data in rows
#print(data[data.day == 'Monday'])

#print(data[data.temp == data.temp.max()].condition)

# Create a dataframe from scratch
data_dict = {
    'Students': ['Amy', 'James',' Angela'],
    'Scores' : [76, 56, 65]
}

data = pd.DataFrame(data_dict)
print(data)
data.to_csv('Day25_csv_pandas/new_data.csv')


from django.shortcuts import render
from operator import attrgetter

# Para a paginação
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage

# Importamos a função criada anteriormente
from post.views import get_post_queryset

POSTS_PER_PAGE = 2  # Pode ser qualquer valor acima de 0, coloquei 1 pois não tenho muitas postagens cadastradas
LIMIT_OF_PAGES = 10

def home_view(request):
    context = {}

    # inicialmente inicializamos a query com texto vazio
    query = ''

    # Caso no request.GET não sejá vazio, no caso seja diferente de <QueryDict: {}>
    # Lembre que qualquer ideia de vazio é considerado falso pelo Django
    if request.GET:
        # Coleta-se a query
        query = request.GET.get('q', '')  # Tenta pegar valor de 'q' caso não tenha retorna ''
        # Envia o valor da query para o template, para se manter a query da busca no search bar
        context['query'] = query

    # Faz a busca dos posts usando a query pra filtrar
    posts = get_post_queryset(query)
    # Ordena as postagens
    posts = sorted(posts, key=attrgetter('date_published'), reverse=True)

    # Pagination
    # Tenta pegar o valor de page do template, caso não encontre retorna o valor 1 que representa a primeira página
    page = request.GET.get('page', 1)
    # Cria-se um paginator, para isso se passa uma lista de objetos e a quantidade de páginas
    posts_paginator = Paginator(posts, POSTS_PER_PAGE)

    # Vai fazer toda a lógica de separação e navegação entre páginas, e já fazendo o tratamento das exceptions
    try:
        # Tenta acessar a página que foi passada pelo template, caso dê erro vai para as exceptions
        posts = posts_paginator.page(page)
    except InvalidPage:
        posts = posts_paginator.page(1)

    # Passa as postagens para o context do template
    context['posts'] = posts
    context['limit_of_pages'] = LIMIT_OF_PAGES
    return render(request, 'personal/home_view.html', context)


#!/usr/bin/env python3

import sys
import mrcfile
args = sys.argv
from IsoNet.util.filter import maxmask,stdmask
import numpy as np
#import cupy as cp
import os

def make_mask_dir(tomo_dir,mask_dir,side = 8, density_percentage=30,std_percentage=1,surface=None):
    tomo_list = ["{}/{}".format(tomo_dir,f) for f in os.listdir(tomo_dir)]
    try:
        os.makedirs(mask_dir)
    except FileExistsError:
        import shutil
        shutil.rmtree(mask_dir)
        os.makedirs(mask_dir)
    mask_list = ["{}/{}_mask.mrc".format(mask_dir,f.split('.')[0]) for f in os.listdir(tomo_dir)]

    for i,tomo in enumerate(tomo_list):
        print('tomo and mask',tomo, mask_list[i])
        make_mask(tomo, mask_list[i],side = side,density_percentage=density_percentage,std_percentage=std_percentage,surface=surface)

def make_mask(tomo_path, mask_name, mask_boundary = None, side = 5, density_percentage=50., std_percentage=50., surface=None):
    from scipy.ndimage.filters import gaussian_filter
    from skimage.transform import resize

    with mrcfile.open(tomo_path, permissive=True) as n:
        header_input = n.header
        #print(header_input)
        pixel_size = n.voxel_size
        tomo = n.data.astype(np.float32)
    sp=np.array(tomo.shape)
    sp2 = sp//2
    bintomo = resize(tomo,sp2,anti_aliasing=True)

    gauss = gaussian_filter(bintomo, side/2)
    if density_percentage <=99.8:
        mask1 = maxmask(gauss,side=side, percentile=density_percentage)
    else:
        mask1 = np.ones(sp2)

    if std_percentage <=99.8:
        mask2 = stdmask(gauss,side=side, threshold=std_percentage)
    else:
        mask2 = np.ones(sp2)

    out_mask_bin = np.multiply(mask1,mask2)

    if mask_boundary is not None:
        from IsoNet.util.filter import boundary_mask
        mask3 = boundary_mask(bintomo, mask_boundary)
        out_mask_bin = np.multiply(out_mask_bin, mask3)

    if (surface is not None) and surface < 1:
        for i in range(int(surface*sp2[0])):
            out_mask_bin[i] = 0
        for i in range(int((1-surface)*sp2[0]),sp2[0]):
            out_mask_bin[i] = 0

    out_mask = np.zeros(sp)
    out_mask[0:-1:2,0:-1:2,0:-1:2] = out_mask_bin
    out_mask[0:-1:2,0:-1:2,1::2] = out_mask_bin
    out_mask[0:-1:2,1::2,0:-1:2] = out_mask_bin
    out_mask[0:-1:2,1::2,1::2] = out_mask_bin
    out_mask[1::2,0:-1:2,0:-1:2] = out_mask_bin
    out_mask[1::2,0:-1:2,1::2] = out_mask_bin
    out_mask[1::2,1::2,0:-1:2] = out_mask_bin
    out_mask[1::2,1::2,1::2] = out_mask_bin
    out_mask = (out_mask>0.5).astype(np.uint8)

    with mrcfile.new(mask_name,overwrite=True) as n:
        n.set_data(out_mask)

        n.header.extra2 = header_input.extra2
        n.header.origin = header_input.origin
        n.header.nversion = header_input.nversion
        n.voxel_size = pixel_size
        #print(n.header)
    # with mrcfile.new('./test_mask1.rec',overwrite=True) as n:
    #     n.set_data(mask1.astype(np.float32))
    # with mrcfile.new('./test_mask2.rec',overwrite=True) as n:
    #     n.set_data(mask2.astype(np.float32))
if __name__ == "__main__":
# the first arg is tomo name the second is mask name
    make_mask(args[1],args[2])


from statistics import mode
from sklearn.feature_extraction import image
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
import os
import random
import pandas as pd
from utils.Metric import compute_meandice_multilabel

from utils.Visualize import plot_whole_imgs
from utils.Utils import concat_bg
from utils.Data import convert_label_to_brats

random_seed = 2022
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(random_seed)
random.seed(random_seed)

os.environ["CUDA_VISIBLE_DEVICES"] = "1"

def remove_input(tensor, axis=0, mode='zeros'):
    """Remove a single input modality

        input tensor: B x C x H x W
        ouptut tensor: B x C x H x W
    """
    output_tensor = torch.zeros_like(tensor)
    single_size = (tensor.size(0), *tensor.size()[2:])
    for i in range(output_tensor.size(1)):
        if i == axis:
            if mode == 'zeros':
                output_tensor[:, i] = torch.zeros(single_size)
            elif mode == 'random':
                output_tensor[:, i] = torch.randn(single_size)
        else:
            output_tensor[:, i] = tensor[:, i]
    return output_tensor

if __name__ == "__main__":

    from utils.Device import gpu_setting, model_dataparallel
    from utils.Model import load_model_weights
    from utils.Data import load_dataloader
    from utils.Uncertainty import get_dropout_uncertainty

    from model.unetr import UNETR
    from tqdm import tqdm

    import monai.transforms as tf

    sigmoid = nn.Sigmoid()

    # Model setting
    amp = True
    device, multi_gpu = gpu_setting()
    unetr = UNETR(in_channels=4,
                  out_channels=4,
                  img_size=240,
                  feature_size=8,
                  dropout_rate=0.3,
                  hidden_size=64,
                  num_heads=4,
                  mlp_dim=128,
                  pos_embed='conv',
                  norm_name='instance',
                  spatial_dims=2).to(device)
    model_weights = os.path.join("./result/exps/unetr-merge-4layer-withaug", "best.pth")
    unetr = load_model_weights(unetr, model_weights, dp=False)
    unetr = model_dataparallel(unetr, multi_gpu)

    test_loader_params = dict(
        batch_size=8,
        shuffle=False
    )

    # validation and test transforms
    val_transforms = tf.Compose([
        tf.LoadImaged(reader="NibabelReader", keys=['image', 'label']),
        # tf.AsDiscreted(keys=['label'], threshold_values=True),
        tf.EnsureTyped(keys=["image", "label"]),
        tf.ToTensord(keys=['image', 'label']),
        tf.ToDeviced(keys=["image", "label"], device=device)
    ])

    # Logging directories
    img_save_dir = os.path.join("./asset", "input_removed")
    os.makedirs(img_save_dir, exist_ok=True)
    root_dir = "/cluster/projects/mcintoshgroup/BraTs2020/data_monai/"

    # Test Dataloder
    test_dataloader = load_dataloader(root_dir, "test", val_transforms, test_loader_params)

    # Image seqs
    images_seqs = ["T1", "T1Gd", "T2", "FLAIR"]
    tumors_names = ["TC", "WT", "ET"]
    model_dice = list()
    # replace_modes = ["random", "zeros"]
    replace_modes = ["zeros"]

    models = [unetr]
    for replace_mode in replace_modes:
        for model_idx, model in enumerate(models):

            dice_dict = {
                "original": 0.,
                "T1": 0.,
                "T1Gd": 0.,
                "T2": 0.,
                "FLAIR": 0.
            }
            total_num_imgs = 0

            model.eval()
            with torch.no_grad():
                for batch_idx, batch in tqdm(enumerate(test_dataloader)):
                    inputs, labels = batch["image"], convert_label_to_brats(concat_bg(batch["label"]))  # use all four mods
                    labels_np = labels.detach().cpu().numpy()
                    inputs = inputs.to(device)
                    total_num_imgs += inputs.size(0)

                    # remove
                    outputs = torch.where(sigmoid(model(inputs)) > 0.5, 1, 0)
                    original_dice = compute_meandice_multilabel(outputs, labels, include_background=False) * inputs.size(0)
                    dice_dict["original"] += float(original_dice.data)

                    if float(original_dice / inputs.size(0)) > 0.75 and (labels_np[:, 1:].sum(1).sum() > 10000):
                        for i in range(4):  # Iterate over image sequences
                            removed_input = remove_input(inputs, i, mode=replace_mode)
                            removed_outputs = torch.where(sigmoid(model(removed_input)) > 0.5, 1, 0)

                            input_np = inputs[:, i].detach().cpu().numpy()
                            out_np = outputs[:, 1:].sum(1).detach().cpu().numpy()
                            label_np = labels_np[:, 1:].sum(1)
                            removed_out_np = removed_outputs[:, 1:].sum(1).detach().cpu().numpy()

                            plot_whole_imgs(input_np,
                                            os.path.join(img_save_dir, f"inputs-{images_seqs[i]}-{replace_mode}-{batch_idx}.jpg"),
                                            num_cols=2)

                            img_to_vis = np.concatenate([label_np, out_np, removed_out_np], -1)
                            plot_whole_imgs(img_to_vis,
                                            os.path.join(img_save_dir, f"outputs-{images_seqs[i]}_removed-{replace_mode}-{batch_idx}.jpg"),
                                            num_cols=2)

                if batch_idx == 192:
                    break

            for key, val in dice_dict.items():
                dice_dict[key] = val / total_num_imgs
            model_dice.append(list(dice_dict.values()))


from selenium import webdriver
import time
from selenium.webdriver.common.by import By

class clickSendkey():

    def test(self):
        driver = webdriver.Firefox(executable_path='/Users/Jatin Singh/Downloads/geckodriver')
        driver.get('https://google.com/')
        driver.maximize_window()

        texttype = driver.find_element_by_xpath("//input[@type='text']")
        texttype.send_keys('letskodeitselenium')
        time.sleep(3)

        search = driver.find_element_by_xpath("//div[@class='FPdoLc tfB0Bf']//input[@class='gNO89b']")
        search.click()
        time.sleep(3)

        driver.close()

o = clickSendkey()
o.test()


import copy
import math
import numpy as np
import random as rd
from typing import List

class Layer:
    def __init__(self, size: int, next_size: int):
        self.size = size
        self.neurons = np.zeros((size,))
        self.biases = np.zeros((size,))
        self.weights = np.zeros((size, next_size))

class Point:
    x: int
    y: int
    type: int

    def __init__(self, x, y, type) -> None:
        self.x = x
        self.y = y
        self.type = type

class NeuralNetwork:
    learningRate: float
    layers: List[Layer]

    def __init__(self, learningRate, sizes) -> None:
        self.learningRate = learningRate
        self.layers = copy.copy(sizes)
        self.epoch = 0
        for i, _ in enumerate(sizes):
            next_size = 0
            if (i < len(sizes) - 1):
                next_size = sizes[i + 1]
            self.layers[i] = Layer(sizes[i], next_size)
            for j in range(sizes[i]):
                self.layers[i].biases[j] = rd.random() * 2.0 - 1.0
                for k in range(next_size):
                    self.layers[i].weights[j][k] = rd.random() * 2.0 - 1.0

    def sigmoid(self, x):
        return 1 / (1 + math.exp(-x))

    def dsigmoid(self, y):
        return y * (1 - y)

    def feed_forfard(self, inputs: List[float]):
        layers = self.layers

        for i, v in enumerate(inputs):
            layers[0].neurons[i] = v

        for i in range(1, len(layers)):
            l = layers[i-1]
            l1 = layers[i]
            for j in range(l1.size):
                l1.neurons[j] = 0
                for k in range(l.size):
                    l1.neurons[j] += l.neurons[k] * l.weights[k][j]

                l1.neurons[j] += l1.biases[j]
                l1.neurons[j] = self.sigmoid(l1.neurons[j])

        return layers[len(layers)-1].neurons

    def back_propagation(self, targets: List[float]):
        layers = self.layers
        errors = np.zeros((layers[len(layers)-1].size,))
        for i in range(layers[len(layers)-1].size):
            errors[i] = targets[i] - layers[len(layers)-1].neurons[i]

        for k in range(len(layers)-2, -1, -1):
            l = layers[k]
            l1 = layers[k+1]
            errors_next = np.zeros((l.size,))
            gradients = np.zeros((l1.size,))

            for i in range(l1.size):

                gradients[i] = errors[i] * \
                    self.dsigmoid(layers[k + 1].neurons[i])
                gradients[i] *= self.learningRate

            deltas = np.zeros((l1.size, l.size))
            for i in range(l1.size):
                for j in range(l.size):
                    deltas[i][j] = gradients[i] * l.neurons[j]

            for i in range(l.size):
                errors_next[i] = 0
                for j in range(l1.size):
                    errors_next[i] += l.weights[i][j] * errors[j]

            errors = np.zeros((l.size,))
            for i, v in enumerate(errors_next):
                errors[i] = v

            weights_new = np.zeros((len(l.weights), len(l.weights[0])))
            for i in range(l1.size):
                for j in range(l.size):
                    weights_new[j][i] = l.weights[j][i] + deltas[i][j]

            l.weights = weights_new
            for i in range(l1.size):
                l1.biases[i] += gradients[i]

        return None


# -*- coding: utf-8 -*-
#
"""
Utility functions related to input/output.
"""

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

from io import open # pylint: disable=redefined-builtin
import os
#import logging
import importlib
import yaml

import tensorflow as tf
from tensorflow import gfile

as_text = tf.compat.as_text

__all__ = [
    "write_paired_text",
    "load_config_single",
    "load_config"
]

#def get_tf_logger(fname,
#                  verbosity=tf.logging.INFO,
#                  to_stdio=False,
#                  stdio_verbosity=None):
#    """Creates TF logger that allows to specify log filename and whether to
#    print to stdio at the same time.
#
#    Args:
#        fname (str): The log filename.
#        verbosity: The threshold for what messages will be logged. Default is
#            `INFO`. Other options include `DEBUG`, `ERROR`, `FATAL`, and `WARN`.
#            See :tf_main:`tf.logging <logging>`.
#        to_stdio (bool): Whether to print messages to stdio at the same time.
#        stido_verbosity (optional): The verbosity level when printing to stdio.
#            If `None` (default), the level is set to be the same as
#            :attr:`verbosity`. Ignored if :attr:`to_stdio` is False.
#
#    Returns:
#        The TF logger.
#    """

def _load_config_python(fname):
    config = {}

    config_module = importlib.import_module(fname.rstrip('.py'))
    for key in dir(config_module):
        if not (key.startswith('__') and key.endswith('__')):
            config[key] = getattr(config_module, key)

    return config

def _load_config_yaml(fname):
    with gfile.GFile(fname) as config_file:
        config = yaml.load(config_file)
    return config

def load_config_single(fname, config=None):
    """Loads config from a single file.

    The config file can be either a Python file (with suffix '.py')
    or a YAML file. If the filename is not suffixed with '.py', the file is
    parsed as YAML.

    Args:
        fname (str): The config file name.
        config (dict, optional): A config dict to which new configurations are
            added. If `None`, a new config dict is created.

    Returns:
        A `dict` of configurations.
    """
    if fname.endswith('.py'):
        new_config = _load_config_python(fname)
    else:
        new_config = _load_config_yaml(fname)

    if config is None:
        config = new_config
    else:
        for key, value in new_config.items():
            if key in config:
                if isinstance(config[key], dict):
                    config[key].update(value)
                else:
                    config[key] = value
            else:
                config[key] = value

    return config

def load_config(config_path, config=None):
    """Loads configs from (possibly multiple) file(s).

    Args:
        config_path: Paths to configuration files. This can be a `list` of
            config file names, or a path to a directory in which all files
            are loaded, or a string of multiple file names separated by commas.
        config (dict, optional): A config dict to which new configurations are
            added. If `None`, a new config dict is created.

    Returns:
        A `dict` of configurations.
    """
    fnames = []
    if isinstance(config_path, (list, tuple)):
        fnames = list(config_path)
    elif gfile.IsDirectory(config_path):
        for fname in gfile.ListDirectory(config_path):
            fname = os.path.join(config_path, fname)
            if not gfile.IsDirectory(fname):
                fnames.append(fname)
    else:
        for fname in config_path.split(","):
            fname = fname.strip()
            if not fname:
                continue
            fnames.append(fname)

    if config is None:
        config = {}

    for fname in fnames:
        config = load_config_single(fname, config)

    return config

def write_paired_text(src, tgt, fname, append=False, mode='h', sep='\t'):
    """Writes paired text to a file.

    Args:
        src: A list (or array) of `str` source text.
        ttg: A list (or array) of `str` target text.
        fname (str): The output filename.
        append (bool): Whether appending to the end of the file if exists.
        mode (str): The mode of writing, with the following options:

            - :attr:`'h'`: The "horizontal" mode. Each source target pair is \
                written in one line, intervened with :attr:`sep`, e.g.,

                    source_1 target_1
                    source_2 target_2

            - :attr:`'v'`: The "vertical" mode. Each source target pair is \
                written in two consecutive lines, e.g,

                    source_1
                    target_1
                    source_2
                    target_2

            - :attr:`'s'`: The "separate" mode. Each source target pair is \
                    written in corresponding lines of two files named \
                    as :attr:`fname`.src and :attr:`fname`.tgt, respectively.
        sep (str): The string intervening between source and target. Used
            when :attr:`mode`='h'.

    Returns:
        The fileanme(s). If :attr:`mode`=='h' or :attr:`mode`=='v', returns
        :attr:`fname`. If :attr:`mode`=='s', returns a list of filenames
        `[':attr:`fname`.src', ':attr:`fname`.tgt']`.
    """
    fmode = 'a' if append else 'w'
    if mode == 's':
        fn_src = '{}.src'.format(fname)
        fn_tgt = '{}.tgt'.format(fname)
        with open(fn_src, fmode, encoding='utf-8') as fs:
            fs.write(as_text('\n'.join(src)))
            fs.write('\n')
        with open(fn_tgt, fmode, encoding='utf-8') as ft:
            ft.write(as_text('\n'.join(tgt)))
            ft.write('\n')
        return fn_src, fn_tgt
    else:
        with open(fname, fmode, encoding='utf-8') as f:
            for s, t in zip(src, tgt):
                if mode == 'h':
                    text = '{}{}{}\n'.format(as_text(s), sep, as_text(t))
                    f.write(as_text(text))
                elif mode == 'v':
                    text = '{}\n{}\n'.format(as_text(s), as_text(t))
                    f.write(as_text(text))
                else:
                    raise ValueError('Unknown mode: {}'.format(mode))
        return fname


import tensorflow as tf

def single_accuracy(y_true: dict, y_pred: list) -> dict:
    """Compute Team, Position and Mask metrics for a batch of single sequence:
        - binary accuracy for Team prediction
        - categorical accuracy for Position classification
        - topk accuracy 3 for Position classification
        - categorical accuracy for Mask prediction
        - topk accuracy 3 for Mask prediction

    Args:
        y_true (dict): a dict with keys: Team, Pos and Mask with the true label for each values
        y_pred (list): a list with elements: Team, Pos and Mask with the predicted label for each element

    Returns:
        dict: dict with single sequence metrics
    """
    pred_team = tf.cast(tf.round(y_pred[0]), dtype ="int64")
    team_accuracy = tf.keras.metrics.binary_accuracy(y_true["Team"], pred_team)
    team_accuracy = tf.reduce_mean(team_accuracy)

    pos_accuracy = tf.keras.metrics.sparse_categorical_accuracy(y_true["Pos"], y_pred[1])
    pos_accuracy = tf.reduce_mean(pos_accuracy)

    pos_topK = tf.keras.metrics.sparse_top_k_categorical_accuracy(y_true["Pos"], y_pred[1], k = 3)
    pos_topK = tf.reduce_mean(pos_topK)

    mask_accuracy = tf.keras.metrics.sparse_categorical_accuracy(y_true["Mask"], y_pred[2])
    mask_accuracy = tf.reduce_mean(mask_accuracy)

    mask_topK = tf.keras.metrics.sparse_top_k_categorical_accuracy(y_true["Mask"], y_pred[2], k = 3)
    mask_topK = tf.reduce_mean(mask_topK)

    return {"Team_Accuracy" : team_accuracy,
            "Pos_Accuracy" : pos_accuracy,
            "Pos_topK" : pos_topK,
            "Mask_Accuracy" : mask_accuracy,
            "Mask_topK" : mask_topK}

def compute_accuracy(y_true: dict, y_pred: list) -> dict:
    """Compute metrics for a single batch. A batch is made of pairs of sequences.

    Each sequence is used to compute the single metric. Then the SamePlay metric is computed using both sequence.
    Every metrics computed by sequence are then averaged into a single value per metrics.

    Args:
        y_true (dict): a dict with keys: Team, Pos and Mask with the true label for each values
        y_pred (list): a list with elements: Team, Pos and Mask with the predicted label for each element

    Returns:
        dict: dict with every metrics
    """
    acc1 = single_accuracy(y_true["Y1"], y_pred[1][1:])
    acc2 = single_accuracy(y_true["Y2"], y_pred[2][1:])

    sameplay_accuracy = tf.keras.metrics.binary_accuracy(y_true["Strat"], y_pred[0])

    team_accuracy = (acc1["Team_Accuracy"]+acc2["Team_Accuracy"])/2

    pos_accuracy = (acc1["Pos_Accuracy"]+acc2["Pos_Accuracy"])/2

    pos_topK = (acc1["Pos_topK"]+acc2["Pos_topK"])/2

    mask_accuracy = (acc1["Mask_Accuracy"]+acc2["Mask_Accuracy"])/2

    mask_topK = (acc1["Mask_topK"]+acc2["Mask_topK"])/2

    return {"SamePlay_Accuracy" : sameplay_accuracy,
            "Team_Accuracy" : team_accuracy,
            "Pos_Accuracy" : pos_accuracy,
            "Pos_topK" : pos_topK,
            "Mask_Accuracy" : mask_accuracy,
            "Mask_topK" : mask_topK}


from Imports import *
from Exercice2 import code_communs

# 1. Fixer comme indice la variable de code commune dans les deux bases. Regarder le
# changement que cela induit sur le display du DataFrame
df_code_communs = df[df["INSEE commune"].isin(code_communs)]
df_city_code_communs = df_city[df_city["CODGEO"].isin(code_communs)]

# 2. Les deux premiers chiffres des codes communes sont le numéro de département.
# Créer une variable de département dep dans df et dans df_city
df["dep"] = df["INSEE commune"].str.slice(0, 2)
df_city["dep"] = df_city["CODGEO"].str.slice(0, 2)

# 3. Calculer les émissions totales par secteur pour chaque département. Mettre en log
# ces résultats dans un objet df_log. Garder 5 départements et produire un barplot
emissions_par_secteur = df.groupby('INSEE commune')['CO2 biomasse hors-total'].sum()

secteur_5_barplot = emissions_par_secteur[0:5]

plt.bar(secteur_5_barplot.index, secteur_5_barplot.values)
plt.xlabel('secteur')
plt.ylabel('Somme')
plt.title("somme des émissions par secteur")
# plt.show()

# 4. Repartir de df. Calculer les émissions totales par département et sortir la liste des 10
# principaux émetteurs de CO2 et des 5 départements les moins émetteurs. Sans faire
# de merge, regarder les caractéristiques de ces départements (population et niveau de vie)
emissions_par_dep = df.groupby('dep')['CO2 biomasse hors-total'].sum()

principaux_emetteurs = sorted(range(len(emissions_par_dep)), key=lambda i: emissions_par_dep[i], reverse=True)[:10]
moins_emetteurs = sorted(range(len(emissions_par_dep)), key=lambda i: emissions_par_dep[i])[:5]

df_principaux_emetteurs = df[df['dep'].isin(principaux_emetteurs)]
df_moins_emetteurs = df[df['dep'].isin(moins_emetteurs)]


from datasets import load_conll2003_en
from conll_dictorizer import CoNLLDictorizer
from dictionizer import dictionize
from sklearn.feature_extraction import DictVectorizer
from keras.preprocessing.sequence import pad_sequences

from keras import models, layers
from keras.utils import to_categorical
from keras.layers import LSTM, Bidirectional, SimpleRNN, Dense

import numpy as np

def build_sequences(dic):
    X, Y = [], []
    for sentence in dic:
        x, y = [], []
        for word in sentence:
            x.append(word['form'].lower())
            y.append(word['ner'])
        X.append(x)
        Y.append(y)
    return X,Y

def vocabulary(train_dict, dictionizer):
    vocabulary_words = []
    for dic in train_dict:
        for d in dic:
            vocabulary_words.append(d['form'].lower())
    for d in dictionizer:
        vocabulary_words.append(d)
    return sorted(list(set(vocabulary_words)))

def to_index(seq, idx):
    tmp_seq = []
    for s in seq:
        s_idx = []
        for l in s:
            #Get the value, if not in word_idx => 0 else value
            if l in idx:
                s_idx.append(idx[l])
            else:
                s_idx.append(0)
        tmp_seq.append(s_idx)
    return tmp_seq

if __name__ == "__main__":
    train_sentences, dev_sentences, test_sentences, column_names = load_conll2003_en()

    conll_dict = CoNLLDictorizer(column_names, col_sep=' +')
    train_dict = conll_dict.transform(train_sentences)
    test_dict = conll_dict.transform(test_sentences)

    X, Y = build_sequences(train_dict)

    dic = dictionize('glove.6B.100d.txt')

    vocabulary = vocabulary(train_dict, dic)

    #tmp_y = []
    #for y in Y:
    #    for y_i in y:
    #        tmp_y.append(y_i)

    tmp_y = sorted(list(set([ner for sentence in Y for ner in sentence])))

    rev_word_idx = dict(enumerate(vocabulary, start=2))
    word_idx = {v: k for k, v in rev_word_idx.items()}

    rev_ner_id = dict(enumerate(tmp_y, start=2))
    ner_idx = {v: k for k, v in rev_ner_id.items()}

    nb_classes = len(tmp_y)
    print(nb_classes)

    M = len(vocabulary) + 2
    #print(M)
    N = 100
    matrix = np.random.rand(M, N)

    for word in vocabulary:
        if word in dic.keys():
            matrix[word_idx[word]] = dic[word]

    #dict_vect = DictVectorizer(sparse=False)

    dev_dict = conll_dict.transform(dev_sentences)
    X_dev, Y_dev = build_sequences(dev_dict)
    X_dev_i = to_index(X_dev, word_idx)
    Y_dev_i = to_index(Y_dev, ner_idx)
    X_dev_pad = pad_sequences(X_dev_i)
    Y_dev_pad = pad_sequences(Y_dev_i)
    Y_dev_cat = to_categorical(Y_dev_pad, num_classes=nb_classes + 2)

    #Ta X och gör om till index-värden
    X_idx = to_index(X, word_idx)

    Y_idx = to_index(Y, ner_idx)

    padded_x = pad_sequences(X_idx, maxlen=150)
    padded_y = pad_sequences(Y_idx, maxlen=150)

    y_train = to_categorical(padded_y, num_classes=nb_classes + 2)

    model = models.Sequential()
    model.add(layers.Embedding(
        M,
        N,
        mask_zero=True,
        input_length=None
    ))
    model.layers[0].set_weights([matrix])
    model.layers[0].trainable = False

    #model.add(SimpleRNN(100, return_sequences=True))
    model.add(layers.Dropout(0.25))
    model.add(Bidirectional(LSTM(100, recurrent_dropout=0.25, return_sequences=True)))
    model.add(layers.Dropout(0.25))
    model.add(Bidirectional(LSTM(100, recurrent_dropout=0.25, return_sequences=True)))
    model.add(layers.Dropout(0.25))
    #model.add(Dense(512, activation='relu'))
    #model.add(layers.Dropout(0.25))
    model.add(Dense(nb_classes + 2, activation='softmax'))

    model.compile(loss='categorical_crossentropy',
                optimizer='rmsprop',
                metrics=['acc'])

    model.summary()

    model.fit(padded_x, y_train, epochs=10, batch_size=128, validation_data=(X_dev_pad, Y_dev_cat))

    X_test, Y_test = build_sequences(test_dict)

    X_test_idx = to_index(X_test, word_idx)
    Y_test_idx = to_index(Y_test, ner_idx)

    #print('X[0] test idx', X_test_idx[0])
    #print('Y[0] test idx', Y_test_idx[0])

    X_test_padded = pad_sequences(X_test_idx)
    Y_test_padded = pad_sequences(Y_test_idx)

    #print('X[0] test idx passed', X_test_padded[0])
    #print('Y[0] test idx padded', Y_test_padded[0])

    Y_test_padded_vectorized = to_categorical(Y_test_padded, num_classes=nb_classes + 2)

    #print('Y[0] test idx padded vectorized', Y_test_padded_vectorized[0])
    print(X_test_padded.shape)
    print(Y_test_padded_vectorized.shape)

    test_loss, test_acc = model.evaluate(X_test_padded, Y_test_padded_vectorized)
    #print('Loss:', test_loss)
    #print('Accuracy:', test_acc)

    print('X_test', X_test[0])
    print('X_test_padded', X_test_padded[0])
    corpus_ner_predictions = model.predict(X_test_padded)
    print('Y_test', Y_test[0])
    print('Y_test_padded', Y_test_padded[0])
    print('predictions', corpus_ner_predictions[0])

    ner_pred_num = []
    for sent_nbr, sent_ner_predictions in enumerate(corpus_ner_predictions):
        ner_pred_num += [sent_ner_predictions[-len(X_test[sent_nbr]):]]
    print(ner_pred_num[:2])

    ner_pred = []
    for sentence in ner_pred_num:
        ner_pred_idx = list(map(np.argmax, sentence))
        ner_pred_cat = list(map(rev_ner_id.get, ner_pred_idx))
        ner_pred += [ner_pred_cat]

    result = open("result_ltsm_no_rnn.txt", "w+")
    for id_s, sentence in enumerate(X_test):
        for id_w, word in enumerate(sentence):
            result.write(f"{word} {Y_test[id_s][id_w]} {ner_pred[id_s][id_w]}\n")
    result.close()


import sys
import csv
import sqlite3

# creates db if one does not exist
# conn = sqlite3.connect('some.db')
class CSVTool(object):

    def __init__(self, path=None):
        self.path = None
        self.csv_name = None
        self.db = None

    def dump_to_db(self, csv_path):
        # store connection
        self.db = sqlite3.connect('csi.db')
        # get cursor object
        cur = self.db.cursor()
        # sql staement to create a tbale with only ID field
        sqlst = 'CREATE TABLE csi(ID int PRIMARY KEY NOT NULL)'
        # preform the above statement
        cur.execute(sqlst)
        with open(csv_path) as csv_file:
            read_csv = csv.DictReader(csv_file)
            import pdb; pdb.set_trace()
            for row in read_csv:
                sqlst = """

                """
                cur.execute(sqlst.format(int(read_csv.line_num)))
            self.db.commit()
            self.db.close()
if __name__ == '__main__':
    ct = CSVTool()
    ct.dump_to_db('csi.csv')


import math
# Write a program that calculates and prints the value according to the given formula:
# Q = Square root of [(2 * C * D)/H]
# Following are the fixed values of C and H:
# C is 50. H is 30.
# D is the variable whose values should be input to your program in a comma-separated sequence.

q = []
C = 50
H = 30
D = (input("Enter value for D: "))

d_array = [int(x) for x in D.split(",")]

for n in d_array:
    q.append(str(math.floor(math.sqrt((2*C*n)/H))))

print(",".join(q))


from setuptools import setup, find_packages
import sys, os

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.rst')).read()
NEWS = open(os.path.join(here, 'NEWS.txt')).read()

version = '0.1'

install_requires = [
    # List your project dependencies here.
    # For more details, see:
    # http://packages.python.org/distribute/setuptools.html#declaring-dependencies
    "scipy>=1.4",
    "seaborn>=0.10",
    "statsmodels>=0.11",
    "matplotlib>=3.2",
    "numpy>=1.18",
    "pandas>=1.0"
]

setup(name='starplot',
    version=version,
    description="Create barplots or boxplots with significant level annotations.",
    long_description=README + '\n\n' + NEWS,
    classifiers=[
      # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
    ],
    keywords='python numpy scipy statsmodels matplotlib seaborn t-test f-test mann-whitney non-parametric boxplots barplots significance-stars',
    author='yufongpeng',
    author_email='sciphypar@gmail.com',
    url='https://github.com/yufongpeng/starplot',
    license='',
    packages=find_packages('src'),
    package_dir = {'': 'src'},include_package_data=True,
    zip_safe=False,
    install_requires=install_requires,
    entry_points={
        'console_scripts':
            ['starplot=starplot:main']
    }
)


# 1. Реализовать скрипт, в котором должна быть предусмотрена функция расчёта заработной платы сотрудника.
# Используйте в нём формулу: (выработка в часах*ставка в час) + премия. Во время выполнения расчёта для конкретных
# значений необходимо запускать скрипт с параметрами.

from sys import argv
script_name, work_hours, rate_per_hour, premium = argv
print("Название скрипта", script_name)
print("Отработано часов", work_hours)
print("Ставка в час", rate_per_hour)
print("Премия", premium)
print("ИТОГО: ", int(work_hours) * int(rate_per_hour) + int(premium))

# 2. Представлен список чисел. Необходимо вывести элементы исходного списка, значения которых больше предыдущего элемента.
# Подсказка: элементы, удовлетворяющие условию, оформить в виде списка. Для его формирования используйте генератор.
# Пример исходного списка: [300, 2, 12, 44, 1, 1, 4, 10, 7, 1, 78, 123, 55].
# Результат: [12, 44, 4, 10, 78, 123].

control_list = [1, 2, 50, 1, 300, 600, 1, 12, 6, 50]
print(f' Контрольный список:  {control_list}')
end_list = [control_list[index] for index in range(1, len(control_list)) if control_list[index] > control_list[index-1]]
print("Значение больше предыдущего:  ", end_list)

# попробую изменить условие на меньше чем предыдущий:

control_list = [1, 2, 50, 1, 300, 600, 1, 12, 6, 50]
#print(f' Контрольный список:  {control_list}')
end_list_min = [control_list[index] for index in range(1, len(control_list)) if control_list[index] < control_list[index-1]]
print("Значение меньше предыдущего: ", end_list_min)

# Контрольный список:  [1, 2, 50, 1, 300, 600, 1, 12, 6, 50]
# Значение больше предыдущего:   [2, 50, 300, 600, 12, 50]
# Значение меньше предыдущего:  [1, 1, 6]

# 3. Для чисел в пределах от 20 до 240 найти числа, кратные 20 или 21. Решите задание в одну строку.
# Подсказка: используйте функцию range() и генератор.

x = [el for el in range(20, 241) if el % 20 == 0 or el % 21 == 0]
print(x)

# Для чистоты эксперимента заменим другими значениями:
# x = [el for el in range(20, 501) if el % 17 == 0 or el % 57 == 0]
# print(x)

# 4. Представлен список чисел. Определите элементы списка, не имеющие повторений. Сформируйте итоговый массив чисел,
# соответствующих требованию. Элементы выведите в порядке их следования в исходном списке. Для выполнения задания
# обязательно используйте генератор.
# Пример исходного списка: [2, 2, 2, 7, 23, 1, 44, 44, 3, 2, 10, 7, 4, 11].
# Результат: [23, 1, 3, 10, 4, 11]

from random import randint

generat = [randint(55, 60) for i in range(20)]
print(f'Сгенерированный список: {generat}')
itog = [el for el in generat if generat.count(el) == 1]
print(f'Не повторяются следующие:  {itog}')
#print("При генерации повторилось- ", len(generat)-len(itog))

# Сгенерированный список: [59, 57, 58, 58, 59, 59, 60, 58, 60, 55, 55, 57, 58, 55, 60, 56, 57, 57, 56, 56]
# Не повторяются следующие:  []
# При генерации повторилось-  20

# 5. Реализовать формирование списка, используя функцию range() и возможности генератора. В список должны войти чётные
# числа от 100 до 1000 (включая границы). Нужно получить результат вычисления произведения всех элементов списка.
# Подсказка: использовать функцию reduce().

from functools import reduce
generat = [i for i in range(100, 1001, 2)]
print(f'Сгенерированный список: {generat}')
itog = reduce(lambda x, y: x*y, generat)
print(itog)

# 6. Реализовать два небольших скрипта:
# итератор, генерирующий целые числа, начиная с указанного;
# итератор, повторяющий элементы некоторого списка, определённого заранее. Подсказка: используйте функцию count() и
# cycle() модуля itertools. Обратите внимание, что создаваемый цикл не должен быть бесконечным. Предусмотрите условие его
# завершения.
# #### Например, в первом задании выводим целые числа, начиная с 3. При достижении числа 10 — завершаем цикл.
# # Вторым пунктом необходимо предусмотреть условие, при котором повторение элементов списка прекратится.

from itertools import count, cycle
import sys

# первая часть
start_from = 10
def integer_generator(start_from):
    for element in count(start_from):
        if element > start_from+5:
            break
        yield element
abc = []
for element in integer_generator(1):
    print(element)
    abc.append(element)
print(abc)
# Вторая часть
x = "135"
x_count = 0
abc = []
for i in cycle(x):
    if i == x[0]:
        x_count += 1
    if x_count < 3:
        print(i)
        abc.append(i)
    else:
        break
print(abc)

# 7. Реализовать генератор с помощью функции с ключевым словом yield, создающим очередное значение. При вызове функции
# должен создаваться объект-генератор. Функция вызывается следующим образом: for el in fact(n). Она отвечает за получение
# факториала числа. В цикле нужно выводить только первые n чисел, начиная с 1! и до n!.
# Подсказка: факториал числа n — произведение чисел от 1 до n. Например, факториал четырёх 4! = 1 * 2 * 3 * 4 = 24.

from functools import reduce
from itertools import count
from math import factorial

def fact(n):
    result = 1
    for el in count(1):
        if el <= n:
            result *= el
            #result = factorial(el)
            yield result
        else:
            break
abc = []
for i in fact(10):
    print(i)
    abc.append(i)
print(abc)


from asyncio.locks import Lock
from typing import Union
import markovify
import asyncio

class InitializationError(Exception):
    """Error thrown when the model is not initialized"""

    pass

class MarkovHandler:
    """
    Manages the state of the internal markov model
    """

    def __init__(self):
        self._markov = None
        self._lock = Lock()

    async def add(self, text: str) -> None:
        """
        Add a list of strings to the markov model's corpus
        """
        async with self._lock:
            if self._markov is None:
                self._markov = await asyncio.get_event_loop().run_in_executor(
                    None, markovify.Text, text
                )
            else:
                new_model = await asyncio.get_event_loop().run_in_executor(
                    None, markovify.Text, text
                )
                self._markov = await asyncio.get_event_loop().run_in_executor(
                    None, markovify.utils.combine, (new_model, self._markov)
                )

    async def generate(self) -> Union[str, None]:
        """
        Generate sentences using the markov model
        """
        if self._markov is not None:
            return await asyncio.get_event_loop().run_in_executor(
                None, self._markov.make_sentence
            )
        else:
            raise InitializationError("Model not initialized")

    async def info(self) -> dict:
        """
        Returns some stats about the state of the markov model
        """

    async def reset(self):
        """
        Reset the internal markov model
        """
        self._markov = markovify.NewlineText()


from odoo import models, api
from odoo.osv.orm import browse_record

class ReportDynamicLabel(models.AbstractModel):
    _name = 'report.of_label.report_label'

    @api.multi
    def get_data(self, row, columns, ids, model, number_of_copy):
        active_model_obj = self.env[model]
        label_print_obj = self.env['label.print']
        label_print_data = label_print_obj.\
            browse(self.env.context.get('label_print'))
        mode = label_print_data.mode
        result = []
        value_vals = []
        diff = 0
        for datas in active_model_obj.browse(ids):
            for i in range(0, number_of_copy):
                vals = []
                bot = False
                bot_dict = {}
                if mode == 'fields':
                    for field in label_print_data.field_ids:
                        string = ''
                        pos = ''
                        if field.python_expression and field.python_field:
                            string = field.python_field.split('.')[-1]
                            value = eval(field.python_field, {'obj': datas})

                        elif field.field_id.name:
                            string = field.field_id.field_description
                            value = getattr(datas, field.field_id.name)

                        if not value:
                            continue

                        if isinstance(value, browse_record):
                            model_obj = self.pool.get(value._name)
                            value = eval("obj." + model_obj._rec_name,
                                         {'obj': value})

                        if not value:
                            value = ''

                        if field.nolabel:
                            string = ''
                        else:
                            string += ' :- '

                        if field.type == 'image' or field.type == 'barcode':
                            string = ''
                            if field.position != 'bottom':
                                pos = 'float:' + str(field.position) + ';'
                                bot = False
                            else:
                                bot = True
                                bot_dict = {'string': string, 'value': value,
                                            'type': field.type,
                                            'newline': field.newline,
                                            'css_string': not field.nolabel and field.css_string or '',
                                            'style': "font-size:" +
                                            str(field.fontsize) + "px;" + pos}
                        else:
                            bot = False
                        if not bot:
                            vals_dict = {'string': string, 'value': value,
                                         'type': field.type,
                                         'newline': field.newline,
                                         'css_string': not field.nolabel and field.css_string or '',
                                         'css_value': field.css_value,
                                         'style': "font-size:" +
                                         str(field.fontsize) + "px;" + pos}
                            vals.append(vals_dict)
                    if bot_dict != {}:
                        vals.append(bot_dict)
                    if vals and vals[0]['value'] not in value_vals:
                        value_vals.append(vals[0]['value'])
                    result.append(vals)
                    temp = vals
                elif label_print_data.mode == 'template':
                    #vals = {'model': model, 'id': datas.id}
                    vals = datas
                    result.append(vals)
                    temp = vals

        newlist_len = 0
        new_list = []
        result1 = []
        list_newdata = []
        for row in range(0, len(result) / (columns) + 1):
            val = result[row * columns: row * columns + columns]
            if val:
                new_list.append(val)
            if mode == 'fields':
                for value_list in val:
                    for value_print in value_list:
                        list_newdata.append(value_print['value'])

        for data in new_list:
            for list_data in data:
                newlist_len = newlist_len + 1

        remain_data = []
        counter = 0
        for newlist_data in list_newdata:
            if newlist_data in list_newdata:
                counter = counter + 1
            if counter > number_of_copy:
                counter = 1
            if counter < number_of_copy:
                remain_copy = number_of_copy - counter
                for xx in range(0, remain_copy):
                    remain_data.append(newlist_data)

        for data_value_vals in value_vals:
            if data_value_vals not in list_newdata:
                for add_data in range(0, number_of_copy):
                    remain_data.append(data_value_vals)

        if newlist_len <= number_of_copy:
            diff = number_of_copy - newlist_len

        if len(ids) == 1:
            for new_result in range(0, diff):
                result1.append(temp)

        # Duplicates the data and raise the issue with the label format.
#         if len(ids) > 1:
#             for remain_data_value in remain_data:
#                 temp[0]['value'] = remain_data_value
#                 result1.append([temp[0].copy()])
#
#         for row in range(0, len(result1)):
#             new_val = result1[row * columns: row * columns + columns]
#             new_list.append(new_val)

        return new_list

    @api.model
    def render_html(self, docids, data):
        report = self.env['report']
        report_name = 'of_label.report_label' + (data.get('landscape') and '_landscape' or '')
        lab_report = report._get_report_from_name(report_name)
        if data is None:
            data = {}
        if not docids:
            docids = data.get('docids')
        docargs = {
            'doc_ids': docids,
            'doc_model': lab_report.model,
            'data': data,
            'get_data': self.get_data,
        }
        return report.render(report_name, docargs)

class ReportDynamicLabelLandscape(models.AbstractModel):
    _name = 'report.of_label.report_label_landscape'
    _inherit = 'report.of_label.report_label'
    u"""Héritage vide pour permettre l'impression au format paysage"""


a = list(map(int, input().split()))

i5 = 0
i7 = 0

for i in a:
    if i == 5:
        i5 += 1
    if i == 7:
        i7 += 1
if i5==2 and i7==1:
    print('YES')
else:
    print('NO')


#-*- encoding:utf-8 -*-
#written by:	Jiao Zhongxiao

import os
import shutil
import xml.etree.ElementTree as ET
import io
import binascii
import sys
from threading import Thread
from ftplib import FTP
#import subprocess
os.chdir( os.path.split( sys.argv[0] )[0] )

#config----------------------------------------------------------
publishDir = "D:\\"	#缓存目录
devDir = "E:\\workspace\\TowerFacebookDev\\branches\\TD_Branch_001\\release\\flash"
assets = ".json,.jpg,.png,.swf,.xml,.mp3"
#ver_config为必须
#bin-debug, bin-release目录的文件只会加上版本号后缀
#其它目录文件的会根据算法生成CDN版本后缀
#（CRC32（文件内容）& 0xFFFFFFFF）的32进制表示
publishDirs = "ver_config,bin-debug,release"
#end config------------------------------------------------------

ver_config = "ver_config"
projectDir = os.getcwd() + "/../"
newFile = 0

print( "欢迎使用Tower版本发布工具" )
print( "使用前请确保已编辑了此文件做好了相关配置" )
#检查运行环境
#print( os.environ["PATH"] )
version = ""
def checkVersion():
	if len(version) > 0:
		return True
	return False

while True:
	version = input( "请输入这次的版本号后 <Enter 回车> 继续：\n" )
	if checkVersion():
		break
#version = "13"

#1、更新SVN-------------------------------------------------------
print( "1、更新SVN" )
def checkToolPath( tool ):
	environ = os.environ["PATH"].split( ";" )
	for possiblePath in environ:
		possiblePath = os.path.join( possiblePath, tool )
		#print( possiblePath )
		if os.path.exists( possiblePath ):
			return True;
	return False;
hasSVN = checkToolPath( "svn.exe" )
def updateSVN():
	if hasSVN:
		os.system( "svn update " + projectDir )
		os.system( "svn update " + devDir )
	else:
		print( "没有SVN工具或没有添加到环境变量" )
		input( "请手动更新SVN，完成后 <Enter 回车> 继续" )

updateSVN();
#2、编译新版本-----------------------------------------------------
print( "2、编译新版本" )
hasFlexSDK = checkToolPath( "mxmlc.exe" )
def publishRelease():
	if hasFlexSDK:
		os.system( "publish.bat" )
	else:
		print( "没有Flex工具或没有添加到环境变量" )
		input( "请手动导出发行版，完成后 <Enter 回车> 继续" )
publishRelease()
#清理bin-debug目录
def clearDir( dirPath ):
	print( "清理目录：" + dirPath )
	for file in os.listdir( dirPath ):
		filepath = os.path.join( dirPath, file )
		if os.path.isdir( filepath ) and filepath.find( ".svn" ) == -1:
			os.system( "rd /s /q " + filepath )

clearDir( os.path.abspath( os.path.join( projectDir, "bin-debug" ) ) )
clearDir( os.path.abspath( os.path.join( projectDir, "release/swffile/towerDefenseFight" ) ) )

#重新生成缓存目录
topDir = os.path.join( publishDir, str(version) )
os.system( "rd /s /q " + topDir )
os.mkdir( topDir )

#Copy所有文件
def copyFiles():
	dirs = publishDirs.split( "," )
	for dir in dirs:
		print( "Now working in:" + dir )
		fixDir = os.path.join( projectDir, dir )
		for rootDir, assetsDirs, files in os.walk( fixDir ):
			for file in files:
				ext = os.path.splitext( file )[1]
				#print( ext )
				if ext != "" and assets.find( ext ) != -1:
					if dir == "bin-debug" or dir == "bin-release":
						#
						shutil.copy( os.path.join( rootDir, file ), topDir )
					else:
						relPath = os.path.relpath( os.path.join( rootDir, file ), projectDir )
						print( "原始：" + relPath )
						relPath = os.path.join( topDir, relPath )
						print( "新的：" + relPath )
						fileDir = os.path.dirname( relPath )
						print( "FileDir:" + fileDir )
						if not os.path.exists( fileDir ):
							os.makedirs( fileDir )
						shutil.copyfile( os.path.join( rootDir, file ), relPath )
					print( "Copy " + os.path.join( rootDir, file ) + " 完成" )
copyFiles()

def addVersionFix():
	global newFile
	files = os.listdir( topDir )
	for file in files:
		oldFile = os.path.join( topDir, file )
		print( oldFile + "---" + str(os.path.isfile( oldFile )) )
		if os.path.isfile( oldFile ):
			ext = os.path.splitext( file )[1]
			newFile = oldFile.replace( ext, "_" + str(version) + ext )
			os.replace( oldFile, newFile )
			print( "重命名：" + oldFile + "<>"  + newFile )
	#加版本号
	curDir = os.path.join( topDir, ver_config )
	files = os.listdir( curDir )
	for file in files:
		oldFile = os.path.join( curDir, file )
		newFile = os.path.join( curDir, file )
		print( oldFile + "---" + str(os.path.isfile( oldFile )) )
		if os.path.isfile( oldFile ):
			ext = os.path.splitext( file )[1]
			newFile = oldFile.replace( ext, "_" + str(version) + ext )
			os.replace( oldFile, newFile )
			print( "重命名：" + oldFile + "<>"  + newFile )
addVersionFix()

#打版本
arr32 = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", ]

def getcrc32( crc ):
	crc32 = []
	while crc >= 0:
		res =  crc % 32
		crc32.append( arr32[res] )
		if crc <= 0 or crc == res:
			break
		else:
			crc = int((crc - res) / 32)
	crc32.reverse()
	return "".join( crc32 )

def packVersion():
	global newFile
	configfile = newFile
	config = ET.parse( newFile )
	root = config.getroot()
	for folderElement in root:
		folder = folderElement.get( "folder" )
		for item in folderElement:
			file = os.path.join( topDir, folder + item.text )
#			if not os.path.exists( file ):
#				if item.get( "id" ) == "magicUpgradePanel":
#					iitem = root.find( "./swffile/item[@id='magicResearchPanel']" )
#					item.set( "size", iitem.get( "size" ) )
#					item.text = iitem.text
#				continue
			if os.path.exists( file ):
				ext = os.path.splitext( file )[1]
				print( "Processing:" + file )
				fio = io.FileIO( file, "r" )
				fdata = fio.readall()
				fio.close()
				crc = binascii.crc32( fdata ) & 0xFFFFFFFF
				crc = getcrc32( crc )
				item.set( "size", str(os.path.getsize( file )) )
				item.text = item.text.replace( ext, "_" + crc + ext )
				newFile = file.replace( ext, "_" + crc + ext )
				os.replace( file, newFile )
	config.write( configfile )
packVersion()

#复制到DEV目录
def copyToDev():
	for rootDir, assetsDirs, files in os.walk( topDir ):
		for file in files:
			print( "Copying:" + file )
			if os.path.isfile( os.path.join( rootDir, file ) ):
				relPath = os.path.relpath( os.path.join( rootDir, file ), topDir )
				print( "原始：" + relPath )
				relPath = os.path.join( devDir, relPath )
				print( "新的：" + relPath )
				fileDir = os.path.dirname( relPath )
				print( "FileDir:" + fileDir )
				if not os.path.exists( fileDir ):
					os.makedirs( fileDir )
				shutil.copyfile( os.path.join( rootDir, file ), relPath )
				print( "Copy " + os.path.join( rootDir, file ) + " 完成" )
copyToDev()

#提交SVN
def commitSVN():
	print( "提交SVN----------------------------------------------" )
	if hasSVN:
		os.chdir( devDir )
		os.system( "svn add * --force" )
		msg = input( "请输入SVN日志：\n" )
		if ( len(msg) <= 1 ):
			msg = "版本更新，提交测试！"
		os.system( "svn commit -m " + msg )
	else:
		input( "请手动提交SVN后 <Enter 回车> 继续" )
commitSVN()

#上传FTP
print( "上传FTP----------------------------------------------" )
ftp = FTP( "172.17.0.78", "www", "uiEd53do64" )
#ftp = FTP( "127.0.0.1", "jiaox99" )
ftp.cwd( "dev-fb-td.shinezoneapp.com/web/dev_branch/flash" )

def sendFile( allFiles ):
	while len(allFiles) > 0:
		uploadfile = allFiles.pop( 0 )
		print( "Uploading:" + uploadfile )
		relPath = os.path.relpath( uploadfile, topDir ).replace( "\\", "/" )
		fileDir = os.path.dirname( relPath )
		if len(fileDir) > 0:
			try:
				ftp.mkd( fileDir )
			except:
				print( "Dir is ok!" )
		curUploadingFile = open( uploadfile, 'rb' )
		ftp.storbinary( "STOR " + relPath, curUploadingFile )

def collectFiles():
	allFiles = []
	for rootDir, assetsDirs, files in os.walk( topDir ):
		for file in files:
			filePath = os.path.join( topDir, os.path.join( rootDir, file ) )
			allFiles.append( filePath )
	return allFiles

sendFile( collectFiles() )

input( "Well done!版本发布完成！ <Enter 回车> 退出" )


import os
def getAllDirDeep(path):#深度遍历
    stack=[]     #建立一个栈
    stack.append(path)    #将第一个目录进行压栈

    #处理栈，当栈为空的时候结束循环
    while len(stack) != 0:
        # 从栈里取出数据
        dirpath = stack.pop()
        # 目录下所有文件
        fileList=os.listdir(dirpath)

        # 处理一个文件，如果是普通文件则打印出来，如果是目录文件就将该目录的地址压栈
        for fileName in fileList:
            # 获取绝对路径
            absPath = os.path.join(dirpath, fileName)
            if os.path.isdir(absPath):#判断是否是目录
                # 是目录则压栈
                print("目录："+fileName)
                stack.append(absPath)
            else:
                # 不是目录则打印普通文件
                print("普通文件："+fileName)

getAllDirDeep(r"C:\Users\Lenovo\Desktop\数据挖掘实验")

基本语法/目录遍历/栈模拟递归遍历目录(深度遍历).py

栈模拟递归遍历目录(深度遍历).py


from os.path import isfile, join, isdir
import numpy as np
import matplotlib.pyplot as plt

##input: heatmap
##output:
##heatmap+boundingbox->scored box->threshold&IOU/I->(ifframe,tp,#detected,#proposal,num_ca)

gaze_dir_prefix = '/home/yixin/Dropbox/JointAttention/tested_face_direction/test/'
prop_path = '/media/yixin/Elements/bbx_proposal/'
RANGE = 1000

def eval_bbx(heatmap,list_now):

    vid = list_now[0].split('_')[-1][:-4]
    ob_bbx = []
    score = []
    gt = []
    #ini output
    ifframe = np.zeros((1,RANGE))
    tp = np.zeros((1, RANGE))
    fp = np.zeros((1, RANGE))
    true_pos = np.zeros((1,RANGE))
    detected_bbx = np.zeros((1,RANGE))
    num_ca = 0

    ##extract object bbx
    if isfile(gaze_dir_prefix + list_now[0].split('/')[-1][:-4] + '.txt'):
        with open(gaze_dir_prefix + list_now[0].split('/')[-1][:-4] + '.txt', 'r') as gaze_to_read:
            gaze_lines = gaze_to_read.readlines()

        face_tmp = np.zeros(shape=(320, 480))

        for gaze_id in range(len(gaze_lines)):
            gaze_list = gaze_lines[gaze_id].split()

            # cv2.rectangle(img_orig, (int(gaze_list[3]), int(gaze_list[4])),
            #               (int(gaze_list[5]), int(gaze_list[6])), (0, 255, 255))

            g_xmin = int(float(gaze_list[3]))
            g_ymin = int(float(gaze_list[4]))
            g_xmax = int(float(gaze_list[5]))
            g_ymax = int(float(gaze_list[6]))

            ob_bbx.append([g_ymin,g_xmin,g_ymax,g_xmax])

    if isfile(prop_path + vid + '/' + str(int(list_now[0].split('/')[-1].split('_')[0])) + '.txt'):

        # object_tmp = np.zeros(shape=(320, 480))

        with open(prop_path + vid + '/' + str(int(list_now[0].split('/')[-1].split('_')[0])) + '.txt',
                  'r') as f2:
            while True:
                line = f2.readline()
                if len(line) == 0:
                    break
                list = line.split()
                xmin = float(list[0])
                ymin = float(list[1])
                xmax = float(list[2])
                ymax = float(list[3])

                xmin = int(xmin)
                ymin = int(ymin)
                xmax = int(xmax)
                ymax = int(ymax)

                ob_bbx.append([xmin, ymin, xmax, ymax])
    ##score for bbx
    for i in range(len(ob_bbx)):
        a = ob_bbx[i]
        submap = heatmap[a[1]:a[3],a[0]:a[2]]
        score.append(submap.sum()/((a[2]-a[0])*(a[3]-a[1])))

    # max_value = max(score)
    # t = np.array(0.2, dtype=np.float64)
    # max_index = score.index(max_value)
    # score = score/t

    #extract co-attention

    if len(list_now) > 1:

        num_ca = (len(list_now) - 1) / 4
        for ca_id in range(num_ca):
            xmin = float(list_now[4 * ca_id + 1])
            ymin = float(list_now[4 * ca_id + 2])
            xmax = float(list_now[4 * ca_id + 3])
            ymax = float(list_now[4 * ca_id + 4])

            xmin = int(xmin)
            ymin = int(ymin)
            xmax = int(xmax)
            ymax = int(ymax)
            gt.append([xmin, ymin, xmax, ymax])

    ##metric 1: if co-attention is detected correctly in this frame
    score = np.asarray(score)

    for i in range(RANGE):
        judge = score >= float(1)/ float(RANGE) * float(i+1)
        judge = judge.tolist()
        if num_ca != 0:
            detected_bbx[0, i] = judge.count(True)

        if True in judge and num_ca > 0:
            ifframe[0, i] = 1
            tp[0, i] = 1
        if True in judge and num_ca == 0:
            fp[0, i] = 1
        if True not in judge and num_ca == 0:
            ifframe[0, i] = 1

    #metric 2: precision/recall
    if num_ca != 0:

        detect = np.zeros((len(ob_bbx), len(gt)))
        for i in range(len(ob_bbx)):
            a = ob_bbx[i]
            for j in range(len(gt)):
                b = gt[j]
                if bb_if_intersection(a, b):
                    detect[i,j] = 1

        for i in range(RANGE):
            judge = np.where(score >=  float(1)/ float(RANGE) * float(i+1))
            judge = judge[0]
            # print(len(gt))
            # print(len(judge))
            for l in range(len(gt)):
                for j in range(len(judge)):
                    if detect[judge[j], l] >= 1:
                        true_pos[0, i] = true_pos[0, i] + 1
                        break

    return tp, fp, ifframe, true_pos, detected_bbx, num_ca

def bb_intersection_over_union(boxA, boxB):
    # determine the (x, y)-coordinates of the intersection rectangle
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])

    # compute the area of intersection rectangle
    interArea = (xB - xA + 1) * (yB - yA + 1)

    # compute the area of both the prediction and ground-truth
    # rectangles
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)

    # compute the intersection over union by taking the intersection
    # area and dividing it by the sum of prediction + ground-truth
    # areas - the interesection area
    iou = interArea / float(boxAArea + boxBArea - interArea)

    # return the intersection over union value
    return iou

def bb_if_intersection(boxA, boxB):
    hoverlaps = True
    voverlaps = True
    if (boxA[0] > boxB[2]) or (boxA[2] < boxB[0]):#[0]xmin[1]ymin[2]xmax[3]ymax
        hoverlaps = False
    if (boxA[3] < boxB[1]) or (boxA[1] > boxB[3]):
        voverlaps = False
    return hoverlaps and voverlaps


def power(base,exp):
    if(exp==1):
        return(base)
    if(exp==0):
        return(1)
    if(exp>1):
        return(base*power(base,exp-1))

fivePowerThree = power(5,3)
print(fivePowerThree)
#---------------------------------------------
myList = [-4, -6, -5, -1, 2, 3, 7, 9, 88]

def positive(lst):
    newList = list(filter(lambda n: (n >= 0), lst))
    return newList

print(positive(myList))


class Computer:

    def __init__(self):#hm kisi b method ko object k sath call kre skrty han
      self.name="Ibrar"
      self.age=23
    def update(self):
        self.age=993
    def compare(self,c2):   #yahan pe jo self ha wo c1 a rha ha
        if self.age==c2.age:
            return True
        else:
            return False

c1=Computer()  #c1=object name have access fro any variables/function  ,Computer() is acting like a constructor
c2=Computer()
print(c1.name)
print(c2.name)

print()
c2.name="Raja"
c1.update()
print(c1.name)
print(c1.age)

print()
print(c2.name)
print(c2.age)

if  c1.compare(c2):
    print("They are same")
else:
    print("Not same")