from parlai_internal.projects.param_sweep_utils.param_sweep import run_grid
import time
import os

SCRIPT_NAME = os.path.basename(__file__).replace(".py", "")
TODAY = format(time.asctime().replace(":", "-").replace(" ", "_")[:-14])
SWEEP_NAME = f"{SCRIPT_NAME}{TODAY}"

here_path = os.path.realpath(__file__).replace(".py", "")
projects = here_path[here_path.find("/projects") :]
SAVEROOT = "/checkpoint/" + projects + TODAY

HOURS = 23
GPUS = 8

TEACHERS_NO_GSGD_GOOD = [
    "fb:taskmaster1:SystemTeacher",
    "parlai_fb.tasks.taskmaster2.formatted_agents:SystemTeacher",
    "fb:taskmaster3:SystemTeacher",
    "fb:msr_e2e:SystemTeacher",
    "parlai_fb.tasks.taskmaster2.formatted_agents:UserSimulatorTeacher",
    "fb:taskmaster3:UserSimulatorTeacher",
    "fb:msr_e2e:UserSimulatorTeacher",
    "fb:multiwoz_tod:UserSimulatorTeacher",
    "fb:multidogo:UserSimulatorTeacher",
]

TEACHERS_NO_GSGD_FUNKY = [
    "fb:metalwoz_internal:SystemTeacher",  # also without the STANDARD_ whatevers, so could be interesting.
    "fb:multiwoz_tod:SystemTeacher",  # API responses makes no sense
    "fb:multidogo:SystemTeacher",  # API responses make no sense
    "fb:metalwoz_internal:UserSimulatorTeacher",  # also without the STANDARD_ whatevers, so could be interesting.
    "fb:taskmaster1:UserSimulatorTeacher",  # no goals
]

TEACHER_GSGD = [
    "parlai_fb.tasks.google_sgd_rl_splits.agents:InDomainUserSimulatorTeacher",
    "parlai_fb.tasks.google_sgd_rl_splits.agents:InDomainSystemTeacher",
]

ALL_TEACHERS = TEACHER_GSGD + TEACHERS_NO_GSGD_GOOD + TEACHERS_NO_GSGD_FUNKY
ALL_TEACHERS_NO_GSGD = TEACHERS_NO_GSGD_GOOD + TEACHERS_NO_GSGD_FUNKY
ALL_GOOD_TEACHERS = TEACHER_GSGD + TEACHERS_NO_GSGD_GOOD

TEACHER_OPTIONS = [
    ",".join(ALL_TEACHERS),
    #    ",".join(ALL_TEACHERS_NO_GSGD),
    ",".join(ALL_GOOD_TEACHERS),
    #    ",".join(TEACHERS_NO_GSGD_GOOD),
    ",".join(TEACHER_GSGD),
]

print(TEACHER_OPTIONS[0])

# Define param grid
grid = {
    # dataset params
    "-t": TEACHER_OPTIONS,
    "--api-descriptions": [True, False],
    # other params
    "--model": ["parlai_fb.agents.bart.r3f:R3fFirstTurnHistoryRepeatAgent"],
    "--fp16": [True],
    "--label-truncate": [512],
    "--log-every-n-secs": [30],
    "--lr-scheduler": ["invsqrt"],
    "--max-lr-steps": [-1],
    "--max-train-steps": [-1],
    "--optimizer": ["adam"],
    "--save-after-valid": [True],
    "--text-truncate": [512],
    "--warmup-updates": [1000],
    "--fp16-impl": ["mem_efficient"],
    "--gradient-clip": [0.1],
    "--skip-generation": [True],
    "-vp": [8],
    "--max-train-time": [HOURS * 60 * 60 - 30 * 60],
    "--load-from-checkpoint": ["true"],
    "-vmt": ["token_em -vmm max"],
    "--multitask-weights": ["stochastic"],
    # Sweeping params
    "--batchsize": [4],
    "--update-freq": [8],
    "-lr": [1e-4],
    "-vstep": [1000],
}

if __name__ == "__main__":
    run_grid(
        grid=grid,
        name_keys={},
        sweep_name=SWEEP_NAME,
        saveroot=SAVEROOT,
        prefix="python -u -m parlai.scripts.distributed_train",
        partition="learnlab",
        jobtime=f"{HOURS}:00:00",
        gpus=8,
        nodes=1,
        create_model_file=True,
        requeue=True,
        include_job_id=False,
        volta32=True,
        hashname=True,
        mem_gb=400,
        email_updates=True,
        wandb=True,
    )


class Solution:
    def smallestDivisor(self, nums: List[int], threshold: int) -> int:
        def ispossible(mid):
            total = 0
            for x in nums:
                total += math.ceil(x / mid)

            if total <= threshold:
                return True
            return False

        lo, hi = 1, max(nums)
        while lo < hi:
            mid = (lo + hi) // 2

            if ispossible(mid):
                hi = mid
            else:
                lo = mid + 1
        return lo

1283. Find the Smallest Divisor Given a Threshold.py


# 2016년 요일 찾기
# 2016년 1월 1일은 금요일
# SUN,MON,TUE,WED,THU,FRI,SAT
from datetime import datetime

def solution(a, b):
    date = '2016-{0}-{1}'.format(a, b) # 날짜
    datetime_date = datetime.strptime(date, '%Y-%m-%d') # 날짜의 타입을 datetime형으로 변경
    dateDict = {0: 'MON', 1:'TUE', 2:'WED', 3:'THU', 4:'FRI', 5:'SAT', 6:'SUN'}
    return dateDict[datetime_date.weekday()]

# 1년 중 첫 시작 요일을 알기 때문에
# 이전 달까지 모두 더하고, 일 수를 더한 후 7로 나눠주면 요일을 알 수 있다.
# 조건이 1년 내이고, 첫번째 날의 요일을 알기 때문에 이런 연산 방법이 더 적합할 것 같다.
def solution1(a, b):
    months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    days = ['FRI', 'SAT', 'SUN', 'MON', 'TUE', 'WED', 'THU']
    return days[(sum(months[:a-1])+b-1)%7]

print(solution(5,24))


import sys
import base64, time, datetime

callbacks = {
    'array': lambda x: [v.text for v in x],
    'dict': lambda x:
        dict((x[i].text, x[i+1].text) for i in range(0, len(x), 2)),
    'key': lambda x: x.text or "",
    'string': lambda x: x.text or "",
    'data': lambda x: base64.b64decode(x.text),
    'date': lambda x:
        datetime.datetime(
            *(time.strptime(x.text, "%Y-%m-%dT%H:%M:%SZ")[0:6])),
    'real': lambda x: float(x.text),
    'integer': lambda x: int(x.text),
    'true': lambda x: True,
    'false': lambda x: False,
}

def _xml_plist_parse(xml_input, _iterparse):
    parser = _iterparse(xml_input)
    for action, element in parser:
        callback = callbacks.get(element.tag)
        if callback:
            data = callback(element)
            element.clear()
            element.text = data
        elif element.tag != 'plist':
            raise IOError("unknown plist tag: %s" % element.tag)
    return parser.root[0].text

def parse_using_etree(xml_input):
    from xml.etree.ElementTree import iterparse as py_iterparse
    _xml_plist_parse(xml_input, py_iterparse)

def parse_using_cetree(xml_input):
    import xml.etree.cElementTree
    from xml.etree.cElementTree import iterparse as c_iterparse
    _xml_plist_parse(xml_input, c_iterparse)

if __name__ == '__main__':
    xmlin = open(sys.argv[1])
    try:
        assert parse_using_cetree(xmlin)
    finally:
        xmlin.close()


from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm

def register(request):
    if request.method =='POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'User {username} has been successfully created')
            return redirect('all-goods')
    else:
        form = UserRegisterForm()
    return render(
        request,
        'users/registration.html',
        {'title': 'Registration page',
         'form': form,
         })


def adding():

    file = open("list", 'a')
    user_input = input("Enter the item you want to add: ")
    file.writelines(f"{user_input}\n")
    file.close()
    print("\nSuccessfully added to the list")

def viewing():

    file = open("list", 'r')
    r = file.read()
    print(f"\n{r}")

def delete():

    input_ = input("Enter the item you want to delete: ")
    file = open("list", 'r')
    lst = file.readlines()
    new_file = open("list", 'w')

    for item in lst:
        if item != f"{input_}\n":
            new_file.writelines(item)

    print("\nSuccessfully deleted from the list")

def what_to_do():

    input_ = input("Enter 'a' to add an item; Enter 'd' to delete and item; Enter 'v' to view the list\n")

    if input_ == "a":
        print(adding())
    elif input_ == "d":
        print(delete())
    elif input_.lower() == "v":
        print(viewing())

what_to_do()


############################################################
############################################################
#Reverse a linked list from position m to n. Do it in-place and in one-pass.
#
#For example:
#Given 1->2->3->4->5->NULL, m = 2 and n = 4,
#
#return 1->4->3->2->5->NULL.
############################################################
############################################################

# Definition for singly-linked list.
# class ListNode:
#    def __init__(self, x):
#        self.val = x
#        self.next = None

class Solution:
    # @param A : head node of linked list
    # @param B : integer
    # @param C : integer
    # @return the head node in the linked list
    def reverseBetween(self, A, B, C):
        p,q = A,A
        current = A
        value1,value2 = 1,1
        if B==C:
            return A
        while value1<B-1:
            p = p.next
            value1+=1
        while value2<C:
            q = q.next
            value2+=1
        if B==1:
            while current != q:
                temp = current
                current = current.next
                temp.next = q.next
                q.next = temp
                A = current
        else:
            while p.next != q:
                temp = p.next
                p.next = p.next.next
                temp.next = q.next
                q.next = temp
        return A


# load the example image and convert it to grayscale
import os

import cv2
import pytesseract

image = "example_01.jpg"
preprocess = "thresh"

image = cv2.imread(image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# check to see if we should apply thresholding to preprocess the
# image
if preprocess == "thresh":
    gray = cv2.threshold(gray, 0, 255,
                         cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# make a check to see if median blurring should be done to remove
# noise
elif preprocess == "blur":
    gray = cv2.medianBlur(gray, 3)

# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)

# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
text = pytesseract.image_to_string(gray)
os.remove(filename)
print(text)

# show the output images
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)


import cv2
import numpy as np
import argparse

def main():

    parser = argparse.ArgumentParser()

    parser.add_argument('-v','--video', type=str)
    parser.add_argument('-o','--output', type=str, default=None)

    args = parser.parse_args()

    vid = cv2.VideoCapture(args.video)

    width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(vid.get(cv2.CAP_PROP_FPS))
    codec = cv2.VideoWriter_fourcc('a','v','c','1')
    out = cv2.VideoWriter(args.output, codec, fps, (width, height))

    while vid.isOpened():
        success, image = vid.read()
        if not success:
            print("ignoring empty video")
            break

        cv2.imshow("before", image)

        max_x, max_y, z = image.shape
        # print('image dimensions: x:', max_x, "by y:", max_y)

        # points are (y, x), thickness -1 for solid
        start_point = (154, 170)
        end_point = (500, 1200)
        # setting for 720x1280 (portrait) handstand video at ../add_noise/sleeves.mp4

        # draw red rectangle around area
        # line_color= (0, 0, 255)
        # line_thickness = 3
        # cv2.rectangle(image, start_point, end_point, line_color, line_thickness)

        # exclude area outside rectangle
        start_y, start_x = start_point
        end_y, end_x = end_point
        mask = np.zeros(image.shape[:2],np.uint8)
        mask[start_x:end_x,start_y:end_y] = 255
        image = cv2.bitwise_and(image,image,mask = mask)

        out.write(image)
        cv2.imshow("after", image)
        if cv2.waitKey(1) & 0xFF == 27:
            break
    vid.release()

if __name__ == "__main__":
    main()

# https://stackoverflow.com/questions/11492214/opencv-via-python-is-there-a-fast-way-to-zero-pixels-outside-a-set-of-rectangle
# img = cv2.imread('testimg.jpeg')
# start_x = 30
# start_y = 30
# end_x = 200
# end_y = 100

# mask = np.zeros(img.shape[:2],np.uint8)
# mask[start_y:start_y+end_y,start_x:start_x+end_x] = 255
# result = cv2.bitwise_and(img,img,mask = mask)
# cv2.imshow("result", result)


import argparse
import glob
import logging
import os
import random
import timeit

import numpy as np
import torch
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from torch.utils.data.distributed import DistributedSampler
from tqdm import tqdm, trange

from transformers import (
    WEIGHTS_NAME,
    AdamW,
    AlbertConfig,
    AlbertForQuestionAnswering,
    AlbertTokenizer,
    #BertConfig,
    #BertForQuestionAnswering,
    #BertForSequenceClassification,
    #BertTokenizer,
    RobertaConfig,
    RobertaForQuestionAnswering,
    RobertaTokenizer,
    get_linear_schedule_with_warmup,
    openqa_convert_examples_to_features,
)
from transformers.data.metrics.squad_metrics import (
    compute_predictions_log_probs,
    compute_predictions_logits,
    squad_evaluate,
)
from transformers.data.processors.openqa import OpenQAResult, OpenQAV1Processor, OpenQAV2Processor

try:
    from torch.utils.tensorboard import SummaryWriter
except ImportError:
    from tensorboardX import SummaryWriter

## added by Jong-Hoon Oh
import torchtext
import cnn_utils
import train_utils

NUM_PARALLEL_EXEC_UNITS = 4
os.environ['OMP_NUM_THREADS'] = str(NUM_PARALLEL_EXEC_UNITS)
os.environ["KMP_AFFINITY"] = "granularity=fine,verbose,compact,1,0"
os.environ['KMP_WARNINGS'] = 'off'

logger = logging.getLogger(__name__)

ALL_MODELS = sum(
    (tuple(conf.pretrained_config_archive_map.keys()) for conf in (AlbertConfig, RobertaConfig,)),
    (),
)

MODEL_CLASSES = {
    "albert": (AlbertConfig, AlbertForQuestionAnswering, AlbertTokenizer),
    "roberta": (RobertaConfig, RobertaForQuestionAnswering, RobertaTokenizer),
}

## added by Jong-Hoon Oh
class TTDataset(torchtext.data.Dataset):
    '''Dummy Dataset for build_vocab'''
    def __init__(self, words, fields):
        data_fields = [('text', fields['text'])]
        ex = (words,)
        examples = [torchtext.data.Example.fromlist(ex, data_fields)]
        super(TTDataset, self).__init__(examples, data_fields)

## added by Jong-Hoon Oh
def load_cnn_model_and_vocab(args, cnn_file, words):
    assert args.emb_file and args.min_freq

    fields = cnn_utils.get_fields()
    train_utils.build_vocab(args, fields, TTDataset(words, fields), [])
    vocab = fields['text'].vocab
    model, pre_fields = train_utils.load_cnn_model(args, cnn_file, fields)

    return model, pre_fields['text'].vocab.stoi

def set_seed(args):
    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if args.n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed)

def to_list(tensor):
    return tensor.detach().cpu().tolist()

# modified by Jong-Hoon Oh
# DATA PROCESSING PART
# - Converting input examples to cached examples
# - cnn_stoi: vocab.stoi for the cnn model
def load_and_cache_examples(args, filename, tokenizer, cnn_stoi, evaluate=False, output_examples=False):
    if args.local_rank not in [-1, 0] and not evaluate:
        # Make sure only the first process in distributed training process the dataset, and the others will use the cache
        torch.distributed.barrier()

    bert_token_str = "ot0"
    input_dir = args.feat_dir if args.feat_dir else "."
    fstem =  list(filter(None,filename.split("/"))).pop()
    fstem =  fstem.split(".")[0]
    fstem =  fstem
    cached_file = "cached_{}_{}_{}_{}_{}_{}".format(
            fstem,
            list(filter(None, args.model_name_or_path.split("/"))).pop(),
            args.cnn_stem,
            list(filter(None, args.cnn_model.split("_"))).pop(),
            bert_token_str,
            str(args.max_seq_length),
        )
    # split the input data into data, positive_data, feature, and example
    dset_dir = input_dir + '/dset'
    pdset_dir = input_dir + '/pdset'
    feat_dir = input_dir + '/feat'
    exset_dir = input_dir + '/exset'
    cached_dset_file = os.path.join(dset_dir,cached_file)
    cached_feat_file = os.path.join(feat_dir,cached_file)
    cached_pdset_file = os.path.join(pdset_dir,cached_file)
    cached_exset_file = os.path.join(exset_dir,cached_file)
    if evaluate:
       logger.info("Specified cached file %s for dev or predict files", cached_dset_file)
    else:
       logger.info("Specified cached file %s for train files", cached_dset_file)

    # Init features and dataset from cache if it exists
    if os.path.exists(cached_dset_file) and not args.overwrite_cache:
        logger.info("Feature files already exist: %s", cached_dset_file)
    else:
        logger.info("Creating features from dataset file at %s", input_dir) # input_dir="." by defaults

        # if no predict file for evaluation or no train file for training
        if not args.data_dir and ((evaluate and not args.predict_file) or (not evaluate and not args.train_file)):
            try:
                import tensorflow_datasets as tfds
            except ImportError:
                raise ImportError("If not data_dir is specified, tensorflow_datasets needs to be installed.")

            if args.version_2_with_negative:
                logger.warn("tensorflow_datasets does not handle version 2 of SQuAD.")

            tfds_examples = tfds.load("openqa")
            examples = OpenQAV1Processor().get_examples_from_dataset(tfds_examples, evaluate=evaluate)
        else:
            # The main part of data processing in our OpenQA experiments
            processor = OpenQAV1Processor()

            if evaluate:
                # initializer
                examples = processor.get_dev_examples(args.data_dir, filename=filename)
            else:
                # initializer
                examples = processor.get_train_examples(args.data_dir, filename=filename)

        features, dataset, possible_dataset = openqa_convert_examples_to_features(
            examples=examples,
            tokenizer=tokenizer,
            cnn_stoi=cnn_stoi,
            max_seq_length=args.max_seq_length,
            doc_stride=args.doc_stride,
            max_query_length=args.max_query_length,
            is_training=not evaluate,
            return_dataset="pt", # "pt" represents 'pytorch dataset'
            threads=args.threads,
        )

        if args.local_rank in [-1, 0]:
            logger.info("Saving features into cached file %s", cached_dset_file)
            if evaluate:
               logger.info("dataset:{}".format(len(dataset)))
               torch.save({"dataset": dataset}, cached_dset_file)
               logger.info("features")
               torch.save({"features": features}, cached_feat_file)
               logger.info("examples")
               torch.save({"examples": examples}, cached_exset_file)
            else:
               logger.info("dataset:{}".format(len(dataset)))
               torch.save({"dataset": dataset}, cached_dset_file)
               logger.info("possible_dataset:{}".format(len(possible_dataset)))
               torch.save({"possible_dataset": possible_dataset}, cached_pdset_file)

    if args.local_rank == 0 and not evaluate:
        # Make sure only the first process in distributed training process the dataset, and the others will use the cache
        torch.distributed.barrier()

def main():
    parser = argparse.ArgumentParser()

    # Required parameters
    parser.add_argument(
        "--model_type",
        default=None,
        type=str,
        required=True,
        help="Model type selected in the list: " + ", ".join(MODEL_CLASSES.keys()),
    )
    parser.add_argument(
        "--model_name_or_path",
        default=None,
        type=str,
        required=True,
        help="Path to pre-trained model or shortcut name selected in the list: " + ", ".join(ALL_MODELS),
    )
    parser.add_argument(
        "--prep_vocab_file",
        default=None,
        type=str,
        help="preprocessed_vocab_file with the train/dev/predict file. see make_openqa_cnn_vocab.py",
    )
    parser.add_argument(
        "--emb_file",
        default=None,
        type=str,
        help="The embedding vector file used for cnn",
    )
    parser.add_argument(
        "--cnn_model",
        default=None,
        type=str,
        help="The cnn model file name",
    )
    parser.add_argument(
        "--cnn_stem",
        default="enwiki",
        type=str,
        help="stem for cnn models for caching (different vocab.stoi for each model)",
    )
    parser.add_argument(
        "--min_freq",
        default=5,
        type=int,
        help="min freq. for unknown words",
    )
    parser.add_argument(
        "--emb_dim",
        default=300,
        type=int,
        help="dim for representation of fastText",
    )

    # Other parameters
    parser.add_argument(
        "--data_dir",
        default=None,
        type=str,
        help="The input data dir. Should contain the .json files for the task."
        + "If no data dir or train/predict files are specified, will run with tensorflow_datasets.",
    )
    parser.add_argument(
        "--train_file",
        default=None,
        type=str,
        help="The input training file. If a data dir is specified, will look for the file there"
        + "If no data dir or train/predict files are specified, will run with tensorflow_datasets.",
    )
    parser.add_argument(
        "--predict_file",
        default=None,
        type=str,
        help="The input evaluation file. If a data dir is specified, will look for the file there"
        + "If no data dir or train/predict files are specified, will run with tensorflow_datasets.",
    )
    parser.add_argument(
        "--dev_file",
        default=None,
        type=str,
        help="The input development file. If a data dir is specified, will look for the file there"
        + "If no data dir or devel files are specified, will run with tensorflow_datasets.",
    )
    parser.add_argument(
        "--config_name", default="", type=str, help="Pretrained config name or path if not the same as model_name"
    )
    parser.add_argument(
        "--tokenizer_name",
        default="",
        type=str,
        help="Pretrained tokenizer name or path if not the same as model_name",
    )
    parser.add_argument(
        "--cache_dir",
        default="",
        type=str,
        help="Where do you want to store the pre-trained models downloaded from s3",
    )
    parser.add_argument(
        "--feat_dir",
        default="",
        type=str,
        help="Where do you want to store the processed data whose features were extracted from the input data",
    )

    parser.add_argument(
        "--max_seq_length",
        default=384,
        type=int,
        help="The maximum total input sequence length after WordPiece tokenization. Sequences "
        "longer than this will be truncated, and sequences shorter than this will be padded.",
    )
    parser.add_argument(
        "--doc_stride",
        default=128,
        type=int,
        help="When splitting up a long document into chunks, how much stride to take between chunks.",
    )
    parser.add_argument(
        "--max_query_length",
        default=64,
        type=int,
        help="The maximum number of tokens for the question. Questions longer than this will "
        "be truncated to this length.",
    )
    parser.add_argument(
        "--do_lower_case", action="store_true", help="Set this flag if you are using an uncased model."
    )

    parser.add_argument(
        "--max_answer_length",
        default=30,
        type=int,
        help="The maximum length of an answer that can be generated. This is needed because the start "
        "and end predictions are not conditioned on one another.",
    )
    parser.add_argument(
        "--verbose_logging",
        action="store_true",
        help="If true, all of the warnings related to data processing will be printed. "
        "A number of warnings are expected for a normal SQuAD evaluation.",
    )
    parser.add_argument(
        "--lang_id",
        default=0,
        type=int,
        help="language id of input for language-specific xlm models (see tokenization_xlm.PRETRAINED_INIT_CONFIGURATION)",
    )

    parser.add_argument("--no_cuda", action="store_true", help="Whether not to use CUDA when available")
    parser.add_argument(
        "--overwrite_cache", action="store_true", help="Overwrite the cached training and evaluation sets"
    )
    parser.add_argument("--seed", type=int, default=42, help="random seed for initialization")

    parser.add_argument("--local_rank", type=int, default=-1, help="local_rank for distributed training on gpus")
    parser.add_argument(
        "--fp16",
        action="store_true",
        help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit",
    )
    parser.add_argument(
        "--fp16_opt_level",
        type=str,
        default="O1",
        help="For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']."
        "See details at https://nvidia.github.io/apex/amp.html",
    )
    parser.add_argument("--server_ip", type=str, default="", help="Can be used for distant debugging.")
    parser.add_argument("--server_port", type=str, default="", help="Can be used for distant debugging.")

    parser.add_argument("--threads", type=int, default=1, help="multiple threads for converting example to features")
    args = parser.parse_args()

    assert args.prep_vocab_file is not None
    assert args.cnn_model is not None
    assert args.cnn_stem is not None
    assert args.emb_dim is not None
    assert args.emb_file is not None

    if (not os.path.exists(args.prep_vocab_file)):
        raise ValueError(
            "prep_vocab_file ({}) does not exist. Check the --prep_vocab_file option.".format( args.prep_vocab_file) )

    if (not os.path.exists(args.cnn_model)):
        raise ValueError(
            "cnn_model ({}) does not exist. Check the --cnn_model option.".format( args.cnn_model) )

    if (not os.path.exists(args.emb_file)):
        raise ValueError(
            "emb_file ({}) does not exist. Check the --emb_file option.".format( args.emb_file) )

    if args.doc_stride >= args.max_seq_length - args.max_query_length:
        logger.warning(
            "WARNING - You've set a doc stride which may be superior to the document length in some "
            "examples. This could result in errors when building features from the examples. Please reduce the doc "
            "stride or increase the maximum length to ensure the features are correctly built."
        )

    # Setup CUDA, GPU & distributed training
    if args.local_rank == -1 or args.no_cuda:
        device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
        args.n_gpu = torch.cuda.device_count()
    else:  # Initializes the distributed backend which will take care of sychronizing nodes/GPUs
        torch.cuda.set_device(args.local_rank)
        device = torch.device("cuda", args.local_rank)
        torch.distributed.init_process_group(backend="nccl")
        args.n_gpu = 1
    args.device = device

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO if args.local_rank in [-1, 0] else logging.WARN,
    )
    logger.warning(
        "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s",
        args.local_rank,
        device,
        args.n_gpu,
        bool(args.local_rank != -1),
        args.fp16,
    )

    # Set seed
    set_seed(args)

    # Load pretrained model and tokenizer
    if args.local_rank not in [-1, 0]:
        # Make sure only the first process in distributed training will download model & vocab
        # The barrier starts
        torch.distributed.barrier()

    # added by Jong-Hoon Oh
    #  - Load cnn model and pre-processed vocab.
    #  - prep_vocab_file: see vocab/
    prep_tokens = torch.load(args.prep_vocab_file)
    all_tokens = prep_tokens['tokens']
    cnn_model, cnn_stoi = load_cnn_model_and_vocab(args, args.cnn_model, all_tokens)
    cnn_dim = len(cnn_model.args.filter_widths) * cnn_model.args.filter_size
    args.cnn_dim = cnn_dim

    args.model_type = args.model_type.lower()
    # "albert": (AlbertConfig, AlbertForQuestionAnswering, AlbertTokenizer),
    config_class, model_class, tokenizer_class = MODEL_CLASSES[args.model_type]
    config = config_class.from_pretrained(
        args.config_name if args.config_name else args.model_name_or_path,
        cache_dir=args.cache_dir if args.cache_dir else None,
    )
    config.num_of_TIERs = 3
    config.cnn_dim = args.cnn_dim
    config.emb_dim = args.emb_dim
    config.cnn_model = args.cnn_model
    config.cnn_stem = args.cnn_stem
    # tokenizer_class: AlbertTokenizer
    tokenizer = tokenizer_class.from_pretrained(
        args.tokenizer_name if args.tokenizer_name else args.model_name_or_path,
        do_lower_case=args.do_lower_case,
        cache_dir=args.cache_dir if args.cache_dir else None,
    )
    # model_class: AlbertForQuestionAnswering
    model = model_class.from_pretrained(
        args.model_name_or_path,
        from_tf=bool(".ckpt" in args.model_name_or_path), # ckpt: tensorflow file, pt: pytorch file
        config=config,
        cache_dir=args.cache_dir if args.cache_dir else None,
    )
    ###########

    if args.local_rank == 0:
        # Make sure only the first process in distributed training will download model & vocab
        # The barrier ends
        torch.distributed.barrier()

    model.to(args.device)
    cnn_model.to(args.device)

    logger.info("Training/evaluation parameters %s", args)

    # Before we do anything with models, we want to ensure that we get fp16 execution of torch.einsum if args.fp16 is set.
    # Otherwise it'll default to "promote" mode, and we'll get fp32 operations. Note that running `--fp16_opt_level="O2"` will
    # remove the need for this code, but it is still valid.
    if args.fp16:
        try:
            import apex

            apex.amp.register_half_function(torch, "einsum")
        except ImportError:
            raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
    if args.train_file is not None:
       load_and_cache_examples(args, args.train_file, tokenizer, cnn_stoi, evaluate=False, output_examples=False)
    if args.predict_file is not None:
       load_and_cache_examples(args, args.predict_file, tokenizer, cnn_stoi, evaluate=True, output_examples=True)
    if args.dev_file is not None:
       load_and_cache_examples(args, args.dev_file, tokenizer, cnn_stoi, evaluate=True, output_examples=True)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3
"""this module contains a function for task 2"""
import numpy as np

def nparser(sentence, n):
    """nparser - parses sentence in to n partitions"""
    uniq_words = []
    for i in range(len(sentence)):
        if i + n <= len(sentence):
            uniq_words.append(str(sentence[i:i+n]))
    return uniq_words

def ngram_bleu(references, sentence, n):
    """this function gives us the unigram bleu score"""
    total = len(sentence)
    lref = min([len(i) for i in references])

    parsed_sent = nparser(sentence, n)
    # print(parsed_sent)

    parsed_tot = len(parsed_sent)

    parsed_ref = []
    for i in references:
        parsed_ref.append(nparser(i, n))
    # print(parsed_ref)

    uniq_ref = {}
    for i in parsed_ref:
        for x in i:
            if x not in uniq_ref:
                uniq_ref[x] = max([l.count(x) for l in parsed_ref])
    # print("ur", uniq_ref)

    uniq_words = {}
    for phrase in parsed_sent:
        # print("phrase ->", phrase)
        if phrase not in uniq_words and phrase in uniq_ref:
            if parsed_sent.count(phrase) >= uniq_ref[phrase]:
                uniq_words[phrase] = uniq_ref[phrase]
            else:
                uniq_words[phrase] += 1
    find = sum(uniq_words.values())
    # print(find)
    # print(uniq_words)
    bp = 1 if total >= lref else np.exp(1 - (lref/total))
    # print("bp->", bp)
    # print(find, total)
    return find/parsed_tot

def cumulative_bleu(references, sentence, n):
    """computes the cumulative bleu score"""
    score_hold = []
    bp_hold = []
    lref = min([len(i) for i in references])
    total = len(sentence)
    for i in range(n):
        score_hold.append(ngram_bleu(references, sentence, i + 1))
    # print(score_hold)
    # print(np.average(score_hold))
    bp = 1 if total >= lref else np.exp(1 - (lref/total))
    # print("bp ->", bp)
    return np.exp(np.average(np.log(score_hold))) * bp


"""
@创建日期 ：2022/4/25
@修改日期 ：2022/4/26
@作者 ：jzj
@功能 ：模型库，输出统一以字典格式
       dqn 输出 value
       a2c 输出 policy value
       fixme: 可能会抽象为参数构建的模式，不确定
"""

from typing import List
import tensorflow as tf
import tensorflow.keras.layers as layers
import tensorflow.keras.models as models

def make_model(id, args):
    if id == "cartpole_dqn":
        return CartPoleDQN(**args)
    elif id == "cartpole_a2c":
        return CartPoleA2C(**args)
    elif id == "flappybirdsimple_dqn":
        return FlappyBirdSimpleDqn(**args)
    elif id == "flappybirdsimple_a2c":
        return FlappyBirdSimpleA2C(**args)
    else:
        raise NotImplementedError

class ModelWrapper(models.Model):
    """fixme: dev ing"""
    def __init__(self, model):
        super(ModelWrapper, self).__init__()
        self.model = model

    def call(self, inputs):
        return self.model(inputs)

    def inference(self, x):
        if hasattr(self.model, "inference"):
            return self.model.inference(x)

        x = tf.expand_dims(x, 0)
        outputs = self.call(x)
        return outputs

# CartPole DQN
class CartPoleDQN(models.Model):
    def __init__(self, action_dim, hidden_dims: List):
        super(CartPoleDQN, self).__init__()
        self.input_layers = layers.InputLayer(input_shape=(4,))
        self.hidden_layers = []
        for hidden_dim in hidden_dims:
            self.hidden_layers.append(layers.Dense(hidden_dim, activation="tanh"))
        self.output_layer = layers.Dense(action_dim)

    def call(self, inputs):
        x = self.input_layers(inputs)
        for layer in self.hidden_layers:
            x = layer(x)
        x = self.output_layer(x)
        return {"value": x}

# CartPole A2C
class CartPoleA2C(tf.keras.Model):
    def __init__(self, num_action=2, num_hidden_units=128):
        super(CartPoleA2C, self).__init__()
        self.common = layers.Dense(num_hidden_units, activation=None)
        self.activation = layers.ReLU()
        self.actor = layers.Dense(num_action)
        self.critic = layers.Dense(1)

    def call(self, inputs: tf.Tensor):
        x = self.common(inputs)
        x = self.activation(x)
        return {"policy": self.actor(x), "value": self.critic(x)}

# FlappyBirdRGB A2C
class ConvBlock(layers.Layer):
    def __init__(self, filter, kernel_size, stride=1):
        super(ConvBlock, self).__init__()
        self.conv = layers.Conv2D(filter, kernel_size, stride, padding="same")
        self.bn = layers.BatchNormalization()
        self.activation = layers.ReLU()

    def call(self, inputs):
        return self.activation(self.bn(self.conv(inputs)))

class ResidualBlock(layers.Layer):
    def __init__(self, filter, kernel_size, stride, squeeze_factor, se=False):
        """fixme: 添加Se支持"""
        super(ResidualBlock, self).__init__()
        self.conv_block1 = ConvBlock(filter//squeeze_factor, kernel_size, stride)
        self.conv_block2 = ConvBlock(filter, kernel_size, stride)
        self.short_cut = ConvBlock(filter, 1)
        self.output_bn = layers.BatchNormalization()
        self.output_ac = layers.ReLU()

    def call(self, inputs):
        x = self.conv_block1(inputs)
        x = self.conv_block2(x)
        x = x + self.short_cut(inputs)
        x = self.output_ac(self.output_bn(x))
        return x

class PolicyHead(layers.Layer):
    def __init__(self, policy_dim):
        super(PolicyHead, self).__init__()
        self.conv = layers.Conv2D(1, kernel_size=3, strides=1, padding="same")
        self.bn = layers.BatchNormalization()
        self.dense = layers.Dense(policy_dim)

    def call(self, inputs):
        b, h, w, c = inputs.shape
        x = self.bn(self.conv(inputs))
        x = tf.reshape(x, (-1, h*w))
        x = self.dense(x)
        return x

class ValueHead(layers.Layer):
    def __init__(self, value_dim):
        super(ValueHead, self).__init__()
        self.conv = layers.Conv2D(1, kernel_size=3, strides=1, padding="same")
        self.bn = layers.BatchNormalization()
        self.dense = layers.Dense(value_dim)

    def call(self, inputs):
        b, h, w, c = inputs.shape
        x = self.bn(self.conv(inputs))
        x = tf.reshape(x, (-1, h*w))
        x = self.dense(x)
        return x

class FlappyBirdA2C(models.Model):
    """
    简单模型，注意policy输出的是logit值为非概率
    """
    def __init__(self, filters=[32, 64, 128], blocks=[2, 2, 4]):
        super(FlappyBirdA2C, self).__init__()
        self.conv1 = layers.Conv2D(32, 5, 2, padding="same")
        self.bn1 = layers.BatchNormalization()
        self.ac1 = layers.ReLU()
        self.pool1 = layers.MaxPooling2D(pool_size=3, strides=2, padding="same")

        self.middle_layers = []

        for filter, block in zip(filters, blocks):
            for n in range(block):
                self.middle_layers.append(ResidualBlock(filter, 3, 1, 4))
            self.middle_layers.append(layers.MaxPooling2D(pool_size=3, strides=2, padding="same"))

        self.policy_head = PolicyHead(policy_dim=2)
        self.value_head = ValueHead(value_dim=1)

    def call(self, inputs):
        x = self.pool1(self.ac1(self.bn1(self.conv1(inputs))))
        for layer in self.middle_layers[:-1]:
            x = layer(x)
        policy = self.policy_head(x)
        value = self.value_head(x)
        return {"policy": policy, "value": value}

# FlappyBirdSimple A2C
class FlappyBirdSimpleA2C(models.Model):
    def __init__(self, policy_dim=2, value_dim=1, hidden_dims=[32, 64]):
        super(FlappyBirdSimpleA2C, self).__init__()
        self.input_layers = layers.InputLayer(input_shape=(2,))
        self.hidden_layers = []
        for hidden_dim in hidden_dims:
            self.hidden_layers.append(layers.Dense(hidden_dim, activation="tanh",))

        self.policy_head = layers.Dense(policy_dim)
        self.value_head = layers.Dense(value_dim)

    def call(self, inputs):
        x = self.input_layers(inputs)
        for layer in self.hidden_layers:
            x = layer(x)
        policy = self.policy_head(x)
        value = self.value_head(x)
        return {"policy": policy, "value": value}

# FlappyBirdSimple DQN
class FlappyBirdSimpleDqn(models.Model):
    def __init__(self, value_dim=2, hidden_dims=[256, 256]):
        super(FlappyBirdSimpleDqn, self).__init__()
        self.input_layers = layers.InputLayer(input_shape=(2,))
        self.hidden_layers = []
        for hidden_dim in hidden_dims:
            self.hidden_layers.append(layers.Dense(hidden_dim, activation="tanh"))

        self.value_head = layers.Dense(value_dim)

    def call(self, inputs):
        x = self.input_layers(inputs)
        for layer in self.hidden_layers:
            x = layer(x)
        value = self.value_head(x)
        return {"value": value}


import pytest
from sqlalchemy import create_engine

from rebrickable.data.database import Session
from rebrickable.data.models import *

models = [Color, Inventory, InventorySet,
          InventoryPart, Part, PartCategory, Set, Theme]

@pytest.fixture(scope='module')
def session():
    engine = create_engine('sqlite:///:memory:', echo=True)
    Session.configure(bind=engine)
    # You probably need to create some tables and
    # load some test data, do so here.

    # To create tables, you typically do:
    Base.metadata.create_all(engine)
    yield Session()
    Session.close_all()

@pytest.fixture
def objects():
    return [
        Color(id=1, name='black', rgb='123456', is_trans=True),
        Inventory(id=1, version=42, set_num='7189-1'),
        InventoryPart(inventory_id=1, part_num='3001', color_id=1, quantity=12),
        InventorySet(inventory_id=1, set_num='7189-1', quantity=1),
        Part(part_num='3001', name='Brick 2X4', part_cat_id=1),
        PartCategory(id=1, name='bricks'),
        Set(set_num='7189-1', name='Dumy Test', year=2015, theme_id=42, num_parts=12),
        Theme(id=42, name='Town', parent_id=None),
        Theme(id=43, name='Police', parent_id=42)
    ]

def test_models(session, objects):
    session.add_all(objects)

    session.commit()

    for obj in objects:
        session.refresh(obj)
        print(obj)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""--------------------------------------------------------------------
GENETIC ALGORITHMS EXPERIMENTS
Started on the 2018/01/03
theo.alves.da.costa@gmail.com
https://github.com/theolvs
------------------------------------------------------------------------
"""

from scipy import stats
import seaborn as sns
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import sys
import time
from tqdm import tqdm
import itertools

#=============================================================================================================================
# DISTRIBUTIONS
#=============================================================================================================================

class Dist(object):
    def __init__(self,mu = None,std = None,label = None):
        self.mu = np.random.rand()*20 - 10 if mu is None else mu
        self.std = np.random.rand()*10 if std is None else std
        self.label = "" if not label else " - "+label
        self.func = lambda x : stats.norm.cdf(x,loc = self.mu,scale = self.std)

    def __repr__(self,markdown = False):
        return "Norm {1}mu={2}{0}, {0}std={3}{0}{4}".format("$" if markdown else "","$\\" if markdown else "",
                                                             round(self.mu,2),round(self.std,2),self.label)

    def plot(self,fill = True):
        x = np.linspace(-20, 20, 100)
        y = stats.norm.pdf(x,loc = self.mu,scale = self.std)
        plt.plot(x,y,label = self.__repr__(markdown = True))
        if fill:
            plt.fill_between(x, 0, y, alpha=0.4)

    def __add__(self,other):
        mu = np.mean([self.mu,other.mu])
        std = np.mean([self.std,other.std])
        return Dist(mu,std)

    def mutate(self,alpha = 1):
        self.mu = self.mu + 1/(1+np.log(1+alpha)) * np.random.randn()
        self.std = max(self.std + 1/(1+np.log(1+alpha)) * np.random.randn(),0.5)

    def fitness(self,x):
        return 1 - stats.kstest(x,self.func).statistic

class Population(object):
    def __init__(self,distributions = None,n = 100):
        if distributions is not None:
            self.distributions = distributions
        else:
            self.distributions = [Dist() for i in range(n)]

    def __getitem__(self,key):
        if type(key) == tuple or type(key) == list:
            d = []
            for i in key:
                d.append(self.distributions[i])
            return d
        else:
            return self.distributions[key]

    def __iter__(self):
        return iter(self.distributions)

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

    def plot(self,title = "Normal distributions",figsize = None):
        if figsize:
            plt.figure(figsize = figsize)
        plt.title(title)
        fill = len(self) < 5
        for d in self:
            d.plot(fill = fill)
        plt.legend()
        plt.xlabel("x")
        plt.show()

    def evaluate(self,x):
        fitnesses = [(i,dist.fitness(x)) for i,dist in enumerate(self)]
        indices,fitnesses = zip(*sorted(fitnesses,key = lambda x : x[1],reverse = True))
        return indices,fitnesses

    def selection(self,x,top = 0.1):
        indices,fitnesses = self.evaluate(x)
        n = int(top*len(fitnesses))
        return indices[:n]

    def crossover(self,indices):
        combinations = list(itertools.combinations(indices,2))
        np.random.shuffle(combinations)
        combinations = combinations[:len(self)]
        new_population = []
        for i,j in combinations:
            new_population.append(self[i]+self[j])
        self.distributions = new_population

    def mutate(self,generation = 1):
        for d in self:
            d.mutate(generation)

    def evolve(self,x,top = 0.25,n_generations = 20,last_selection = True):
        all_fitnesses = [self.evaluate(x)[1]]

        for generation in tqdm(range(n_generations)):

            indices = self.selection(x,top)
            self.crossover(indices)
            self.mutate(generation)

            indices,fitnesses = self.evaluate(x)
            all_fitnesses.append(fitnesses)

        self._plot_fitnesses(all_fitnesses)

        if last_selection:
            indices = self.selection(x,top)

        return Population(self[indices])

    def _plot_fitnesses(self,fitnesses):
        sups = []
        infs = []
        means = []
        for step in fitnesses:
            sups.append(np.max(step))
            infs.append(np.min(step))
            means.append(np.mean(step))

        plt.figure(figsize=(10,6))
        plt.plot(means)
        plt.fill_between(range(len(means)),sups,infs, alpha = 0.2)
        plt.xlabel('# Generation')
        plt.ylabel('Fitness')
        plt.legend()
        plt.show()

#=============================================================================================================================
# LOGREG
#=============================================================================================================================

import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

class LogReg(torch.nn.Module):
    def __init__(self, n_feature,n_output = 1,alpha = 10e-1):
        self.alpha = alpha
        self.args = n_feature,n_output
        super(LogReg, self).__init__()
        self.out = torch.nn.Linear(n_feature,n_output,bias = False)   # output layer

    def forward(self, x):
        x = Variable(torch.FloatTensor(x))
        x = F.sigmoid(self.out(x))
        return x

    def __add__(self,other):
        new = LogReg(*self.args)
        new.out.weight.data = torch.FloatTensor(0.5 * (self.out.weight.data.numpy() + other.out.weight.data.numpy()))
        return new

    def mutate(self,generation):
        out = self.out.weight.data.numpy()
        noise_out = self.alpha * np.random.randn(*out.shape)
        self.out.weight.data = torch.FloatTensor(self.out.weight.data.numpy() + noise_out)

    def evaluate(self,x,y):
        pred = self.forward(x).data.numpy()
        loss_1 = np.sum(np.log(pred + 10e-9)*y.reshape(-1,1))
        loss_0 = np.sum(np.log(1-pred + 10e-9)*(1-y).reshape(-1,1))
        return loss_1 + loss_0

    def plot_coefs(self):
        plt.figure(figsize = (15,4))
        plt.title("Coefficients")
        plt.axhline(0,c = "black")
        plt.plot(self.out.weight.data.numpy()[0])
        plt.xlabel("# Pixel")
        plt.show()

class PopulationLogReg(object):
    def __init__(self,x,y,regs = None,n = 20,top = 0.25,**kwargs):

        self.x = x
        self.y = y
        self.kwargs = kwargs

        if regs is None:
            self.regs = [LogReg(**kwargs) for i in range(n)]
        else:
            self.regs = regs

    def __getitem__(self,key):
        if type(key) == tuple or type(key) == list:
            d = []
            for i in key:
                d.append(self.regs[i])
            return d
        else:
            return self.regs[key]

    def __iter__(self):
        return iter(self.regs)

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

    def evaluate(self):
        fitnesses = [(i,element.evaluate(self.x,self.y)) for i,element in enumerate(self)]
        indices,fitnesses = zip(*sorted(fitnesses,key = lambda x : x[1],reverse = True))
        return indices,fitnesses

    def selection(self,top = 0.5):
        indices,fitnesses = self.evaluate()
        n = int(top*len(fitnesses))
        return indices[:n]

    def crossover(self,indices):
        combinations = list(itertools.combinations(indices,2))
        np.random.shuffle(combinations)
        combinations = combinations[:len(self)]
        new_population = []
        for i,j in combinations:
            new_population.append(self[i]+self[j])

        if len(new_population) < len(self):
            new_population.extend([LogReg(**self.kwargs) for i in range(len(self)-len(new_population))])
        self.regs = new_population

    def mutate(self,generation):
        for d in self:
            d.mutate(generation)

    def evolve(self,top = 0.25,n_generations = 20,last_selection = True):
        n_fittest = int(top*len(self))
        offsprings = len(list(itertools.combinations(range(n_fittest),2)))
        print("- Generations {}".format(len(self)))
        print("- Fittest : {}".format(n_fittest))
        print("- Offsprings : {}".format(offsprings))

        all_fitnesses = [self.evaluate()[1]]

        for generation in tqdm(range(n_generations)):

            indices = self.selection(top)
            self.crossover(indices)
            self.mutate(generation)

            indices,fitnesses = self.evaluate()
            all_fitnesses.append(fitnesses)

        self._plot_fitnesses(all_fitnesses)

        if last_selection:
            indices = self.selection(top)

        return PopulationLogReg(self.x,self.y,regs = self[indices])

    def _plot_fitnesses(self,fitnesses):

        from sklearn.linear_model import LogisticRegression
        lr = LogisticRegression()
        lr.fit(self.x,self.y)
        pred_bench = lr.predict_proba(self.x)
        loss_bench = np.sum(np.log(pred_bench + 10e-9)*self.y.reshape(-1,1)) + np.sum(np.log(1-pred_bench + 10e-9)*(1-self.y).reshape(-1,1))

        sups = []
        infs = []
        means = []
        for step in fitnesses:
            sups.append(np.max(step))
            infs.append(np.min(step))
            means.append(np.mean(step))

        plt.figure(figsize=(10,6))
        plt.plot(means)
        plt.fill_between(range(len(means)),sups,infs, alpha = 0.2)
        plt.axhline(loss_bench)
        plt.xlabel('# Generation')
        plt.ylabel('Fitness')
        plt.legend()
        plt.show()


# Look for #IMPLEMENT tags in this file.
'''
All models need to return a CSP object, and a list of lists of Variable objects
representing the board. The returned list of lists is used to access the
solution.

For example, after these three lines of code

    csp, var_array = caged_csp_model(board)
    solver = BT(csp)
    solver.bt_search(prop_FC, var_ord)

var_array[0][0].get_assigned_value() should be the correct value in the top left
cell of the FunPuzz puzzle.

The grid-only models do not need to encode the cage constraints.

1. binary_ne_grid (worth 10/100 marks)
    - A model of a FunPuzz grid (without cage constraints) built using only
      binary not-equal constraints for both the row and column constraints.

2. nary_ad_grid (worth 10/100 marks)
    - A model of a FunPuzz grid (without cage constraints) built using only n-ary
      all-different constraints for both the row and column constraints.

3. caged_csp_model (worth 25/100 marks)
    - A model built using your choice of (1) binary binary not-equal, or (2)
      n-ary all-different constraints for the grid.
    - Together with FunPuzz cage constraints.

'''
from cspbase import *
import itertools

def binary_ne_grid(fpuzz_grid):
    ##IMPLEMENT
    constraints, constraint_values, constraint_names = [], [], []
    initial_variables = get_initial_variables(fpuzz_grid)
    size_of_board = initial_variables["size"]
    cell_values = initial_variables["cell_values"]
    variables = initial_variables["variables"]

    for cell in itertools.product(list(range(size_of_board)), list(range(size_of_board)), list(range(size_of_board))):
        col_c1 = cell_name(cell[1], cell[0]) + ", " + cell_name(cell[2], cell[0])
        col_c2 = cell_name(cell[2], cell[0]) + ", " + cell_name(cell[1], cell[0])
        if col_c1 not in constraint_names and col_c2 not in constraint_names and cell[2] != cell[1]:
            satisfying_col_constraints = []
            for v1 in cell_values:
                for v2 in cell_values:
                    if v1 != v2:
                        satisfying_col_constraints.append((v1, v2))
            c1 = Constraint(cell_name(cell[1], cell[0]) + ", " + cell_name(cell[2], cell[0]),
                            [variables[cell[1]][cell[0]], variables[cell[2]][cell[0]]])
            c1.add_satisfying_tuples(satisfying_col_constraints)
            constraints.append(c1)
            constraint_values.append(c1)
            constraint_names.append(c1.name)
            row_c1 = cell_name(cell[0], cell[1]) + ", " + cell_name(cell[0], cell[2])
            row_c2 = cell_name(cell[0], cell[2]) + ", " + cell_name(cell[0], cell[1])
            if row_c1 not in constraint_names and row_c2 not in constraint_names and cell[2] != cell[1]:
                satisfying_row_constraints = []
                for v1 in cell_values:
                    for v2 in cell_values:
                        if v1 != v2:
                            satisfying_row_constraints.append((v1, v2))

                added_constraints = Constraint(cell_name(cell[0], cell[1]) + ", " + cell_name(cell[0], cell[2]),
                                               [variables[cell[0]][cell[1]], variables[cell[0]][cell[2]]])
                added_constraints.add_satisfying_tuples(satisfying_row_constraints)
                constraint_values.append(added_constraints)
                constraint_names.append(added_constraints.name)

    csp = CSP('binary_ne', [variable for rows in variables for variable in rows])
    for constraint in constraints:
        csp.add_constraint(constraint)
    return (csp, variables)

def nary_ad_grid(fpuzz_grid):
    ##IMPLEMENT
    constraints, scope = [], []
    initial_variables = get_initial_variables(fpuzz_grid)
    cell_values = initial_variables["cell_values"]
    variables = initial_variables["variables"]

    for col in cell_values:
        for row in cell_values:
            scope.append(variables[row][col])
        cells1 = []
        for value_pair1 in itertools.permutations(cell_values):
            cells1.append(value_pair1)
        c1 = Constraint(hash(col), scope)
        c1.add_satisfying_tuples(cells1)
        constraints.append(c1)
        cells2 = []
        for value_pair2 in itertools.permutations(cell_values):
            cells2.append(value_pair2)
        c2 = Constraint(hash(col), variables[col])
        c2.add_satisfying_tuples(cells2)
        constraints.append(c2)

    csp = CSP('nary_ad', [variable for rows in variables for variable in rows])
    for constraint in constraints:
        csp.add_constraint(constraint)
    return (csp, variables)

def caged_csp_model(fpuzz_grid):
    ##IMPLEMENT
    constraints, constraint_values, constraint_names = [], [], []
    initial_variables = get_initial_variables(fpuzz_grid)
    size_of_board = initial_variables["size"]
    cell_values = initial_variables["cell_values"]
    cage_constraints = range(1, size_of_board)
    csp, variables = binary_ne_grid(fpuzz_grid)

    for cage in cage_constraints:
        row = list(fpuzz_grid[cage])
        operation, target, scope_values = row[-1], row[-2], row[:-2]
        scope, cells = [], []
        for scope_value in scope_values:
            value = variables[(scope_value // 10) - 1][(scope_value % 10) - 1]
            scope.append(value)
        constraint_name = "Operation: " + str(operation) + "Target:" + str(target)
        constraint = Constraint(constraint_name, scope)
        op = check_operation(operation)
        if op['addition']:
            for cell in itertools.product(tuple(cell_values), repeat=len(scope)):
                if sum(cell) == target:
                    cells.append(cell)
        elif op['subtraction']:
            for cell in itertools.product(tuple(cell_values), repeat=len(scope)):
                for i in range(len(scope)):
                    difference = cell[i] - sum(cell[:i] + cell[i + 1:])
                    if difference == target:
                        cells.append(cell)
        elif op['multiplication']:
            for cell in itertools.product(tuple(cell_values), repeat=len(scope)):
                for i in range(len(scope)):
                    product = float(cell[i])
                    for v1 in cell[:i] + cell[i + 1:]:
                        product *= v1
                    if product == target:
                        cells.append(cell)
        elif op['division']:
            for cell in itertools.product(tuple(cell_values), repeat=len(scope)):
                for i in range(len(scope)):
                    quotient = float(cell[i])
                    for v1 in cell[:i] + cell[i + 1:]:
                        quotient = quotient / v1
                    if quotient == target:
                        cells.append(cell)

        constraint.add_satisfying_tuples(cells)
        constraints.append(constraint)
        constraint_values.append(constraint)
        constraint_names.append(constraint.name)

    for constraint in constraints:
        csp.add_constraint(constraint)
    return (csp, variables)

def check_operation(operation):
    operation_dictionary = {'addition': False, 'subtraction': False, 'multiplication': False, 'division': False}
    if operation == 0:
        operation_dictionary['addition'] = True
    elif operation == 1:
        operation_dictionary['subtraction'] = True
    elif operation == 3:
        operation_dictionary['multiplication'] = True
    elif operation == 2:
        operation_dictionary['division'] = True
    return operation_dictionary

def cell_name(row, column):
    # Return cell name used for constraints
    return "Row: " + str(row) + " Col: " + str(column)

def get_initial_variables(fpuzz_grid):
    # Return size_of_board, cell_values, variables in that order
    size_of_board = fpuzz_grid[0][0]
    max_value = size_of_board + 1
    cell_values = list(range(1, max_value))
    variables = []
    for r in range(size_of_board):
        row = []
        for c in range(size_of_board):
            variable = Variable(cell_name(r, c), list(range(1, size_of_board + 1))[:])
            row.append(variable)
        variables.append(row)
    return {"size": size_of_board, "cell_values": cell_values, "variables": variables}


import abc
import sys
from importlib import import_module
from typing import TypeVar

import pytest

from sphinx.ext.autodoc.mock import _MockModule, _MockObject, mock

def test_MockModule():
    mock = _MockModule('mocked_module')
    assert isinstance(mock.some_attr, _MockObject)
    assert isinstance(mock.some_method, _MockObject)
    assert isinstance(mock.attr1.attr2, _MockObject)
    assert isinstance(mock.attr1.attr2.meth(), _MockObject)

    assert repr(mock.some_attr) == 'mocked_module.some_attr'
    assert repr(mock.some_method) == 'mocked_module.some_method'
    assert repr(mock.attr1.attr2) == 'mocked_module.attr1.attr2'
    assert repr(mock.attr1.attr2.meth) == 'mocked_module.attr1.attr2.meth'

    assert repr(mock) == 'mocked_module'

def test_MockObject():
    mock = _MockObject()
    assert isinstance(mock.some_attr, _MockObject)
    assert isinstance(mock.some_method, _MockObject)
    assert isinstance(mock.attr1.attr2, _MockObject)
    assert isinstance(mock.attr1.attr2.meth(), _MockObject)

    # subclassing
    class SubClass(mock.SomeClass):
        """docstring of SubClass"""

        def method(self):
            return "string"

    obj = SubClass()
    assert SubClass.__doc__ == "docstring of SubClass"
    assert isinstance(obj, SubClass)
    assert obj.method() == "string"
    assert isinstance(obj.other_method(), SubClass)

    # parametrized type
    T = TypeVar('T')

    class SubClass2(mock.SomeClass[T]):
        """docstring of SubClass"""

    obj2 = SubClass2()
    assert SubClass2.__doc__ == "docstring of SubClass"
    assert isinstance(obj2, SubClass2)

def test_mock():
    modname = 'sphinx.unknown'
    submodule = modname + '.submodule'
    assert modname not in sys.modules
    with pytest.raises(ImportError):
        import_module(modname)

    with mock([modname]):
        import_module(modname)
        assert modname in sys.modules
        assert isinstance(sys.modules[modname], _MockModule)

        # submodules are also mocked
        import_module(submodule)
        assert submodule in sys.modules
        assert isinstance(sys.modules[submodule], _MockModule)

    assert modname not in sys.modules
    with pytest.raises(ImportError):
        import_module(modname)

def test_mock_does_not_follow_upper_modules():
    with mock(['sphinx.unknown.module']):
        with pytest.raises(ImportError):
            import_module('sphinx.unknown')

@pytest.mark.skipif(sys.version_info < (3, 7), reason='Only for py37 or above')
def test_abc_MockObject():
    mock = _MockObject()

    class Base:
        @abc.abstractmethod
        def __init__(self):
            pass

    class Derived(Base, mock.SubClass):
        pass

    obj = Derived()
    assert isinstance(obj, Base)
    assert isinstance(obj, _MockObject)
    assert isinstance(obj.some_method(), Derived)

def test_mock_decorator():
    mock = _MockObject()

    @mock.function_deco
    def func():
        """docstring"""

    class Foo:
        @mock.method_deco
        def meth(self):
            """docstring"""

    @mock.class_deco
    class Bar:
        """docstring"""

    assert func.__doc__ == "docstring"
    assert Foo.meth.__doc__ == "docstring"
    assert Bar.__doc__ == "docstring"


from scipy.special import comb
"""
This file contains a set of functions to practice your
probabilities skills.

It needs to be completed with "vanilla" Python, without
help from any library -- except for the bin_dist function.
"""

def head_tails(p, n):
    """
    Given a coin that have probability p of giving a heads
    in each toss independently, what is the probability of
    having n heads consecutively in a row?

    :param p: probability of a head
    :param n: number of heads in a row (int)
    :return: probability of having n heads in a row
    :rtype: float
    """

    return  p**n

head_tails(0.5,1)

def bin_dist(n, p, x):
    """
    Given n number of trials, p the probability of success,
    what is the probability of having x successes?

    Your function should raise a ValueError if x is higher
    than n.

    If you need to compute combinations, you can import the
    function "comb" from the package "scipy.special"

    :param n: number of trials (int)
    :param p: probability of success
    :param x: number of successes (int)
    :return: probability of having x successes
    :rtype: float
    :raise ValueError: if x > n
    """

    if x > n:
       #  raise ValueError('value error')
       return ValueError

    return comb(n, x, exact=True) * (p ** x) * ((1 - p) ** (n - x))

bin_dist(10, .5, 6)
bin_dist(3, .7, 4)

def fact(x):
    """
    Return the factorial of x.
    Your function should raise a ValueError
    if x is negative

    :param x: a number (int)
    :return: the factorial of x
    :rtype: float
    :raise ValueError:
    """

    if x < 0:
        raise ValueError('x is negative')
    lfact = 1
    for i in range(1, x+1):
        lfact = lfact*i

    return float(lfact)

def bin_cdf(n, p, x):
    """
    Given n number of trials, p the probability of successes,
    what is the probability of having less than or equal to x successes?

    Your function should raise a ValueError if x is higher
    than n.

    :param n: number of trials (int)
    :param p: probability of success
    :param x: number of successes (int)
    :return: probability of having less than or
    equal to x successes
    :rtype: float
    :raise ValueError: if x > n
    """
    if x > n:
     return ValueError

    if p == 0:
        return 0

    q = 1 - p
    l_outcomes = (fact(n)/(fact(x)*fact(n-x)))
    l_probability = (p**n)
    return l_outcomes/l_probability

bin_cdf(3, 1, 1)
bin_cdf(3, 0 ,1)
bin_cdf(3, 0.7, 2)
bin_cdf(3, 0.7, 4)
bin_cdf(4, 0.2, 3)
bin_cdf(4, 0.4, 2)
bin_cdf(4, 0.8, 3)
bin_cdf(5, 0.2, 2)
bin_cdf(5, 0.2, 3)
bin_cdf(5, 0.4, 2)
bin_cdf(5, 0.4, 3)
bin_cdf(5, 0.8, 3)
bin_cdf(5, 0.2, 2)
bin_cdf(6, 0.2, 3)
bin_cdf(6, 0.4, 2)
bin_cdf(6, 0.4, 3)
bin_cdf(6, 0.8, 3)


from _rawffi import alt

class MetaStructure(type):

    def __new__(cls, name, bases, dic):
        cls._compute_shape(name, dic)
        return type.__new__(cls, name, bases, dic)

    @classmethod
    def _compute_shape(cls, name, dic):
        fields = dic.get('_fields_')
        if fields is None:
            return
        struct_descr = alt._StructDescr(name, fields)
        for field in fields:
            dic[field.name] = field
        dic['_struct_'] = struct_descr

class Structure(object):
    __metaclass__ = MetaStructure


class ListNode:
    def __init__(self, val=0, next=None):
        """
        :type val: int
        :type next: ListNode
        """
        self.val = val
        self.next = next

def mergeTwoLists(list1 ,list2):
    """
    :type l1: ListNode
    :type l2: ListNode
    :rtype: ListNode
    """
    result = ListNode()
    tail = result
    while list1 and list2:
        if list1.val < list2.val:
            tail.next = list1
            list1 = list1.next
        else:
            tail.next = list2
            list2 = list2.next
        tail = tail.next

    return result.next

list1 = ListNode(1,ListNode(2,ListNode(4)))
list2 = ListNode(1,ListNode(3,ListNode(4)))

res = mergeTwoLists(list1,list2)

while res.next is not None:
    print(res.val,end=" ")
    res = res.next
print(res.val)


from decouple import config
import os

class HTML_file:
    def __init__(self, group_name: str, measure: str) -> None:
        self.group_name = group_name
        self.measure = measure
        self.png_dir = os.path.join(config('root'), 'work/visual_graphs')

    def save_directory(self) -> str:
        return os.path.join(config('root'), 'results/assumptions')

    def html_markup(self) -> str:

        html_head_css = """
        <!DOCTYPE html>

        """

        html_body = f"""
                 <title>Assumption graphs for {self.group_name}</title>

Distro plots for average clustering, average shortest path length, assortativity, modularity and efficieny

          <centre>

        </centre>

Network measure plots

        """
        return html_head_css + html_body

    def save_to_file(self) -> None:
        directory = self.save_directory() + f'/{self.group_name}_assumptions_for_{self.measure}.html'
        html = self.html_markup()
        with open(directory, 'w') as file:
            file.write(html)

class Group_differences_HTML_file:
    def __init__(self, groups: dict, measure: str) -> None:
        self.png_dir = os.path.join(config('root'), 'work/visual_graphs')
        self.groups = [key for key in groups]
        self.measure = measure

    def save_directory(self) -> str:
        return os.path.join(config('root'), 'results/group_differences')

    def img_src(self):
        img_src = f"""

 Cluster plot for {self.groups[0]}

 Cluster plot for {self.groups[1]}

        """

        if len(self.groups) == 3:
            img_src = img_src + f"""

 Cluster plot for {self.groups[2]}

            """

        return img_src

    def html_markup(self) -> str:
        img = self.img_src()
        html_head_css = """
        <!DOCTYPE html>

        """

        html_body = f"""
                 <title>Group difference graphs</title>

Global measure plots for each group

          <centre>
          <img src = "{self.png_dir}/global_measure_plots_for_{self.measure}.png"
          </centre>

Cluster plots

          <centre>
          {img}
        </centre>

        """
        return html_head_css + html_body

    def save_to_file(self) -> None:
        directory = self.save_directory() + f'/group_differences_for_{self.measure}.html'
        html = self.html_markup()
        with open(directory, 'w') as file:
            file.write(html)


import random
from xml.dom.minidom import parseString

file=open('/home/med/Desktop/bioInfo.xml', 'r')
data= file.read()
dom=parseString(data)

f = open('/home/med/Desktop/seedpopulation.txt', "w")

PS=dom.getElementsByTagName('problemSize')[0].toxml()
PopS=dom.getElementsByTagName('populationSize')[0].toxml()

ProblemSize =PS.replace('<problemSize>','').replace('</problemSize>','')
PopulationSize=PopS.replace('<populationSize>','').replace('</populationSize>','')

psint=int(ProblemSize)+1
popsint=int(PopulationSize)+1

for i in range(1,popsint):
    for l in range(1, psint):
        x=random.randrange(0,2)
        y=str(x)
        f.write(y)
    f.write('\n')


import logging

from igraph import Graph as iGraph
from parvusdb import GraphDatabase
from parvusdb.utils.code_container import DummyCodeContainer
from parvusdb.utils.match import Match, MatchException

from .node_matcher import VectorNodeMatcher

_logger = logging.getLogger()

class GraphMatcher:
    def __init__(self, small_graph, metric, relations_metric):
        self.small_graph = small_graph
        self.metric = metric
        self.relations_metric = relations_metric

    def apply(self, g):
        if not isinstance(g, iGraph):
            raise TypeError("GraphRule.apply_to_graph() needs an igraph.Graph as an argument")
        db = GraphDatabase(g, node_matcher=VectorNodeMatcher(self.metric, self.relations_metric))
        rule = 'MATCH ' + str(self.small_graph) + ' RETURN __RESULT__;'
        lst = db.query(rule)
        if lst and lst[0]:
            return True
        return False

class GraphWeightedMatch:
    def __init__(self, big_graph, metric, relations_metric):
        self.big_graph = big_graph
        self.metric = metric
        self.relations_metric = relations_metric

    def apply(self, g):
        if not isinstance(g, iGraph):
            raise TypeError("GraphRule.apply_to_graph() needs an igraph.Graph as an argument")
        match = Match(matching_code_container=DummyCodeContainer(),
                      node_matcher=VectorNodeMatcher(self.metric, self.relations_metric, gradient=False))
        big_graph = self.big_graph._g
        try:
            matching_variables = match.get_variables_substitution_dictionaries(g, big_graph)
            w = 0
            for k, v in matching_variables[0].items():
                rindex = big_graph.vs['name' == v]['vector']
                lindex = g.vs['name' == k]['vector']
                w += self.metric.indices_dot_product(lindex, rindex)
            return w
        except MatchException as e:
            _logger.warning('Cannot find matching variables %s', str(e))
        return 0


import sys
from collections import deque
n, k = map(int, sys.stdin.readline().split())
m = 100001
visited = [-1] * m
check = [0] * m
q = deque()
visited[n] = 0
q.append(n)
def path(x):
    move = []
    temp = x
    for _ in range(visited[x] + 1):
        move.append(temp)
        temp = check[temp]
    print(*move[::-1])
while q:
    x = q.popleft()
    if x == k:
        print(visited[x])
        path(x)
        break
    else:
        for i in [2*x, x-1, x+1]:
            if (0 <= i <= (m-1)) and visited[i] == -1:
                visited[i] = visited[x] + 1
                q.append(i)
                check[i] = x # 이동 경로 저장


import os
import sys

#モジュール探索パス追加
p = ['../','../../']
for e in p: sys.path.append(os.path.join(os.path.dirname(__file__),e))

import discord
from discord.ext import commands
from discord import app_commands
from cmmod.json_module import open_json
from cmmod.time_module import get_currenttime
from cmmod.discord_module import CustomEmbed
from usermenu.cmfunc.userfunc import UserDBFunc
from usermenu.cmfunc.teamfunc import TeamDBFunc
from usermenu.error.usermenu_error import UserMenuError

#app_commandsで使うデータ
cmddata = open_json(r'menu/usermenu/data/apply_team.json')
cmdname = cmddata["name"]
cmddesp = cmddata["description"]
cmddesb = cmddata["describe"]
cmdcho = cmddata["choices"]
cmdcho_apt = [app_commands.Choice(name=c["name"],value=c["value"]) for c in cmdcho["apptype"]]
cmdcho_lgid = [app_commands.Choice(name=c["name"],value=c["value"]) for c in cmdcho["league"]]
cmddix = cmddata["dataindex"]

class ApplyTeam(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.userdbfunc = UserDBFunc()
        self.teamdbfunc = TeamDBFunc()
        self.custembed = CustomEmbed()

    @app_commands.command(name=cmdname, description=cmddesp)
    @app_commands.describe(apptype=cmddesb["apptype"],teamname=cmddesb["teamname"],league=cmddesb["league"],
    leader=cmddesb["leader"],member1=cmddesb["member1"],member2=cmddesb["member2"],member3=cmddesb["member3"],member4=cmddesb["member4"])
    @app_commands.choices(apptype=cmdcho_apt,league=cmdcho_lgid)
    @app_commands.guild_only()
    async def apply_team_command(self, interaction:discord.Interaction, apptype:app_commands.Choice[int], teamname:str, league:app_commands.Choice[int],
    leader:discord.User, member1:discord.User, member2:discord.User, member3:discord.User, member4:discord.User=None) -> None:
        author = interaction.user #コマンド実行者
        try:
            #【thinking処理】
            await interaction.response.defer(thinking=True)

            #【チーム情報確認処理】
            raw_teamdata = await self.teamdbfunc.get_teamdata(leaderid=author.id)
            teamdata = raw_teamdata[0]
            #[ERROR] 申請区分が「登録」且つ既にチーム情報が存在する場合
            if apptype.value == 0 and teamdata:
                error = "既にいずれかのチームのリーダーとなっています。新たにチーム登録する場合は、情報更新でリーダーを変更後行ってください"
                raise UserMenuError(error)
            #[ERROR] 申請区分が「更新」且つチーム情報が存在しない場合
            elif apptype.value == 1 and not teamdata:
                error = "いずれかのチームのリーダーであることが確認できませんでした。チームリーダーであるにもかかわらず、このエラーメッセージが送信された場合は運営まで連絡してください"
                raise UserMenuError(error)

            #【ユーザ情報確認処理】
            members = [leader, member1, member2, member3, member4]
            for member in members:
                if member != None:
                    raw_userdata = await self.userdbfunc.get_userdata(member.id)
                    userdata = raw_userdata[0]
                    #[ERROR] 指定ユーザの情報がデータベースに登録されていない場合
                    if not userdata:
                        error = f"指定ユーザ{member.mention}の情報がデータベースに登録されていません。ユーザ情報を登録行ってからチーム情報登録・更新を行ってください"
                        raise UserMenuError(error)

            #【メンバー4確定処理】
            if member4 == None: MEMBER4 = ''
            else: MEMBER4 = str(member4.id)

            #【登録日時確定処理】
            currenttime = get_currenttime()
            if apptype.value == 0: REGISTRATION_DATE = currenttime
            else: REGISTRATION_DATE = teamdata[9]

            #【チーム情報作成処理】
            postdata = {"チーム名":teamname, "リーグ":league.name, "リーダー":str(leader.id), "メンバー1":str(member1.id), "メンバー2":str(member2.id),
                        "メンバー3":str(member3.id), "メンバー4":MEMBER4, "登録日時":REGISTRATION_DATE, "最終更新日時":currenttime}

            #【POST処理】
            await self.teamdbfunc.post_teamdata(leaderid=leader.id, postdata=postdata, apptype=apptype.value)
            await self.teamdbfunc.log_teamdata(author=author, postdata=postdata, currenttime=currenttime, apptype=apptype.value)

        except UserMenuError as e:
            await interaction.followup.send(content=author.mention, embed=self.custembed.error(description=str(e)))

        except Exception as e:
            error = "コマンド実行中に予期せぬエラーが発生しました。このエラーが発生した場合は運営まで連絡をお願いします。\nエラー内容:"+str(e)
            print(error)
            await interaction.followup.send(content=author.mention,embed=self.custembed.error(description=error))

        else:
            #【完了送信処理】
            success = f"{author.mention}からリーダー:{leader.mention}のチーム情報{apptype.name}を受け付けました。データベースからの完了通知をお待ちください。通知が無かった場合は運営まで連絡をお願いします"
            await interaction.followup.send(content=author.mention, embed=self.custembed.success(description=success))

async def setup(client: commands.Bot):
    await client.add_cog(ApplyTeam(client))


import nilearn
from nilearn.plotting import plot_carpet, plot_glass_brain, plot_anat, plot_stat_map, plot_design_matrix, plot_epi, plot_contrast_matrix
from nilearn import image, masking, input_data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from nilearn.glm.first_level import make_first_level_design_matrix, FirstLevelModel
from nilearn.glm import threshold_stats_img
from nilearn.reporting import get_clusters_table, make_glm_report
from nilearn.input_data import NiftiLabelsMasker, NiftiMasker, NiftiSpheresMasker
from nilearn import datasets
from nilearn.regions import RegionExtractor
from nilearn import plotting
from nilearn import surface
from nilearn.decoding import Decoder

def get_events_file(events_home_dir, subject_id, run):
    events_file = events_home_dir + 'sub-' + subject_id  + '/run-' + str(run).zfill(2) + '/events.csv'
    #events_file = 'events_run_' + str(i) + '.csv'

    events = pd.read_csv(events_file)
    events = events.drop('Unnamed: 0', 1)
    return events

def fit_glm(subject_id, run):
    events = get_events_file(subject_id, run)
    tr = 1.25
    n_scans = image.load_img(fmri_image[run-1]).shape[-1]
    frame_times = np.arange(n_scans) * tr
    motion = np.cumsum(np.random.randn(n_scans, 6), 0)
    add_reg_names = ['tx', 'ty', 'tz', 'rx', 'ry', 'rz']
    design_matrix = make_first_level_design_matrix(frame_times, events,
                                               drift_model='polynomial', drift_order=3,
                                               add_regs=motion, add_reg_names=add_reg_names,
                                               hrf_model='spm')
    fmri_glm_model = FirstLevelModel(t_r=1.25, minimize_memory=False, noise_model='ar1', mask_img=mask_image[run-1])
    fmri_glm_model.fit(fmri_image[run-1], design_matrices=design_matrix)
    print("run done: ", run)
    return fmri_glm_model, design_matrix

def compute_no_diff_contrasts(glm, run):
    z_maps = list()
    conditions_label = list()
    sessions_label = list()
    events = get_events_file(subject_id, run)
    conditions = events.trial_type.unique()

    for condition_ in conditions:
        z_maps.append(glm[run-1].compute_contrast(condition_))
        conditions_label.append(condition_)
        sessions_label.append(str(run))
    return z_maps, conditions_label, sessions_label

def get_movement_minus_wait_contrasts(design_matrices, glms):
    z_map_movement_minus_wait = list()
    movement_minus_wait_labels = list()
    for run in range(1, 11):
        contrast_matrix = np.eye(design_matrices[run-1].shape[1])
        basic_contrasts = dict([(column, contrast_matrix[i])
                                for i, column in enumerate(design_matrices[run-1].columns)])
        movement_contrasts = basic_contrasts['movement_153'] + basic_contrasts['movement_207'] + basic_contrasts['movement_45'] + basic_contrasts['movement_99'] - basic_contrasts['wait']

        z_map_movement_minus_wait.append(glms[run-1].compute_contrast(movement_contrasts))
        movement_minus_wait_labels.append('Movement minus wait, run_' + str(run).zfill(2))

    return z_map_movement_minus_wait, movement_minus_wait_labels

def get_prep_minus_wait_contrasts(design_matrices, glms):
    z_map_prep_minus_wait = list()
    prep_minus_wait_labels = list()
    for run in range(1, 11):
        contrast_matrix = np.eye(design_matrices[run-1].shape[1])
        basic_contrasts = dict([(column, contrast_matrix[i])
                                for i, column in enumerate(design_matrices[run-1].columns)])
        movement_contrasts = basic_contrasts['go_153_prep'] + basic_contrasts['go_207_prep'] + basic_contrasts['go_45_prep'] + basic_contrasts['go_99_prep'] + basic_contrasts['nogo_153_prep'] + basic_contrasts['nogo_207_prep'] + basic_contrasts['nogo_45_prep'] + basic_contrasts['nogo_99_prep'] - basic_contrasts['wait']

        z_map_prep_minus_wait.append(glms[run-1].compute_contrast(movement_contrasts))
        prep_minus_wait_labels.append('Prep minus wait, run_' + str(run).zfill(2))

    return z_map_prep_minus_wait, prep_minus_wait_labels

def plot_contrast_maps(z_maps, z_map_no, condition_label, display_mode = 'ortho', correction = 'bonferroni', alpha = 0.05):
    _, threshold = threshold_stats_img(
        z_maps[z_map_no], alpha= alpha, height_control=correction)
    print('Bonferroni-corrected, p<0.05 threshold: %.3f' % threshold)

    plot_map = plot_stat_map(z_maps[z_map_no], threshold = threshold,
                             black_bg=True, display_mode=display_mode, draw_cross=False,
                             title = condition_label[z_map_no] + ' '+ correction + ' corrected, p<0.05')
    masker.fit(z_maps[z_map_no])
    #report = masker.generate_report()
    #plot_map.add_contours(image.index_img(atlas_filename, 11))
    plotting.show()
    return plot_map, masker


from torch.utils.data import Dataset
import torch
from PIL import Image
from pathlib import Path
import numpy as np
from dataclasses import dataclass
import random

@dataclass
class imageset:
    t1: Path
    t2: Path
    cm: Path

@dataclass
class patch:
    imset: imageset
    x: tuple
    y: tuple

class CDDataset(Dataset):
    """"""

    imagesets = None
    patchsize = None
    nx = 0
    ny = 0
    patches = []
    normalize = True

    cache = {}

    def loadrgb(self, image):
        if image not in self.cache:
            img = self._loadrgb(image).astype(np.float32)
            if self.normalize:
                img = (img - img.mean(axis=(-1, -2))[:, None, None]) / img.std(
                    axis=(-1, -2)
                )[:, None, None]
            self.cache[image] = img
        return self.cache[image]

    def loadcm(self, image):
        if image not in self.cache:
            self.cache[image] = self._loadcm(image).astype(np.int64)
        return self.cache[image]

    def __init__(self):
        if self.imagesets is None or self.patchsize is None:
            raise NotImplementedError
        m, v = np.zeros(3), np.zeros(3)
        self.patches = []
        for imset in self.imagesets:
            im1 = self.loadrgb(imset.t1)
            im2 = self.loadrgb(imset.t2)
            cm = self.loadcm(imset.cm)
            assert im1.shape[1:] == im2.shape[1:] == cm.shape
            assert im1.shape[0] == im2.shape[0] == 3
            for ix in range(im1.shape[1] // self.patchsize):
                for iy in range(im1.shape[2] // self.patchsize):
                    self.patches.append(
                        patch(
                            imset,
                            (self.patchsize * ix, self.patchsize * (ix + 1)),
                            (self.patchsize * iy, self.patchsize * (iy + 1)),
                        )
                    )
            self.nx += ix / len(self.imagesets)
            self.ny += iy / len(self.imagesets)
        self._m = m
        self._s = np.sqrt(v)

    def __getitem__(self, idx):
        patch = self.patches[idx]
        im1 = self.loadrgb(patch.imset.t1)
        im2 = self.loadrgb(patch.imset.t2)
        cm = self.loadcm(patch.imset.cm)
        im1 = im1[..., patch.x[0] : patch.x[1], patch.y[0] : patch.y[1]]
        im2 = im2[..., patch.x[0] : patch.x[1], patch.y[0] : patch.y[1]]
        #         if self.normalize:
        #           im1=(im1-im1.mean(axis=(-1,-2))[:,None,None])/im1.std(axis=(-1,-2))[:,None,None]
        #           im2=(im2-im2.mean(axis=(-1,-2))[:,None,None])/im2.std(axis=(-1,-2))[:,None,None]
        cm = cm[..., patch.x[0] : patch.x[1], patch.y[0] : patch.y[1]]
        return (im1, im2, cm)

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

class WV_S1(CDDataset):
    def __init__(self, path: Path, patchsize: int):
        self.imagesets = [imageset(*(path / f for f in ["t1.bmp", "t2.bmp", "gt.bmp"]))]
        self.patchsize = patchsize
        super(WV_S1, self).__init__()

    def _loadrgb(self, image):
        return np.array(Image.open(image)).transpose(2, 0, 1) / 255

    def _loadcm(self, image):
        return np.array(Image.open(image)) < 128

class OSCD(CDDataset):
    def __init__(self, path: Path, patchsize: int):

        self.imagesets = [
            imageset(im1, im2, cm)
            for im1, im2, cm in zip(
                sorted((path / "images").rglob("imgs_1_rect")),
                sorted((path / "images").rglob("imgs_2_rect")),
                sorted((path / "labels").rglob("cm")),
            )
        ]
        self.patchsize = patchsize
        super(OSCD, self).__init__()

    def _loadrgb(self, image):
        return np.stack(
            [np.array(Image.open(image / b)) for b in ("B02.tif", "B03.tif", "B04.tif")]
        )

    def _loadcm(self, image):
        return np.array(Image.open(next(image.glob("*-cm.tif")))) > 1

from typing import Tuple
from torch.utils.data import Subset

def split(
    ds: Dataset, validation_ratio: float, test_ratio: float, runsize=16, seed=0
) -> Tuple[Dataset, Dataset, Dataset]:
    """
    splits dataset by ratio (0..1) of validation and test in validation, test and train (remainder)
    while ensuring somewhat equal distribution between different parts
    of the Dataset by randomly choosing out of partitions of size runsize
    """
    rng = np.random.RandomState(0)
    val = list()
    test = list()
    train = list()
    split = np.array_split(np.arange(len(ds)), len(ds) / runsize)
    for s in split:
        nv = int(
            validation_ratio * (len(val) + len(test) + len(train) + len(s)) - len(val)
        )
        i = rng.choice(s, nv, replace=False)
        s = np.setdiff1d(s, i)
        val += i.tolist()
        nt = int(test_ratio * (len(val) + len(test) + len(train) + len(s)) - len(test))
        i = rng.choice(s, nt, replace=False)
        s = np.setdiff1d(s, i)
        test += i.tolist()
        train += s.tolist()
    return CDSubset(ds, train), CDSubset(ds, val), CDSubset(ds, test)

class CDSubset(Subset):
    """
    Subset of a CDDataset at specified indices with optional augmentation.
    """

    augment = False

    def __getitem__(self, idx):
        items = super().__getitem__(idx)
        if self.augment:
            if random.randint(0, 1):
                items = [np.swapaxes(item, -1, -2) for item in items]
            rot = random.randint(0, 3)
            items = [np.copy(np.rot90(item, rot, (-1, -2))) for item in items]
        return items

class CDCat(Dataset):
    """
    Concats the two images along first dimension
    """
    def __init__(self, baseObject):
        self.__class__ = type(baseObject.__class__.__name__,
                  (self.__class__, baseObject.__class__),
                  {})
        self.__dict__ = baseObject.__dict__
        self.baseObject=baseObject
    def __getitem__(self, idx):
        im1, im2, cm = self.baseObject[idx]
        return np.concatenate((im1,im2),0),cm


#Uppgift 3
"""
Primtalsfaktorerna av 13195 är 5, 7, 13 och 29.
Vilken är den största primtalsfaktorn av 600851475143?
"""

#Svar: 6857

num = 600851475143
factors = []

while num > 1:
  for i in range(2,num+1):
    if num % i == 0:
      num = int(num/i)
      factors.append(i)
      break

print(factors)


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

from odoo import fields, models, api

line_sizes = [
    ('medium', 'Moyen'),
    ('small', 'Petit'),
    ('smaller', 'Plus petit'),
    ('x-small', u'Très petit')
    ]

class ResCompany(models.Model):
    _inherit = "res.company"

    use_of_custom_footer = fields.Boolean(
        string=u'Utiliser le pied de page personnalisé pour cette société.',
        help=u"Cochez si vous voulez utiliser ce pied de page personnalisé pour les rapports PDF")
    of_custom_footer_line_1 = fields.Char(string='Ligne 1')
    of_custom_footer_line_2 = fields.Char(string='Ligne 2')
    of_custom_footer_line_3 = fields.Char(string='Ligne 3')
    of_custom_footer_line_1_size = fields.Selection(
        selection=line_sizes, string='Taille', default='small', required=True)
    of_custom_footer_line_2_size = fields.Selection(
        selection=line_sizes, string='Taille', default='small', required=True)
    of_custom_footer_line_3_size = fields.Selection(
        selection=line_sizes, string='Taille', default='smaller', required=True)

    of_custom_header_line_1 = fields.Char(string='Ligne 1')
    of_custom_header_line_2 = fields.Char(string='Ligne 2')
    of_custom_header_line_3 = fields.Char(string='Ligne 3')
    of_custom_header_line_4 = fields.Char(string='Ligne 4')
    of_custom_header_line_1_size = fields.Selection(
        selection=line_sizes, string='Taille', default='medium', required=True)
    of_custom_header_line_2_size = fields.Selection(
        selection=line_sizes, string='Taille', default='medium', required=True)
    of_custom_header_line_3_size = fields.Selection(
        selection=line_sizes, string='Taille', default='medium', required=True)
    of_custom_header_line_4_size = fields.Selection(
        selection=line_sizes, string='Taille', default='medium', required=True)

    of_max_height_bandeau = fields.Integer(string=u'Hauteur max bandeau (px)', default=130, required=True)

    of_position_header_lines = fields.Selection(
        [
            ('logo_under', u"Logo société et adresse configurable dessous"),
            ('logo_right', u"Logo société et adresse configurable à droite"),
            ('bandeau_pastille', u"Bandeau image + pastille"),
            ('bandeau_totalite', u"Bandeau image totalité page "),
            ], string="Type d'en-tête société", default="logo_right",
        help=u"Position des lignes d'en-tête relativement au logo de société\n"
             u"Sous le logo : les lignes d'en-tête seront placées sous le logo de société.\n"
             u"À droite du logo : les lignes d'en-tête seront placées à droite du logo.")

    @api.multi
    def get_line_content(self, header_or_footer="header", number=1):
        """Analyse des variables mako"""
        self.ensure_one()
        field_to_render = self.of_custom_header_line_1
        if header_or_footer == "header":
            if number == 2:
                field_to_render = self.of_custom_header_line_2
            elif number == 3:
                field_to_render = self.of_custom_header_line_3
            elif number == 4:
                field_to_render = self.of_custom_header_line_4
        else:
            if number == 1:
                field_to_render = self.of_custom_footer_line_1
            elif number == 2:
                field_to_render = self.of_custom_footer_line_2
            else:
                field_to_render = self.of_custom_footer_line_3
        content = self.env['mail.template'].render_template(field_to_render, 'res.company', self.id, post_process=False)
        return content

class View(models.Model):
    _inherit = 'ir.ui.view'

    @api.multi
    def render(self, values=None, engine='ir.qweb'):
        # Ajout de la possibilité d'appeler hasattr depuis une vue qweb
        return super(View, self).render(dict(values or {}, hasattr=hasattr), engine)


import pytest

from src.maximum_twin_sum_of_a_linked_list import Solution
from src.utils.linked_list import to_linked_list

@pytest.mark.parametrize(
    "in_list,expected",
    (
        ([5, 4, 2, 1], 6),
        ([4, 2, 2, 3], 7),
        ([1, 100_000], 100_001),
    ),
)
def test_solution(in_list, expected):
    head = to_linked_list(in_list)
    assert Solution().pairSum(head) == expected


import os
from discord.ext import commands, tasks
import motor.motor_asyncio
import util.util
from util.help import HelpCommand
from util.setup import load_text, load_data, mod_data, get_files
import discord
import itertools

bot = commands.Bot(
    command_prefix="!",  # Change to desired prefix
    case_insensitive=True, # Commands aren't case-sensitive
    intents=discord.Intents.all()
)

bot.help_command = HelpCommand(bot)

STATUS = itertools.cycle(["a", "b"])
mongo_client = motor.motor_asyncio.AsyncIOMotorClient("")  # need to create a database, i used mongo atlas
bot.db = mongo_client.bhv
bot.author_id = 656373241144934420  # Change to your discord id!!!

@bot.event
async def on_ready():  # When the bot is ready
    for extension in files:
        print(extension)
        await bot.load_extension(extension)  # Loades every extension.
    bot.hdb = bot.get_cog("Database")
    bot.util = util.util.setup(bot)
    bot.embed = discord.Embed(color=discord.Colour.from_str("#f77394"))
    mod_data(bot)
    change_status.start()
    print("I'm in")
    print(bot.user)  # Prints the bot's username and identifier

@tasks.loop(seconds=10)
async def change_status():
    await bot.change_presence(activity=discord.Game(next(STATUS)))

files = [file.replace("/", ".")[:-3] for file in get_files("cogs", [])]

bot.t = load_text()
bot.d = load_data()
token = ""  # your own token
bot.run(token)  # Starts the bot


from apivk.function_vk import vkinder
from datetime import date
from database.script_bd import check_users_vk, check_search_results, save_users_vk, save_search_results
from botvk.function_botvk import write_msg, send_photo

# определение статуса отношений
def find_relation(search_user_id):
    res = vkinder.about_user(search_user_id)['response']
    for item in res:
        if 'relation' not in item:
            relation = None
        elif 'relation' in item:
            relation = item['relation']
        return relation

# определение пола
def find_sex(search_user_id):
    res = vkinder.about_user(search_user_id)['response']
    for item in res:
        if 'sex' not in item:
            any_sex = None
        elif 'sex' in item:
            sex = item['sex']
            if sex == 2:
                any_sex = 1
            elif sex == 1:
                any_sex = 2
            else:
                any_sex = 0
        return any_sex

# определение города
def find_city(search_user_id):
    res = vkinder.about_user(search_user_id)['response']
    for item in res:
        if 'city' not in item:
            city = None
        elif 'city' in item:
            city = item['city']['id']
        return city

# определение возраста
def find_age(search_user_id):
    res = vkinder.about_user(search_user_id)['response']
    for item in res:
        if 'bdate' not in item:
            age = None
        elif 'bdate' in item:
            bdate = item['bdate']
            if len(bdate) >= 8:
                day, mon, year = bdate.split('.')
                day = int(day)
                mon = int(mon)
                year = int(year)
                today = date.today()
                age = today.year - year - ((today.month, today.day) < (mon, day))
            else:
                age = None
        return age

# отправить ссылку на человека и топ-3 фото
def choose_photo(age_from, age_to, relation, sex, city, search_user_id, user_id):
    try:
        search = vkinder.users_search(age_from, age_to, relation, sex, city)
        people_id = search['response']['items']
        for people in people_id:
            try:
                id_people = int(people['id'])
                first_name = people['first_name']
                last_name = people['last_name']
                status = people['is_closed']
                city_ = people['city']['id']
            except KeyError:
                pass
            if status is False and city_ == city:
                if city_ == city:
                    if check_users_vk(id_people) is None:
                        save_users_vk(id_people, first_name, last_name)
                    if check_search_results(search_user_id, id_people) is None:
                        save_search_results(search_user_id, id_people)
                        write_msg(user_id, f'Зацени {first_name} {last_name} https://vk.com/id{id_people}')
                        for i in vkinder.photo_user(id_people):
                            owner_id = i[1][2]
                            photo_id = i[1][3]
                            photo = f'photo{owner_id}_{photo_id}'
                            send_photo(user_id, photo)
                        write_msg(user_id, 'Для продолжения поиска повторно введите команду "поиск"')
                        break
    except TypeError:
        write_msg(user_id, 'Не хватает данных для поиска')

# проверка полноты информации о пользователе
def check(age, city, sex, relation, user_id):
    count = 0
    if age is not None:
        count += 1
    if city is not None:
        count += 1
    if sex is not None:
        count += 1
    if relation is not None:
        count += 1
    if count == 4:
        write_msg(user_id, 'Отлично! Для начала поиска введите команду "поиск"')