# from HeatmapGenerator import ModelType
from dataset import *
import pandas as pd
import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from itertools import cycle
import sklearn.metrics as sklm
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score, roc_curve, auc, roc_auc_score

def make_pred_multilabel(model, test_df, val_df, path_image, device):
    """
    Gives predictions for test fold and calculates AUCs using previously trained model
    Args:

        model: densenet-121 from torchvision previously fine tuned to training data
        test_df : dataframe csv file
        PATH_TO_IMAGES:
    Returns:
        pred_df: dataframe containing individual predictions and ground truth for each test image
        auc_df: dataframe containing aggregate AUCs by train/test tuples
    """

    BATCH_SIZE = 32
    workers = 8
    n_classes = 14

    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

    dataset_test = NIH(test_df, path_image=path_image, transform=transforms.Compose([
        transforms.Scale(256),
        transforms.CenterCrop(256),
        transforms.ToTensor(),
        normalize]))
    test_loader = DataLoader(dataset_test, BATCH_SIZE, shuffle=False,
                             num_workers=workers, pin_memory=True)

    dataset_val = NIH(val_df, path_image=path_image, transform=transforms.Compose([
        transforms.Scale(256),
        transforms.CenterCrop(256),
        transforms.ToTensor(),
        normalize]))
    val_loader = DataLoader(dataset_val, BATCH_SIZE, shuffle=True,
                            num_workers=workers, pin_memory=True)

    size = len(test_df)
    print("Test _df size :", size)
    size = len(val_df)
    print("val_df size :", size)

    # criterion = nn.BCELoss().to(device)
    model = model.to(device)
    # to find this thresold, first we get the precision and recall withoit this, from there we calculate f1 score, using f1score, we found this theresold which has best precsision and recall.  Then this threshold activation are used to calculate our binary output.

    PRED_LABEL = ['Atelectasis',
            'Cardiomegaly',
            'Effusion',
            'Infiltration',
            'Mass',
            'Nodule',
            'Pneumonia',
            'Pneumothorax',
            'Consolidation',
            'Edema',
            'Emphysema',
            'Fibrosis',
            'Pleural_Thickening',
            'Hernia']

    # for mode in ["Threshold"]:
    for mode in ["test"]:
        # create empty dfs
        pred_df = pd.DataFrame(columns=["path"])
        bi_pred_df = pd.DataFrame(columns=["path"])
        true_df = pd.DataFrame(columns=["path"])

        if mode == "Threshold":
            loader = val_loader
            Eval_df = pd.DataFrame(columns=["label", 'bestthr'])
            thrs = []

        if mode == "test":
            loader = test_loader
            TestEval_df = pd.DataFrame(columns=["label", 'auc', "auprc"])

            Eval = pd.read_csv("results/Threshold.csv")
            thrs = [Eval["bestthr"][Eval[Eval["label"] == "Atelectasis"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Cardiomegaly"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Effusion"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Infiltration"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Mass"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Nodule"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Pneumonia"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Pneumothorax"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Consolidation"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Edema"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Emphysema"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Fibrosis"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Pleural_Thickening"].index[0]],
                    Eval["bestthr"][Eval[Eval["label"] == "Hernia"].index[0]]]

        for i, data in enumerate(loader):
            inputs, labels, item = data

            inputs = inputs.to(device)
            labels = labels.to(device)

            true_labels = labels.cpu().data.numpy()

            batch_size = true_labels.shape

            model.eval()
            with torch.no_grad():
                outputs = model(inputs)
                probs = outputs.cpu().data.numpy()

            # get predictions and true values for each item in batch
            for j in range(0, batch_size[0]):
                thisrow = {}
                bi_thisrow = {}
                truerow = {}

                truerow["path"] = item[j]
                thisrow["path"] = item[j]
                if mode == "test":
                    bi_thisrow["path"] = item[j]
                    # iterate over each entry in prediction vector; each corresponds to
                    # individual label
                for k in range(len(PRED_LABEL)):
                    thisrow["prob_" + PRED_LABEL[k]] = probs[j, k]
                    truerow[PRED_LABEL[k]] = true_labels[j, k]

                    if mode == "test":
                       bi_thisrow["bi_" + PRED_LABEL[k]] = probs[j, k] >= thrs[k]

                pred_df = pred_df.append(thisrow, ignore_index=True)
                true_df = true_df.append(truerow, ignore_index=True)
                if mode == "test":
                    bi_pred_df = bi_pred_df.append(bi_thisrow, ignore_index=True)

            if (i % 200 == 0):
                print(str(i * BATCH_SIZE))

        for column in true_df:
            if column not in PRED_LABEL:
                continue
            actual = true_df[column]
            pred = pred_df["prob_" + column]

            thisrow = {}
            thisrow['label'] = column

            if mode == "test":
                bi_pred = bi_pred_df["bi_" + column]
                thisrow['auc'] = np.nan
                thisrow['auprc'] = np.nan
            else:
                thisrow['bestthr'] = np.nan

            try:
#                 n_booatraps = 1000
#                 rng_seed = int(size / 100)
#                 bootstrapped_scores = []

#                 rng = np.random.RandomState(rng_seed)
#                 for i in range(n_booatraps):
#                     indices = rng.random_integers(0, len(actual.as_matrix().astype(int)) - 1, len(pred.as_matrix()))
#                     if len(np.unique(actual.as_matrix().astype(int)[indices])) < 2:
#                         continue

#                     score = sklm.roc_auc_score(
#                         actual.as_matrix().astype(int)[indices], pred.as_matrix()[indices])
#                     bootstrapped_scores.append(score)

#                 thisrow['auc'] = np.mean(bootstrapped_scores)

                if mode == "test":
                    fpr = dict()
                    tpr = dict()
                    roc_auc = dict()
                    for i in range(n_classes):
                        a = true_labels[:, i].astype(int)
                        b = probs[:, i]
                        fpr[i], tpr[i], _ = roc_curve(true_labels[:, i].astype(int), probs[:, i])
                        roc_auc[i] = auc(fpr[i], tpr[i])
                    thisrow['auc'] = roc_auc_score(actual.to_numpy().astype(int), pred.to_numpy())
                    thisrow['auprc'] = sklm.average_precision_score(actual.to_numpy().astype(int), pred.to_numpy())
                    # Compute micro-average ROC curve and ROC area
                    fpr["micro"], tpr["micro"], _ = roc_curve(true_labels[:, i], probs[:, i])
                    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
                    # 绘制多分类问题的ROC曲线
                    # First aggregate all false positive rates
                    all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))

                    # Then interpolate all ROC curves at this points
                    mean_tpr = np.zeros_like(all_fpr)
                    for i in range(n_classes):
                        mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])

                    # Finally average it and compute AUC
                    mean_tpr /= n_classes

                    fpr["macro"] = all_fpr
                    tpr["macro"] = mean_tpr
                    roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

                    # Plot all ROC curves
                    plt.figure()
                    lw = 2
                    plt.plot(
                        fpr["micro"],
                        tpr["micro"],
                        label="micro-average ROC curve (area = {0:0.2f})".format(roc_auc["micro"]),
                        color="deeppink",
                        linestyle=":",
                        linewidth=4,
                    )

                    plt.plot(
                        fpr["macro"],
                        tpr["macro"],
                        label="macro-average ROC curve (area = {0:0.2f})".format(roc_auc["macro"]),
                        color="navy",
                        linestyle=":",
                        linewidth=4,
                    )

                    colors = cycle(["aqua", "darkorange", "cornflowerblue"])
                    for i, color in zip(range(n_classes), colors):
                        plt.plot(
                            fpr[i],
                            tpr[i],
                            color=color,
                            lw=lw,
                            label="ROC curve of class {0} (area = {1:0.2f})".format(i, roc_auc[i]),
                        )

                    plt.plot([0, 1], [0, 1], "k--", lw=lw)
                    plt.xlim([0.0, 1.0])
                    plt.ylim([0.0, 1.05])
                    plt.xlabel("False Positive Rate")
                    plt.ylabel("True Positive Rate")
                    plt.title("Some extension of Receiver operating characteristic to multiclass")
                    plt.legend(loc="lower right")
                    plt.savefig('results/mutilroc1.png')
                else:

                    p, r, t = sklm.precision_recall_curve(actual.as_matrix().astype(int), pred.as_matrix())
                    # Choose the best threshold based on the highest F1 measure
                    f1 = np.multiply(2, np.divide(np.multiply(p, r), np.add(r, p)))
                    bestthr = t[np.where(f1 == max(f1))]

                    thrs.append(bestthr)
                    thisrow['bestthr'] = bestthr[0]

            except BaseException:
                print("can't calculate auc for " + str(column))

            if mode == "Threshold":
                Eval_df = Eval_df.append(thisrow, ignore_index=True)

            if mode == "test":
                TestEval_df = TestEval_df.append(thisrow, ignore_index=True)

        pred_df.to_csv("results/preds.csv", index=False)
        true_df.to_csv("results/True.csv", index=False)

        if mode == "Threshold":
            Eval_df.to_csv("results/Threshold.csv", index=False)

        if mode == "test":
            TestEval_df.to_csv("results/TestEval.csv", index=False)
            bi_pred_df.to_csv("results/bipred.csv", index=False)

    print("AUC ave:", TestEval_df['auc'].sum() / 14.0)

    print("done")

    return pred_df, Eval_df, bi_pred_df, TestEval_df  # , bi_pred_df , Eval_bi_df

if __name__=="__main__":
    val_df = pd.read_csv(val_df_path)
    test_df = pd.read_csv(test_df_path)

    CheckPointData = torch.load('results/checkpoint')
    model = CheckPointData['model']

    make_pred_multilabel(model, test_df, val_df, path_image, device)


from typing import List
# LINK TO PROBLEM ----->  https://leetcode.com/problems/two-sum/

def two_sum(nums: List[int], target: int) -> List[int]:
    num_map = {}  # dictionaries in python work like hashmaps in java (?)
    solution = []
    for i in range(0, len(nums)):
        if target - nums[i] in num_map:
            solution.append(num_map.get(target - nums[i]))
            solution.append(i)
            return solution
        num_map[nums[i]] = i
    return solution

# O(n) Solution
numbers = [3, 3]
print(two_sum(numbers, 6))

# Success

#    for i in range(0, len(nums)):
#        for j in range(i + 1, len(nums)):
#            if nums[i] + nums[j] == target:
#                return i, j
#            j += 1
#        i += 1

#    First try, failed, struggling
#    i = 1
#    for number in nums:
#        while i in range(nums.index(number) + 1, len(nums)-1):
#            num2 = nums[i]
#            if number + num2 == target:
#                return nums.index(number), i
#            else:
#                i += 1


# algorithme de rendu de monnaie (glouton)

def rendu(montant):
    facteur = 1000
    montant_int = int(montant * facteur)
    pieces = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100]
    liste_rendu = []
    total = 0
    indice = len(pieces)-1
    while total < montant_int and indice >= 0:
        if int(pieces[indice]*facteur) <= (montant_int-total):
            liste_rendu.append(pieces[indice])
            total = total + int(pieces[indice]*facteur)
        else:
            indice = indice - 1
    return liste_rendu

print(rendu(3.75))


import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QLabel, QPushButton, QLineEdit

class AnalyzerUI(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.video_label = QLabel(self)
        self.text_label = QLabel(" ", self)
        self.capture_btn = QPushButton("Capture")
        self.analysis_btn = QPushButton("Analysis")

        self.hbox = QHBoxLayout()
        self.hbox.addWidget(self.capture_btn)
        self.hbox.addWidget(self.analysis_btn)

        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.video_label)
        self.vbox.addWidget(self.text_label)
        self.vbox.addLayout(self.hbox)

        self.setLayout(self.vbox)
        self.resize(400, 400)
        self.show()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = AnalyzerUI()
    sys.exit(app.exec_())


from marshmallow import pre_load, EXCLUDE

from ..extensions import ma
from ..models.board import BoardModel

class BoardSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = BoardModel
        include_relationships = True
        load_instance = True
        unknown = EXCLUDE

    @pre_load
    def parsing(self, input_data, **kwargs):
        """
        add webhook_body key value
        """
        return {
            **input_data,
            # "webhook_body": input_data
            }


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

import datetime
import numpy as np
import pandas as pd
import re
import warnings
import wordfreq

from lexicon_English import Words

warnings.filterwarnings("ignore", 'This pattern is interpreted as a regular expression, and has match groups.')

def print_time(message=None):
    now = datetime.datetime.now()
    time_str = now.strftime('%H:%M:%S')
    if message is None:
        print(time_str, end="\t")
    else:
        print(f"{time_str}\t{message}")

def sentences_to_df(sentences):
    sentences_txt = [" ".join(sentence) for sentence in sentences]
    df = pd.DataFrame({"sentence": sentences_txt})
    return df

def df_to_sentences(df):
    sentences_txt = df["sentence"]
    sentences = [sentence_txt.split(" ") for sentence_txt in sentences_txt]
    return sentences

def godown_dict_keys(d, ks):
    res = set()
    if len(ks) == 0:
        return d
    elif (type(d) is list):
        return d
    elif (type(d) is not dict):
        return res
    else:
        next_ds = [next_d for k, next_d in d.items() if re.search(ks[0], k)]
        for next_d in next_ds:
            res = res.union(godown_dict_keys(next_d, ks[1:]))
    return res

def reg_unigrams(w_list, border=r"\b"):
    return "|".join(rf"{border}{w}{border}" for w in w_list)

def reg_bigrams(w1_list, w2_list):
    return f"({reg_unigrams(w1_list)})" + r"\s" + f"({reg_unigrams(w2_list)})"

def remove_faulty_agreements(df):
    # Warning: adjency between noun and verb does not guarantee agreement
    # Here it is protected because we look at (noun, verb) pairs very early
    # Beware of RC, auxiliaries, etc.

    patterns_a = []

    pro_inanimate = ["it"]
    noun_inanimate = godown_dict_keys(Words, ['nouns_inanimate', '.*'])
    verb_animate = godown_dict_keys(Words, [r'\bverbs\b|\bverbs_intran_anim\b|\bmatrix_verbs\b', '.*', '.*'])
    pattern_animacy = "[A-Za-z]+\s" + reg_bigrams(noun_inanimate, verb_animate)
    pattern_it_animacy = reg_bigrams(pro_inanimate, verb_animate)

    patterns_a.append(pattern_it_animacy)
    patterns_a.append(pattern_animacy)

    pro_verb_s = ["he", "she", "it"]
    noun_sg_anim = godown_dict_keys(Words, [r'(\bnouns\b|\bnouns_inanimate\b)', '.*', 'singular'])
    noun_sg_inanim = godown_dict_keys(Words, [r'(\bnouns_inanimate\b)', 'singular'])
    noun_sg = noun_sg_anim.union(noun_sg_inanim)
    proper_names = godown_dict_keys(Words, [r'\bproper_names\b', '.*', '.*'])

    pro_verb_no_s = ["I", "you", "we", "they"]
    noun_pl = godown_dict_keys(Words, [r'\bnouns\b|\bnouns_inanimate\b', '.*', 'plural'])

    verb_sg = godown_dict_keys(Words, ['verbs', 'present', 'singular'])
    verb_pl = godown_dict_keys(Words, ['verbs', 'present', 'plural'])

    pattern_PN_sg = reg_bigrams(proper_names, verb_pl)
    pattern_noun_sg = "[A-Za-z]+\s" + reg_bigrams(noun_sg, verb_pl)
    pattern_pro_sg = reg_bigrams(pro_verb_s, verb_pl)
    pattern_pl = "[A-Za-z]+\s" + reg_bigrams(noun_pl, verb_sg)
    pattern_pro_pl = reg_bigrams(pro_verb_no_s, verb_sg)

    patterns_a.append(pattern_PN_sg)
    patterns_a.append(pattern_noun_sg)
    patterns_a.append(pattern_pro_sg)
    patterns_a.append(pattern_pl)
    patterns_a.append(pattern_pro_pl)

    patterns = []
    for pattern in patterns_a:
        patterns.append(f"^{pattern}")
        patterns.append(f"(that|whether)\s{pattern}")
    patterns.append("which\s.*" + reg_bigrams(noun_sg, verb_pl))
    patterns.append("which\s" + reg_bigrams(noun_pl, verb_sg))
    patterns.append("which\s.*" + reg_bigrams(noun_inanimate, verb_animate))
    patterns.append(reg_bigrams(["who"], verb_pl))

    quant_sg = ["every", "no"]
    quant_pl = ["all", "few"]
    pattern_q_sg = reg_bigrams(quant_sg, noun_pl)
    pattern_q_pl = reg_bigrams(quant_pl, noun_sg)
    patterns.append(f"{pattern_q_sg}")
    patterns.append(f"{pattern_q_pl}")

    patterns.sort(key=len)

    for pattern in patterns:
        mask = df["sentence"].str.contains(pattern)
        df = df[~mask]

    return df

def extract_verb(POS, anim_feature, Wkey):
    if (POS == "embedverb_Matrix"):
        return ""
    W = Words[Wkey]
    res = ""
    res = f"""
{POS}[finite=true, TENSE=pres, NUM=sg, PERS=1{anim_feature}] -> '{"'|'".join(W['present']['plural'])}'
{POS}[finite=true, TENSE=pres, NUM=sg, PERS=2{anim_feature}] -> '{"'|'".join(W['present']['plural'])}'
{POS}[finite=true, TENSE=pres, NUM=sg, PERS=3{anim_feature}] -> '{"'|'".join(W['present']['singular'])}'
{POS}[finite=true, TENSE=pres, NUM=pl{anim_feature}] -> '{"'|'".join(W['present']['plural'])}'
"""
#block future for now:
#{POS}[finite=true, TENSE=future{anim_feature}] -> '{"'|'".join(W['future'])}'"""
    if "-finite" in W.keys():
        res += f"""
{POS}[finite=false{anim_feature}] -> '{"'|'".join(W['-finite'])}'\n"""
        res += "\n"
    if "past" in W.keys():
        res += f"""
{POS}[finite=true, TENSE=past{anim_feature}] -> '{"'|'".join(W['past'])}'\n"""
        res += "\n"
    return res

def fix_poss_anim(df):
    df.loc[df.poss=='her', 'poss_ANIM'] = True
    df.loc[df.poss=='his', 'poss_ANIM'] = True
    return df

def add_features_to_dict(d, pos_tuple):
    word, pos = pos_tuple
    for i_item, (key, val) in enumerate(pos.items()):
        if i_item == 0: # Get subj/verb/object from *type* of pos
            svo = None
            if '_' in val:
                svo, svo_type = val.split('_')
                if svo_type != '':
                    d[f'{svo}_type'] = svo_type
                    d[f'{svo}'] = word
        else:  # Features of pos
            if svo is not None:
                feature_name = key
                d[f'{svo}_{feature_name}'] = val
    return d

def check_incongruence(f1, f2, role1=None, role2=None, poss_type=None):
    if pd.isnull(f1) or pd.isnull(f2):
        return np.nan
    elif role1=='poss' and poss_type!=role2:
        # Verify that the possessive is of the current role.
        # Since, poss-subj congruence means 'poss' is the possessive of the subject
        # And poss-obj congruence means the 'poss' is of the object.
        return np.nan
    else:
        return (f1 != f2)

def get_agreement_mismatch(row, role1, role2, agr_features=None):
    if agr_features is None:
        agr_features = ["GEN", "NUM", "PERS", "ANIM"]
    agreement_mismatch = {}
    if pd.isnull(row[f"{role1}_type"]) or pd.isnull(row[f"{role2}_type"]):
        # How do we mark that?
        # return np.nan?
        # agreement_match = {feature: np.nan for feature in agr_features}
        pass
    else:
        for feature in agr_features:
            agreement_mismatch[feature] = check_incongruence(
                row[f"{role1}_{feature}"],
                row[f"{role2}_{feature}"])
    agreement_mismatch["overall"] = any(
        agr_value for agr_value in agreement_mismatch.values()
        if agr_value is not np.nan
        )
    return agreement_mismatch

def order_columns(df, bring2front):
    # sort columns by name
    cols = sorted(list(df))

    # Move sentence to front column and save
    for name in bring2front:
        cols.insert(0, cols.pop(cols.index(name)))
        df = df.loc[:, cols]
    return df

def remove_repeated_sentences(df):
    df = df.drop_duplicates(subset=['sentence'])
    return df

def remove_sentences_with_repeated_lemma(df):
    # Remove sentences where a lemma is repeated
    # verbs are not included if one verb is trans and the other is intrans
    SINGLE_STRINGS = godown_dict_keys(Words, ['proper_names', 'singular', '.*'])
    SINGLE_STRINGS = [rf"\b{w}\b" for w in SINGLE_STRINGS]
    SINGLE_PAIRS = ([
            Words['nouns']['masculine'][NUM] +
            Words['nouns']['feminine'][NUM] +
            Words['nouns_inanimate'][NUM]
            for NUM in ['singular', 'plural']])
    SINGLE_PAIRS = [
        rf"\b{n_sg}\b|\b{n_pl}\b"
        for (n_sg, n_pl) in zip(*SINGLE_PAIRS)]
    REG_EX = SINGLE_STRINGS + SINGLE_PAIRS
    for E in REG_EX:
        doubleE = f"({E}).*({E})"
        mask = (df['sentence'].str.contains(doubleE))
        df = df[~mask]
    df = df.reset_index(drop=True)
    return df

def remove_impossible_binding(df):
    # remove "He saw him", "I saw me", etc.
    binding_problems = (
        (df["subj_type"] == "PRO") &
        (df["obj_type"] == "PRO") &
        (~(df["obj_REFL"] == True)) &
        (df["incongruence_subj_obj_count"]==0)
    )
    for test_exclude in ["I saw me", "She saw her"]:
        assert (test_exclude in df[binding_problems]["sentence"]) or ( not (test_exclude in df["sentence"]) )
    for test_include in ["I saw myself", "She saw herself"]:
        assert not (test_include in df[binding_problems]["sentence"])
    df = df[~binding_problems]
    df = df.reset_index(drop=True)
    return df

def calc_incongruence_counts(df):
    df = calc_incongruence_count(df, "subj", "obj")
    df = calc_incongruence_count(df, "poss", "subj")
    df = calc_incongruence_count(df, "poss", "obj")
    df = calc_incongruence_count(df, "subj", "embedsubj")
    return df

def calc_incongruence_count(df, role1, role2, features=None):
    if features is None:
        features = ['NUM', 'GEN', 'PERS', 'ANIM']
    df = calc_incongruence_feature(df, role1, role2)
    cols = [f"incongruent_{role1}_{role2}_{feat}" for feat in features]
    cols = [c for c in cols if c in df.columns]
    df[f"incongruence_{role1}_{role2}_count"] = df[cols].sum(axis=1, min_count=1)
    return df

def calc_incongruence_feature(df, role1, role2):
    for feat in ['NUM', 'GEN', 'PERS', 'ANIM']:
        if f'{role1}_{feat}' in df.columns and f'{role2}_{feat}' in df.columns:
            df[f'incongruent_{role1}_{role2}_{feat}'] = df.apply(lambda row:
                                                    check_incongruence(row[f'{role1}_{feat}'],
                                                                     row[f'{role2}_{feat}'],
                                                                     role1, role2,
                                                                     row['poss_type']),
                                                    axis=1)
    return df

def add_sentence_length(df):
    df['sentence_length'] = df.apply(lambda row:
                                     len(row['sentence'].split()),
                                     axis=1)
    return df

def add_has_property(df, groups=['subjrel', 'objrel', 'embed', 'main']):
    for group in groups:
        df[f'has_{group}'] = df.apply(lambda row:
                                      row['sentence_GROUP'].startswith(f'{group}'),
                                      axis=1)

    df['has_relative_clause'] = df.apply(lambda row:
                                         (row['has_subjrel'] or row['has_objrel']),
                                         axis=1)
    return df

def sanity_checks(sentence, tree):
    for fragment_test in [
        "dog see ",  # not a perfect test: "The boy that saw the dogs see the man"
        "dogs falls"  # not a perfect test: "The boy that saw the dogs falls"
    ]:
        if fragment_test in sentence:
            print("WARNING")
            print(sentence)
            print(tree)
        return

def compute_mean_zipf(row, words=['subj', 'embedsubj', 'verb', 'embedverb'],
                      lang='en'):
    zipfs = []
    for word in words:
        if not pd.isnull(row[word]):
            zipfs.append(wordfreq.zipf_frequency(row[word],
                                                 lang=lang))
    return np.floor(np.mean(zipfs))

def add_word_zipf(df):
    df['mean_zipf'] = df.apply(lambda row: compute_mean_zipf(row), axis=1)
    return df

def get_clause_type(row):
    if row['sentence_GROUP'].startswith(('main_', 'embed_')):
        # remove prefix (embed_/main_) and suffix (_clause) from sentence_GROUP
        return row['sentence_GROUP'].split('_')[1]
    else:
        return np.nan

def add_clause_type(df):
    df['clause_type'] = df.apply(lambda row: get_clause_type(row), axis=1)
    return df

def get_aux_tense(row):
    if row['sentence_GROUP'].startswith("main_obj"):
        return row['do_TENSE']
    else:
        return row['verb_TENSE']

def modify_tense_of_obj_quest(df):
    df['verb_TENSE'] = df.apply(lambda row: get_aux_tense(row), axis=1)
    return df

def check_lr_attractor(row):
    if row['sentence_GROUP'] in ['pp', 'objrel']:
        return check_incongruence(row['subj_NUM'], row['embedsubj_NUM'])
    elif row['sentence_GROUP'] == 'subjrel':
        return check_incongruence(row['subj_NUM'], row['embedobj_NUM'])
    else:
        return np.nan

def lr_agreement_with_attractor(df):
    df['long_range_agreement_with_attractor'] = df.apply(lambda row: check_lr_attractor(row),
                                                         axis=1)
    return df

def calc_simple_binding(row):
    bound_variable = np.nan
    coref_variable = np.nan
    if pd.isnull(row['quantifier']):
        if (row['subj_type'] == "PRO"):
            coref_variable = (row["obj_REFL"] == True)
        elif (row['poss_type'] == "subj"):
            agreement_mismatch = get_agreement_mismatch(row, "poss", "obj")
            coref_variable = not agreement_mismatch["overall"]
        elif (row['obj_type'] == "PRO"):
            coref_variable = (row["obj_REFL"] == True)
        elif (row['poss_type'] == "obj"):
            agreement_mismatch = get_agreement_mismatch(row, "poss", "subj")
            coref_variable = not agreement_mismatch["overall"]
    elif not (pd.isnull(row['quantifier'])):
        if (row['obj_type'] == "PRO"):
            bound_variable = (row["obj_REFL"] == True)
        elif (row['poss_type'] == "obj"):
            agreement_mismatch = get_agreement_mismatch(row, "poss", "subj")
            bound_variable = not agreement_mismatch["overall"]
    return {
        'bound_variable': bound_variable,
        'coref_variable': coref_variable}

def add_binding(df):
    binding_cols = df.apply(calc_simple_binding, axis=1, result_type="expand")
    df = pd.concat([df, binding_cols], axis=1)
    return df


import sys

from collections import deque

import double_pb2

import zmq

import kivy
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.label import Label

class MyApp(App):
	def __init__(self,receiver):
		super(MyApp,self).__init__()
		self.receiver = receiver

	def build(self,**kwargs):
		bufflen = 5
		self.recv_label = ReceiverLabel(
				self.receiver,bufflen,text='n.a.')
		#Clock.schedule_interval(self.recv,0)
		# update only every 0.05s at max, to limit cpu use
		Clock.schedule_interval(self.recv_label.display_latest,0.1)
		return self.recv_label

class ZMQReceiver():
	def __init__(self,parser):
		"""
		'parser' is called with the recieved message in 'self.recv()'
		"""
		self.parser = parser

		#  Socket to talk to server
		self.context = zmq.Context()
		self.socket = self.context.socket(zmq.SUB)

		# seems not to work :/
		## set High-Water-Mark to 1, so basically disabling the message
		## queue, 'cause it's likely that the GUI will not keep up with
		## the stream
		## Note: possible solution is to put the receiver in a dedicated
		##       thread/process, which is planned for the future!
		##self.socket.setsockopt(zmq.RCVHWM, 1)
		#self.socket.set_hwm(1)
		#print self.socket.hwm

		self.socket.connect("ipc:///tmp/sensor.ipc")

		# Subscribe to all messages
		self.socket.setsockopt_string(zmq.SUBSCRIBE, u"")

	def recv(self):
		return self.parser(self.socket.recv())

	def try_recv_latest(self):
		try:
			msg = self.socket.recv(flags = zmq.NOBLOCK)
		except zmq.ZMQError as e:
			raise BufferError(e)

		while(True):
			try:
				temp = self.socket.recv(flags = zmq.NOBLOCK)
			except zmq.ZMQError:
				break
			else:
				msg = temp

		return self.parser(msg)

	def recv_all(self,maxlen=None):
		msg_list = deque(maxlen=maxlen)
		while(True):
			try:
				msg_list.append(self.socket.recv(flags = zmq.NOBLOCK))
			except zmq.ZMQError:
				break

		return [ self.parser(msg) for msg in msg_list ]

class ReceiverLabel(Label):
	def __init__(self,receiver,bufflen,**kwargs):
		super(ReceiverLabel,self).__init__(**kwargs)
		self.receiver = receiver
		self.deque = deque(maxlen=bufflen)

	def recv(self,dt=0):
		self.text = str(self.receiver.recv())

	def try_recv_latest(self,dt=0):
		"""
		discards all previous messages that are in a buffer potentially
		does nothing if buffer is empty (no message to be received)
		"""
		try:
			self.text = str(self.receiver.try_recv_latest())
		except BufferError:
			pass

	def display_latest(self,dt=0):
		msg_list = self.recv_all(maxlen=self.deque.maxlen)

		if len(msg_list) == 0:
			return

		for msg in msg_list:
			self.deque.append(msg)

		string_list = [ str(elem) for elem in self.deque ]
		text = ''.join(string_list)

		self.text = text

	def recv_all(self,maxlen=None):
		return self.receiver.recv_all(maxlen)

if __name__ == '__main__':

	# use the string-parsing function generated from protoc
	recv = ZMQReceiver(double_pb2.RawDouble.FromString)

	print("Collecting updates from sensor")
	MyApp(recv).run()


import pandas as pd
from keras.models import Model,load_model
import keras.backend as K
from  keras.losses import mean_absolute_error
from keras.layers import Dense,Input,Lambda,Layer
from keras   import regularizers
import numpy as np
import matplotlib.pyplot as plt

def sampling(args):
    """Reparameterization trick by sampling fr an isotropic unit Gaussian.
       # Arguments:
           args (tensor): mean and log of variance of Q(z|X)
       # Returns:
           z (tensor): sampled latent vector
       """
    z_mean,z_log_var = args
    batch  = K.shape(z_mean)[0]
    dim = K.shape(z_mean)[1]
    epsilon = K.random_normal(shape=(batch,dim),
                              mean=0.0,stddev=1.0)
    std_epsilon = 1e-4
    return z_mean + (z_log_var + std_epsilon) * epsilon

def to_sequence(data,n_input):
    n_start = 0
    data_len = len(data)
    data_x  = list()
    data_x_len = data_len - n_input + 1
    if n_start < data_x_len:
        for _ in range(data_x_len):
            n_end = n_start + n_input
            data_x.append(data[n_start:n_end])
            n_start += 1
    return np.array(data_x)

def input(input_shape):
    return Input(input_shape,name='input')

def encoder(input,intermediate_dim,z_dim):
    # encoder
    dense_z1 = Dense(intermediate_dim,
                     activation='relu',
                     kernel_regularizer=regularizers.l2(0.001))(input)
    dense_z2 = Dense(intermediate_dim,
                     activation='relu',
                     kernel_regularizer=regularizers.l2(0.001))(dense_z1)

    z_mean = Dense(z_dim, name='z_mean')(dense_z2)
    z_log_var = Dense(z_dim, activation='softplus', name='z_log_var')(dense_z2)

    z = Lambda(sampling, output_shape=(z_dim,), name='z')([z_mean,z_log_var])
    return z,z_mean,z_log_var
def decoder(z,intermediate_dim,x_dim):
    # decoder
    dense_x1 = Dense(intermediate_dim,
                     activation='relu',
                     kernel_regularizer=regularizers.l2(0.001))(z)
    dense_x2 = Dense(intermediate_dim,
                     activation='relu',
                     kernel_regularizer=regularizers.l2(0.001))(dense_x1)

    x_mean = Dense(x_dim, name='zx_mean')(dense_x2)
    x_log_var = Dense(x_dim, activation='softplus', name='zx_log_var')(dense_x2)

    z_x = Lambda(sampling, output_shape=(x_dim,), name='z_x')([x_mean,x_log_var])
    return z_x

def donut(input_shape,intermediate_dim,z_dim,x_dim):
    inp = input(input_shape)
    z , z_mean, z_log_var = encoder(inp,intermediate_dim,z_dim)
    x = decoder(z,intermediate_dim,x_dim)
    model = Model(inp,x)
    reconstruction_loss = mean_absolute_error(inp,x)
    reconstruction_loss *= x_dim
    kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
    kl_loss = K.sum(kl_loss, axis=-1)
    kl_loss *= -0.5
    vae_loss = K.mean(reconstruction_loss + kl_loss)
    model.add_loss(vae_loss)
    model.compile(optimizer='sgd', metrics=['loss', 'acc'])

    return model

#
# class CustomVariationalLayer(Layer):
#     def __init__(self,z_mean,z_log_var,**kwargs):
#         self.is_placeholder = True
#         self.z_mean = z_mean
#         self.z_log_var = z_log_var
#         super(CustomVariationalLayer, self).__init__(**kwargs)
#
#     def vae_loss(self,x,z_x,z_mean,z_log_var):
#         mse = mean_absolute_error(x,z_x)
#         kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var),axis=-1)
#         return K.mean(mse + kl_loss)
#
#     def call(self, inputs,**kwargs):
#         x = inputs[0]
#         z_x = inputs[1]
#         loss = self.vae_loss(x,z_x,self.z_mean,self.z_log_var)
#         self.add_loss(loss,inputs=inputs)
#         return x


#A program, which will help Siren, with creating a beatiful banner

def Under_lining(message):
    print("Halloween")
    print("-" * len(message))
    print(message)

def Over_lining(message):
    print(message)
    print("-" * len(message))
    print("Halloween")

def Display_both(message):
    print(message)
    print("-" * len(message))
    print("Halloween")
    print("-" * len(message))
    print(message)

def Display_grid(message):

    message_lenght = len(message)

    if message_lenght >= 5:
        message_lenght = 5

    for count in range (0,message_lenght,1):
        print("Halloween|" * message_lenght)

def TakeInput(message):

    if message == "":
        print("Enter the message, which you would like to use for the banner")
        message = input()
    print("Please select an option for your banner!")
    print("""1 - Display your banned with a line under your message
2 - Display your banner with a line over your message
3 - Display your banner with both underline and an overline
4 - Display a grid with the word Halloween""")
    user_option = input()

    if user_option == "1":
        Under_lining(message)
    elif user_option == "2":
        Over_lining(message)
    elif user_option == "3":
        Display_both(message)
    elif user_option == "4":
        Display_grid(message)
    else:
        print("Please enter a valid option")
        TakeInput("")

TakeInput("")


#!/usr/bin/env python
#==============================================================================
#author         :Miryam de Lhoneux
#email          :miryam.de_lhoneux@lingfil.uu.se
#date           :2015/12/30
#version        :1.0
#description    :Perform a transformation on a file
#usage          :python transform_file.py infile outfile [transform|detransform|to_conllx]
#Python version :2.7.6
#==============================================================================

import sys
from src.treebank_transformer import TreebankTransformer

if __name__=="__main__":
    infile = sys.argv[1]
    out = sys.argv[2]
    change = sys.argv[3]
    #dep_style = 'pdt'
    dep_style = 'ud'
    TM = TreebankTransformer(dep_style=dep_style)
    TM.transform(infile,out,change)


import pytest

from blik.utils import DispatchList

def test_dispatch_list():
    lst = ['test']
    dl = DispatchList(lst)
    assert dl.upper()._data == ['TEST']
    assert 'test' in dl
    dl[0] = 'test2'
    assert dl[0] == 'test2'
    dl2 = DispatchList([dl])
    assert dl2.flatten()[0] == 'test2'
    assert dl.disp[0]._data == ['t']
    with pytest.raises(AttributeError):
        dl.nope
    dl.append('test3')
    assert 'test3' in dl
    dl[-1:] == ['test3']


import numpy as np
import pandas as pd
import scipy.stats as stats
from scipy.stats import randint
from time import time
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import make_scorer, accuracy_score, recall_score
from pprint import pprint

# Customized scoring function to compute recall on class 'deceased'
def _recall_on_deceased(y, y_pred, **kwargs):
        y_series = pd.Series(y)
        y_deceased = y_series[y_series == 0]
        y_pred_deceased = pd.Series(y_pred)[y_deceased.index]
        return recall_score(
            y_true = y_deceased,
            y_pred = y_pred_deceased,
            average = 'micro'
        )

def report(results, n_top = 5):
    for i in range(1, n_top + 1):
        candidates = np.flatnonzero(results['rank_test_Recall_on_deceased'] == i)
        for candidate in candidates:
            print("Model with rank: {0}".format(i))
            print("Accuracy: {0:.3f}".format(results['mean_test_Accuracy'][candidate]))
            print("Overall recall: {0:.3f}".format(results['mean_test_Recall'][candidate]))
            print("Recall on 'deceased': {0:.3f}".format(results['mean_test_Recall_on_deceased'][candidate]))
            print("Parameters: {0}".format(results['params'][candidate]))
            print("")

def main():
    X_train_inputfile = "../dataset/3.1_X_train.csv.gz"
    X_valid_inputfile = "../dataset/3.1_X_valid.csv.gz"
    y_train_inputfile = "../dataset/3.1_y_train.csv.gz"
    y_valid_inputfile = "../dataset/3.1_y_valid.csv.gz"
    X_train = pd.read_csv(X_train_inputfile)
    X_valid = pd.read_csv(X_valid_inputfile)
    y_train = pd.read_csv(y_train_inputfile).transpose().values[0]
    y_valid = pd.read_csv(y_valid_inputfile).transpose().values[0]

    sgd_model = SGDClassifier()

    param_dist = {
        'loss': ['hinge', 'modified_huber', 'log'],
        'penalty': ['l1','l2','elasticnet'],
        'max_iter': randint(25, 250)
    }
    n_iter_search = 15
    scoring = {
        'Accuracy': make_scorer(accuracy_score),
        'Recall': make_scorer(
            lambda y, y_pred, **kwargs:
                recall_score(y, y_pred, average = 'micro')
        )
    }

    random_search = RandomizedSearchCV(
        sgd_model,
        param_distributions = param_dist,
        n_iter = n_iter_search,
        n_jobs = -1,
        pre_dispatch = '2*n_jobs',
        scoring = scoring,
        refit = 'Recall'
    )

    start = time()
    random_search.fit(X_train, y_train)
    print("RandomizedSearchCV took %.2f seconds for %d candidates"
          " parameter settings." % ((time() - start), n_iter_search))
    report(random_search.cv_results_)

if __name__ == '__main__':
    main()


# https://leetcode.com/problems/median-of-two-sorted-arrays/

import math

def findMedianSortedArrays(nums1: list, nums2: list):
    total_len = len(nums1) + len(nums2)
    is_odd = total_len % 2
    k = math.ceil(total_len / 2)

    def getKthElement(k):
        offset_l = offset_r = 0

        while True:
            if offset_l >= len(nums1):
                return nums2[offset_r + k - 1]
            if offset_r >= len(nums2):
                return nums1[offset_l + k - 1]
            if k == 1:
                return min(nums1[offset_l], nums2[offset_r])

            cur = k // 2 - 1

            index_l = min(cur + offset_l, len(nums1) - 1)
            index_r = min(cur + offset_r, len(nums2) - 1)
            if nums1[index_l] <= nums2[index_r]:
                k -= index_l - offset_l + 1
                offset_l += cur + 1
            else:
                k -= index_r - offset_r + 1
                offset_r += cur + 1

    if is_odd:
        return getKthElement(k)
    else:
        return (getKthElement(k) + getKthElement(k + 1)) / 2

if __name__ == "__main__":
    nums1 = [1]
    nums2 = [2, 3, 4, 5, 6]
    print(findMedianSortedArrays(nums1, nums2))


import unittest
import sys
from metadata_extractor import MetadataExtractor
from bs4 import BeautifulSoup

class TestExtractMethods(unittest.TestCase):
    def setUp(self):
        self.wat = "test_files/test.wat"
        self.html = "test_files/test.html"
        self.extractor = MetadataExtractor()

    def test_get_title(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_title()
        data = 'New Crimea leaders move up referendum date'
        self.assertEqual(d,data)

    def test_get_author(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_author()
        data = 'Sergei L. Loiko'
        self.assertEqual(d,data)

    def test_get_published_date(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_published_date()
        data = '2014-03-01T04:36:00-0800'
        self.assertEqual(d,data)

    def test_get_modified_date(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_modified_date()
        data = '2014-03-01T04:36:43-0800'
        self.assertEqual(d,data)

    def test_get_created_date(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_created_date()
        data = None
        self.assertEqual(d,data)

    def test_get_header(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_header()
        data = "KIEV, Ukraine -- Crimea's new pro-Moscow premier, Sergei Aksenov, moved the date of the peninsula's status referendum to March 30."
        self.assertEqual(d,data)

    def test_get_publisher(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_publisher()
        data = "latimes.com"
        self.assertEqual(d,data)

    def test_get_publisher_origin(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_publisher_origin('http://www.latimes.com/world/worldnow/la-fg-wn-crimea-referendum-date-20140301-story.html')
        data = (u'US','47.6062,-122.3321')
        self.assertEqual(d,data)

    def test_get_language(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_language()
        data = None
        self.assertEqual(d,data)

    def test_get_url(self):
        with open("test_files/test.html") as fp:
            self.extractor.article = BeautifulSoup(fp, 'html.parser')
        d = self.extractor.get_url()
        data = 'http://www.latimes.com/world/worldnow/la-fg-wn-crimea-referendum-date-20140301-story.html'
        self.assertEqual(d,data)

    def test_extract_wat(self):
        d = self.extractor.extract_data_from_wat(self.wat)
        data = {'publisher': 'The Verge', 'publisherCountry': u'US', 'author': 'Chris Welch', 'url': 'https://www.theverge.com/2018/1/22/16921102/google-breaks-amazon-fire-tv-youtube-workaround', 'publisherCoordinates': '25.7743,-80.1937', 'publishedDate': '2018-01-22T17:34:03-05:00', 'title': 'Google briefly broke Amazon\xe2\x80\x99s workaround for YouTube on Fire TV', 'header': 'Fire TV owners can no longer access the TV-friendly YouTube interface in a web browser.', 'modifiedDate': '2018-01-22T17:34:03-05:00'}

        self.assertEqual(d,data)

    def test_extract_html(self):
        d = self.extractor.extract_data_from_html(self.html)
        data = {'publisher': 'latimes.com', 'publisherCountry': u'US', 'author': 'Sergei L. Loiko', 'url': 'http://www.latimes.com/world/worldnow/la-fg-wn-crimea-referendum-date-20140301-story.html', 'publisherCoordinates': '47.6062,-122.3321', 'publishedDate': '2014-03-01T04:36:00-0800', 'title': 'New Crimea leaders move up referendum date', 'header': "KIEV, Ukraine -- Crimea's new pro-Moscow premier, Sergei Aksenov, moved the date of the peninsula's status referendum to March 30.", 'modifiedDate': '2014-03-01T04:36:43-0800'}

        self.assertEqual(d,data)

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


from client import client
def main():
    server_ip = '192.168.0.13'
    server_port = 8000
    sensor_port = 8005
    video_port = 8006
    s = client()
if __name__ == '__main__':
    main()
    print('server test completed')


class Solution:
    # @param A : tuple of integers
    # @param B : tuple of integers
    # @param C : tuple of integers
    # @return an integer
    def get(self,a,b,c):
        return max(abs(a-b),abs(b-c),abs(c-a))

    def minimize(self, A, B, C):
        i,j,k=0,0,0
        mv = None           #min value
        while i<len(A) and j<len(B) and k<len(C):
            cmv = self.get(A[i],B[j],C[k])      #current min value
            if mv == None or cmv < mv:
                mv = cmv
            if min(A[i], B[j], C[k]) == A[i]:
                i += 1
            elif min(A[i], B[j], C[k]) == B[j] :
                j += 1
            else :
                k += 1
        return mv

if __name__=='__main__':
    n = int(input())
    for i in range(n):
        S = Solution()
        c = list(map(int,input().split(' ')))
        c1 = list(map(int,input().split(' ')))
        c2 = list(map(int,input().split(' ')))
        print (S.minimize(c,c1,c2))

'''

You are given 3 arrays A, B and C. All 3 of the arrays are sorted.

Find i, j, k such that :
max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])) is minimized.
Return the minimum max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i]))

**abs(x) is absolute value of x and is implemented in the following manner : **

      if (x < 0) return -x;
      else return x;

Example :

Input :
        A : [1, 4, 10]
        B : [2, 15, 20]
        C : [10, 12]

Output : 5
         With 10 from A, 15 from B and 10 from C.

'''


N, S = map(int, input().split())
arr = [int(i) for i in input().split()]
s_ptr = 0
e_ptr = 0
result = 100001
sum_arr = arr[0]
while True:
    if sum_arr >= S:
        result = min(result, e_ptr - s_ptr + 1)
        sum_arr -= arr[s_ptr]
        s_ptr += 1
    elif e_ptr == N - 1:
        break
    else:
        e_ptr += 1
        sum_arr += arr[e_ptr]
if result == 100001:
    print(0)
else:
    print(result)


####################################
# Models

# embed the input character indices using a nn.Embedding layer and then feed resulting embedding tensors into an RNN.
# After that, you can use RNN’s output feature of the last timestamp as the input of a two-layer feed-forward network,
# and obtain the probability.
####################################

import numpy as np
import torch
import torch.nn as nn

np.random.seed(42)

class RNNClsModel(nn.Module):
    def __init__(self,
                 weight_matrix=None,
                 vocab_size: int = 1000,
                 embed_dim: int = 64,
                 hidden_dim: int = 32,
                 output_dim: int = 1,
                 dropout: float = 0.1
                 ) -> None:
        super().__init__()

        # ------------------

        self.hidden_dim = hidden_dim

        # embedding layer
        if weight_matrix is not None:  # load pretrained embeddings
            vocab_size, embed_dim = weight_matrix.shape
            self.embed = nn.Embedding(vocab_size, embed_dim)
            self.embed.load_state_dict({'weight': torch.tensor(weight_matrix)})
            self.embed.weight.requires_grad = False
        else:
            self.embed = nn.Embedding(vocab_size, embed_dim)

        # recurrent neural network
        self.rnn = nn.RNN(embed_dim, hidden_dim, dropout=dropout, batch_first=True)  # (batch, seq, feature)

        # two-layer feed-forward neural network
        self.fnn = nn.Sequential(
            # first layer
            nn.Linear(hidden_dim, hidden_dim),
            nn.BatchNorm1d(hidden_dim),  # BatchNorm, normally between linear and activation
            nn.ReLU(inplace=True),  # most of time, ReLU
            # second layer
            nn.Linear(hidden_dim, output_dim),  # the output dimension of the feedforward neural network is 1
            nn.Sigmoid()  # probabilities for binary classification
        )

        # ------------------

    def forward(self, x: torch.Tensor) -> torch.Tensor:

        out = torch.Tensor()

        # ------------------
        # Write your code here
        # The shape of x is [batch_size, length_of_sequence], batch_first = True
        # The shape of out is [batch_size, 1]

        # embed input character indices using embedding layer
        batch_size = x.size(0)
        x = self.embed(x)  # out: [batch_size, length_of_sequence, hidden_size]
        # feed resulting embedding tensors into an RNN
        hidden = torch.zeros(1, batch_size, self.hidden_dim)
        x, hidden = self.rnn(x, hidden)
        # use RNN’s output feature of the last timestamp as the input of a two-layer feed-forward network
        out = self.fnn(x[:, -1, :])  # input: [batch_size, hidden_size]

        # ------------------

        return out

class LSTMClsModel(nn.Module):
    def __init__(self,
                 weight_matrix=None,
                 vocab_size=1000,
                 embed_size=64,
                 lstm_size=32,  # hidden dim
                 dense_size=0,  # optional: more linear layers
                 output_size=1,
                 lstm_layers=1,  # number of layers
                 dropout=0.1):
        """
        Initialize the model
        """
        super().__init__()
        self.lstm_layers = lstm_layers
        self.lstm_size = lstm_size
        self.dense_size = dense_size

        # embedding layer
        if weight_matrix is not None:  # load pretrained embeddings
            vocab_size, embed_size = weight_matrix.shape
            self.embedding = nn.Embedding(vocab_size, embed_size)
            self.embedding.load_state_dict({'weight': torch.tensor(weight_matrix)})
            self.embedding.weight.requires_grad = False
        else:
            self.embedding = nn.Embedding(vocab_size, embed_size)

        # if lstm_layers >= 2, dropout occurs except last layer
        self.lstm = nn.LSTM(embed_size, lstm_size, lstm_layers, dropout=dropout, batch_first=True)
        self.dropout = nn.Dropout(dropout)  # add dropout for last layer

        # linear layers
        if dense_size == 0:
            self.fc = nn.Linear(lstm_size, output_size)
        else:  # additional linear layer: dense layer
            self.fc1 = nn.Linear(lstm_size, dense_size)  # nn.Sequential cannot pass multiple inputs/sizes
            self.fc2 = nn.Linear(dense_size, output_size)

        self.sig = nn.Sigmoid()  # can do binary classification, output is probability [0,1]

    def init_hidden(self, batch_size):
        """
        Initialize the hidden state
        """
        weight = next(self.parameters()).data
        hidden = (weight.new(self.lstm_layers, batch_size, self.lstm_size).zero_(),
                  weight.new(self.lstm_layers, batch_size, self.lstm_size).zero_())

        return hidden

    def forward(self, nn_input_text):
        """
        Perform a forward pass of the model on nn_input
        """
        batch_size = nn_input_text.size(0)
        nn_input_text = nn_input_text.long()

        # 1. embedding layer
        embeds = self.embedding(nn_input_text)

        # 2. LSTM layer
        hidden_state = self.init_hidden(batch_size)  # initialize every epoch
        lstm_out, _ = self.lstm(embeds, hidden_state)
        # Stack up LSTM outputs, apply dropout
        lstm_out = lstm_out[:, -1, :]  # because batch_first=True, (batch, seq, feature)
        lstm_out = self.dropout(lstm_out)

        # 3. Linear + Dense layer
        if self.dense_size == 0:
            out = self.fc(lstm_out)
        else:
            dense_out = self.fc1(lstm_out)
            out = self.fc2(dense_out)  # (batch, 1)

        # 4. output
        # probabilities for binary classification
        probs = self.sig(out)

        return probs


from Crypto.Util.number import *
import requests
modul=99999999999
a=245
c=143
post_id=90462233978
for i in range(10000):
    tmp=post_id-c
    seed=tmp*inverse(a,modul)%modul
    print(seed)
    post_id=seed
    url="http://web.challenge.bi0s.in:5432/b5cd7ae0-7b50-7ae0-7ae0-47a03b473015"
    r=requests.get(url+f"/{post_id}")
    if "not found" not in r.text: #and "name" not in r.text and "<script>fetch(" not in r.text
        print(r.text)
        break


import glob
import json
import os
import string

_KEYNAME_TO_FINGERPRINT_FILE = "keyname-to-fingerprint.json"
_TRUSTDB_FILE = "trustdb.txt"
ALLOWED_FINGERPRINT_CHARACTERS = set(string.ascii_uppercase + string.digits)

def get_trusted_keys_dir():
    """Get the trusted keys directory from an environment variable"""
    trusted_keys_dir = os.environ.get("SERIALIZATION_TRUSTED_KEYS_DIR")
    if trusted_keys_dir is None:
        raise ValueError(
            "Could not find trusted pub key directory. "
            "You must set environment variable `SERIALIZATION_TRUSTED_KEYS_DIR`"
        )
    return trusted_keys_dir

def _verify_trusted_keys_dir(trusted_keys_dir):
    """Verify that a trusted keys directory has the proper contents"""
    files = glob.glob(os.path.join(trusted_keys_dir, "*"))
    has_pub_key = any(filepath.endswith(".pub.asc") for filepath in files)
    if not has_pub_key:
        raise ValueError("No public keys found in directory {}, {}".format(trusted_keys_dir, files))
    has_trustdb = any(os.path.split(filepath)[-1] == _TRUSTDB_FILE for filepath in files)
    if not has_trustdb:
        raise ValueError("No `{}` found in directory {}".format(_TRUSTDB_FILE, trusted_keys_dir))
    has_fingerprint_json = any(
        os.path.split(filepath)[-1] == _KEYNAME_TO_FINGERPRINT_FILE for filepath in files
    )
    if not has_fingerprint_json:
        raise ValueError(
            "No file `{}` found in {}.".format(_KEYNAME_TO_FINGERPRINT_FILE, trusted_keys_dir)
        )

def _is_fingerprint_valid(fingerprint):
    """Validate that a fingerprint is in the right format"""
    if len(fingerprint) != 40:
        return False
    elif set(fingerprint).difference(ALLOWED_FINGERPRINT_CHARACTERS):
        return False
    else:
        return True

def _validate_keyname_to_fingerprint_item(keyname, fingerprint):
    """Validate that a keyname and fingerprint are well-formed."""
    if not isinstance(keyname, str):
        raise TypeError("Found keyname of type {} instead of `str`".format(type(keyname)))
    if len(keyname) == 0:
        raise ValueError("Found empty keyname")
    if not isinstance(fingerprint, str):
        raise TypeError("Found fingerprint of type {} instead of `str`".format(type(fingerprint)))
    if not _is_fingerprint_valid(fingerprint):
        raise ValueError(
            "Found incorrect fingerprint format {}. Fingerprint should be a "
            "40 character string of uppercase letters and numbers. "
            "You can find the fingerprint with `gpg2 --list-keys` with the "
            "appropriate `--homedir` specified".format(fingerprint)
        )

def get_keyname_to_fingerprint():
    """Get a map of pub key filename to fingerprint"""
    trusted_keys_dir = get_trusted_keys_dir()
    filepath = os.path.join(trusted_keys_dir, _KEYNAME_TO_FINGERPRINT_FILE)
    with open(filepath, "r") as fi:
        keyname_to_fingerprint = json.load(fi)
    for keyname, fingerprint in keyname_to_fingerprint.items():
        _validate_keyname_to_fingerprint_item(keyname, fingerprint)
    return keyname_to_fingerprint

def get_trusted_pub_keys():
    """Get the trusted pub key files"""
    trusted_keys_dir = get_trusted_keys_dir()
    _verify_trusted_keys_dir(trusted_keys_dir)
    pub_key_files = glob.glob(os.path.join(trusted_keys_dir, "*.pub.asc"))
    return frozenset(os.path.basename(filepath) for filepath in pub_key_files)


class Node:
    def __init__(self,key):
        self.left = None
        self.right = None
        self.data = key
# Finding the nodes at k distance from root. (number of edges to root)
#bst = {
#    6 -> 4,9
#    4 -> 2,5
#    9 -> 8,12
#    12 -> 10,14
# k=2 -> 2, 5, 8, 12

def findKDistantNodes(root, k):
    res = []
    findKDistant(root, k, res)
    return str(res)

def findKDistant(root, k, res):
    if root == None:
        return
    if k == 0:
        res.append(root.data)
    else:
        findKDistant(root.left, k - 1, res)
        findKDistant(root.right, k - 1, res)

root = Node(6)
root.left = Node(4)
root.right = Node(9)
root.left.left = Node(2)
root.left.right = Node(5)

root.right = Node(9)
root.right.left = Node(8)
root.right.right = Node(12)

root.right.right.left = Node(10)
root.right.right.right = Node(14)

print(findKDistantNodes(root, 2))


class Error:
    def throw(self, types: int = 99, stages: int = 99, message: str = "", suggestion: str = "") -> None:
        """
        Error Types:\n
        (1) Unexpected Symbol Error\n
        (2) Syntax Error\n
        (3) Type Error\n
        (99) Unexpected Error Type\n

        States:\n
        (1) Lexing Stage\n
        (2) Parsing Stage\n
        (99) Unexpected Stage\n
        """

        # Define names of error types and stages.
        errorTypes = {
            1: "Unexpected Symbol Error",
            2: "Syntax Error",
            3: "Type Error",
            99: "Unexpected Error Type"
        }
        errorStage = {
            1: "Lexing Stage",
            2: "Parsing Stage",
            99: "Unexpected Stage"
        }

        # Check if types, stages, message, and suggestion exists.
        if types not in errorTypes: types = 99
        if stages not in errorStage: stages = 99
        if not message: message = "No error message."
        if suggestion != "": suggestion = f"Suggestion: {suggestion}\n"

        # Print error message.
        print(f"Error: {errorTypes[types]}\nThrowed At: {errorStage[stages]}\nError Message: {message}\n{suggestion}")

        # Exit with code -1.
        exit(-1)


# -*- coding: utf-8 -*-
from odoo import api, fields, models, _

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    def _compute_is_alerte(self):
        for obj in self:
            alerte=''
            for line in obj.invoice_line_ids:
                if line.price_unit==0:
                    alerte = "Prix facturé à 0"
                if line.price_unit>=9999:
                    alerte = "Prix facturé > 9999"
            obj.is_alerte=alerte

    is_export_compta_id = fields.Many2one('is.export.compta', 'Folio', copy=False)
    is_alerte = fields.Text('Alerte', copy=False, compute=_compute_is_alerte)

class AccountInvoiceLine(models.Model):
    _inherit = 'account.invoice.line'

    def _compute_is_colise(self):
        for obj in self:
            colis=0
            for move in obj.move_line_ids:
                colis+=move.quantity_done
            obj.is_colis = colis

    is_colis = fields.Integer('Colis', compute=_compute_is_colise)

class AccountPayment(models.Model):
    _inherit = 'account.payment'

    is_export_compta_id = fields.Many2one('is.export.compta', 'Folio', copy=False)


#
# @lc app=leetcode.cn id=703 lang=python3
#
# [703] 数据流中的第 K 大元素
#

# @lc code=start
"""
成绩很差但是通过了（主要是python里的PriorityQueue跟我想的不一样呀！）
Accepted
10/10 cases passed (916 ms)
Your runtime beats 13.07 % of python3 submissions
Your memory usage beats 6.98 % of python3 submissions (19 MB)
"""
class KthLargest:

    def __init__(self, k: int, nums: List[int]):
        self.k = k
        self.q = []
        for num in nums:
            self.add(num)

    def add(self, val: int) -> int:
        if len(self.q) < self.k:
            self.q.append(val)
            self.q.sort()
        else:
            if self.q[0] < val:
                self.q.append(val)
                self.q.sort()

        if len(self.q) >= self.k:
            return self.q[-self.k]

# Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)
# @lc code=end


from utils.utils import Logger
from utils.utils import save_checkpoint, save_checkpoint_name
from utils.utils import AverageMeter
from evals.evals import compute_confident_known_mask, openset_eval
from datasets.datasets import get_dataset_2
import time
import sys
from sklearn.metrics import accuracy_score

from common.train import *
import torch
import resource

from training.sup import setup
# setup training routine
train, fname = setup(P)

logger = Logger(fname, ask=not resume, local_rank=P.local_rank)
logger.log(P)
logger.log(model)

# Run experiments
losses = dict()
losses['sim'] = AverageMeter()
losses['norm'] = AverageMeter()
losses['time'] = AverageMeter()
check = time.time()

P.style_iter = iter(P.style_loader)

data_iter = iter(train_loader)

for its in range(start_its, P.iterations + 1):
    model.train()

    kwargs = {}
    kwargs['simclr_aug'] = simclr_aug

    # train one iteration
    sim_loss, simclr_norm, train_loader, data_iter = train(P, its, model, criterion, optimizer, scheduler_warmup, train_loader, data_iter, logger=logger, **kwargs)

    model.eval()

    if its == P.iterations and P.local_rank == 0:
        if P.multi_gpu:
            save_states = model.module.state_dict()
        else:
            save_states = model.state_dict()
        save_checkpoint(its, save_states, optimizer.state_dict(), logger.logdir)
        save_checkpoint_name(save_states, logger.logdir, f"{its}")
        logger.log("[Saving checkpoint]")

    # log
    losses['sim'].update(sim_loss, 1)
    losses['norm'].update(simclr_norm, 1)
    losses['time'].update(time.time()-check, 1)

    check = time.time()
    if its%10 == 0 :
        eta_sec = (P.iterations - its)*losses['time'].average
        hour = eta_sec // 3600
        eta_sec = eta_sec % 3600
        min = eta_sec // 60
        eta_sec = eta_sec % 60

        lr = optimizer.param_groups[0]['lr']

        logger.log('[Iteration %3d] [Loss_Sim %5.2f] [SimclrNorm %f] [LR %f] [Avg time %.2fs] [ETA %dh%dm%ds]' % (its, losses['sim'].average,
            losses['norm'].average, lr, losses['time'].average, hour, min, eta_sec))

        losses['sim'] = AverageMeter()
        losses['norm'] = AverageMeter()

    if P.iterative and its in P.its_breakpoints:
        logger.log(f"Reached breakpoint iteration {its}. Start computing pseudo labels for new known samples")

        known_mask, known_pseudo_labels, known_gt_labels = compute_confident_known_mask(P, model, source_test_loader, target_test_loader, logger=None)

        acc = accuracy_score(known_gt_labels[known_mask], known_pseudo_labels[known_mask])
        logger.log("Selected {} target samples as known. Classification accuracy for selected samples: {:.4f}".format(len(known_mask.nonzero()[0]), acc))
        del known_gt_labels

        if len(known_mask.nonzero()[0]) > 0:
            train_sets, n_classes = get_dataset_2(P, train=True, target_known_mask=known_mask, target_known_pseudo_labels = known_pseudo_labels)
            whole_source = ConcatDataset([train_sets[idx] for idx in range(n_classes)])
            my_sampler = BalancedMultiSourceRandomSampler(whole_source, P.batch_p, P.local_rank, P.n_gpus)
            print(f"{P.local_rank} sampler_size: {len(my_sampler)}")
            loader_kwargs = {'pin_memory': False, 'num_workers': 1, 'drop_last':True}
            train_loader = DataLoader(whole_source, sampler=my_sampler, batch_size=single_GPU_batch_size, **loader_kwargs)
            data_iter = iter(train_loader)

    if its%5000 == 0 or its == P.iterations:

        logger.log("Running openset eval")
        openset_eval(P, model, source_test_loader, target_test_loader, logger=logger)

sys.exit(0)


import os

os.chdir('Experiment_4/Frames')
print(os.getcwd())

for count, f in enumerate(os.listdir()):
    f_name, f_ext = os.path.splitext(f)
    f_name = "frame" + str(count)

    new_name = f'{f_name}{f_ext}'
    os.rename(f, new_name)


from odoo import api, fields, models, _
from datetime import datetime, timedelta
from odoo.exceptions import UserError

class medical_appointment(models.Model):
    _name = "medical.appointment"
    _inherit = 'mail.thread'

    name = fields.Char(string="Appointment ID", readonly=True, copy=True)
    patient_id = fields.Many2one('medical.patient','Patient',required=True)
    appointment_type = fields.Selection([
			('new', 'New'),
			('scheduled', 'Scheduled'),
			('regular', 'Regular'),
		], 'Appointment Type', sort=False,default='outpatient')
    urgency_level = fields.Selection([
			('a', 'Normal'),
			('b', 'Urgent'),
			('c', 'Medical Emergency'),
		], 'Urgency Level', sort=False,default="b")
    appointment_date = fields.Datetime('Appointment Date',required=True,default = fields.Datetime.now)
    appointment_end = fields.Datetime('Appointment End',required=True)
    doctor_id = fields.Many2one('medical.physician','Physician',required=True)
    notes = fields.Text(string="Info")

    @api.model
    def create(self, vals):
        vals['name'] = self.env['ir.sequence'].next_by_code('medical.appointment') or 'APT'
        msg_body = 'Appointment created'
        for msg in self:
          msg.message_post(body=msg_body)
        result = super(medical_appointment, self).create(vals)
        return result

    def confirm(self):
      self.write({'state': 'confirmed'})

    def done(self):
      self.write({'state': 'done'})

    def cancel(self):
      self.write({'state': 'cancel'})


import pygame
import Car
import Map

pygame.init()

info = pygame.display.Info()
# sw = info.current_w
# sh = info.current_h
sw = 1600
sh = 900

# sw = 1200
# sh = 700

size = sw, sh
bg = 120, 180, 120

screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.display.set_caption("Race Track")
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)

mapImg = pygame.image.load("track1s.png")
mapSized = pygame.transform.scale(mapImg, (1600, 900))
firstMap = Map.Map("First", mapSized)

carImg = pygame.image.load("car.png")
carSized = pygame.transform.rotozoom(carImg, 0, 0.5)
firstCar = Car.Car("First car", carSized, 0, 0.95, 0.93, 100, 100)

carImg2 = pygame.image.load("car2.png")
carSized2 = pygame.transform.rotozoom(carImg2, 0, 0.5)
secondCar = Car.Car("Second car", carSized2, 0, 0.95, 0.93, 100, 200)

cars = [firstCar]

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                quit()
            elif event.key == pygame.K_n:
                if(len(cars) == 1):
                    cars.append(secondCar)
            else:
                for car in cars:
                    car.handle_key_down(event.key)

        if event.type == pygame.KEYUP:
            for car in cars:
                car.handle_key_up(event.key)

    screen.fill(bg)
    firstMap.update()
    for car in cars:
        car.update()

    pygame.display.update()
    clock.tick(60)


"""
* Author : Batyr Nuryyev
* Date   : March 14, 2019
"""

"""
The following file will contain the reproduction class/methods such as
crossover. Selection will be done in population.py and mutation will be
called here after selection.
"""

import operator
import numpy as np
from copy import deepcopy
from genome import Genome

# For testing
from connectionGene import Connection

# For comparisons of floats
EPSILON = 0.0000001

# Helper for comparison
def equal(a, b):
    return abs(a - b) < EPSILON

# TODO: have to thoroughly test this function (edge cases and load testing)
def crossover(p, q):
    """ This function defines the crossover operation. For more details,
        please refer to NEAT paper (cited in the report).

        It expects p and q to be instances of Genome.
    """
    assert isinstance(p, Genome), "First parent is not an instance of Genome!"
    assert isinstance(q, Genome), "Second parent is not an instance of Genome!"
    assert p.get_inputs() == q.get_inputs(), "Input nodes of parents do not match!"
    assert p.get_outputs() == q.get_outputs(), "Output nodes of parents do not match!"

    # P - parent 1 (an instance of Genome)
    # Q - parent 2 (an instance of Genome)

    # Process genes
    A_score = p.get_score()
    B_score = q.get_score()

    # Sort the connection genes by innovation number
    A_genes = sorted(p.get_connections(), key = operator.attrgetter('innov'))
    B_genes = sorted(q.get_connections(), key = operator.attrgetter('innov'))

    # Single child
    offspring = []

    # Pointers for left and right genomes
    i = 0
    j = 0

    while i < len(A_genes) and j < len(B_genes):
        A_i_innov = A_genes[i].get_innov()
        B_j_innov = B_genes[j].get_innov()

        if A_i_innov == B_j_innov:
            # Choose randomly
            if np.random.rand() > 0.5:
                #print("Accepting A")
                offspring.append(deepcopy(A_genes[i]))
            else:
                #print("Accepting B")
                offspring.append(deepcopy(B_genes[j]))

            # Increment both pointers
            i += 1
            j += 1
        elif A_i_innov < B_j_innov:
            # Append left disjoint connection only if left genome scored higher
            # If scored equally, choose randomly
            if A_score > B_score:
                offspring.append(A_genes[i])
            elif equal(A_score, B_score):
                if np.random.rand() > 0.5:
                    offspring.append(deepcopy(A_genes[i]))
                else:
                    offspring.append(deepcopy(B_genes[j]))

            # Increment only left pointer
            i += 1
        elif A_i_innov > B_j_innov:
            # Append right disjoint connection only if right genome scored higher
            # If scored equally, choose randomly
            if B_score > A_score:
                offspring.append(deepcopy(B_genes[j]))
            elif equal(A_score, B_score):
                if np.random.rand() > 0.5:
                    offspring.append(deepcopy(A_genes[i]))
                else:
                    offspring.append(deepcopy(B_genes[j]))

            # Increment only right pointer
            j += 1

    # Choose excess numbers randomly if scored equally
    # Otherwise, choose from the fitter parent
    if A_score > B_score and i < len(A_genes):
        offspring += deepcopy(A_genes[i:])
    elif B_score > A_score and j < len(B_genes):
        offspring += deepcopy(B_genes[j:])
    elif equal(A_score, B_score) and abs(len(A_genes) - len(B_genes)) > 0:
        # If scores are the same yet one of the genomes is bigger, choose
        # excess connections randomly
        while i < len(A_genes):
            if np.random.rand() > 0.5:
                offspring.append(deepcopy(A_genes[i]))

            i += 1

        while j < len(B_genes):
            if np.random.rand() > 0.5:
                offspring.append(deepcopy(B_genes[j]))

            j += 1

    # Prepare inputs, outputs, and connections and make a new genome
    # Does not matter which parent's inputs/outputs we take (all constant)
    inputs = p.get_inputs()
    outputs = p.get_outputs()

    genome_offspring = Genome()
    genome_offspring.set_genome_crossover(inputs, outputs, offspring)

    return genome_offspring


#!/usr/bin/env python3
import json
from typing import Union, Iterable, Type, Tuple, Optional, Callable

from aox.challenge import Debugger
from utils import BaseChallenge

class Challenge(BaseChallenge):
    def solve(self, _input, debugger: Debugger):
        """
        >>> Challenge().default_solve()
        111754
        """
        return JsonWalker().get_numbers_sum_from_json(_input)

JsonValue = Union[None, bool, int, float, str]
JsonStructure = Union[dict, list]
Json = Union[JsonValue, JsonStructure]
ShouldWalk = Callable[[Json], bool]

class JsonWalker:
    def get_numbers_sum_from_json(self, json_string: str,
                                  should_walk: Optional[ShouldWalk] = None,
                                  ) -> float:
        """
        >>> JsonWalker().get_numbers_sum_from_json('{"a":{"b":4},"c":-1}')
        3
        >>> JsonWalker().get_numbers_sum_from_json(
        ...     '{"a":{"b":4},"c":-1}',
        ...     lambda datum: not isinstance(datum, dict) or 'b' not in datum)
        -1
        """
        return self.get_numbers_sum(
            json.loads(json_string), should_walk=should_walk)

    def get_numbers_sum(self, data: Json,
                        should_walk: Optional[ShouldWalk] = None) -> float:
        """
        >>> JsonWalker().get_numbers_sum([1, 2, 3])
        6
        >>> JsonWalker().get_numbers_sum({"a": 2, "b": 4})
        6
        >>> JsonWalker().get_numbers_sum([[[3]]])
        3
        >>> JsonWalker().get_numbers_sum({"a": {"b": 4}, "c": -1})
        3
        >>> JsonWalker().get_numbers_sum({"a": [-1, 1]})
        0
        >>> JsonWalker().get_numbers_sum([-1, {"a": 1}])
        0
        >>> JsonWalker().get_numbers_sum([])
        0
        >>> JsonWalker().get_numbers_sum({})
        0
        """
        return sum(self.walk_values_of_type(
            data, (int, float), should_walk=should_walk))

    def walk_values_of_type(self, data: Json,
                            _type: Union[Type, Tuple[Type, ...]],
                            should_walk: Optional[ShouldWalk] = None,
                            ) -> Iterable[JsonValue]:
        """
        >>> list(JsonWalker().walk_values_of_type([1, 2.0, 3], int))
        [1, 3]
        >>> list(JsonWalker().walk_values_of_type([1, 2.0, 3], (int, float)))
        [1, 2.0, 3]
        >>> list(JsonWalker().walk_values_of_type(
        ...     {"a": {"b": 4, "d": [1, "f", {"e": 5}]},"c": True}, int))
        [4, 1, 5]
        >>> list(JsonWalker().walk_values_of_type(
        ...     {"a": {"b": 4, "d": [1, "f", {"e": 5}]},"c": True}, bool))
        [True]
        """
        if _type is bool or (isinstance(_type, tuple) and bool in _type):
            for value in self.walk_values(data, should_walk=should_walk):
                if isinstance(value, _type):
                    yield value
        else:
            for value in self.walk_values(data, should_walk=should_walk):
                if isinstance(value, _type) and not isinstance(value, bool):
                    yield value

    def walk_values(self, data: Json,
                    should_walk: Optional[ShouldWalk] = None,
                    ) -> Iterable[JsonValue]:
        """
        >>> list(JsonWalker().walk_values([1, 2, 3]))
        [1, 2, 3]
        >>> list(JsonWalker().walk_values(
        ...     {"a": {"b": 4, "d": [1, "f", {"e": 5}]},"c": -1}))
        [4, 1, 'f', 5, -1]
        >>> list(JsonWalker().walk_values([[[3]]]))
        [3]
        >>> list(JsonWalker().walk_values(
        ...     {"a": {"b": 4, "d": [1, "f", {"e": 5}]},"c": -1},
        ...     lambda datum: not isinstance(datum, dict) or 'e' not in datum))
        [4, 1, 'f', -1]
        """
        if should_walk:
            if not should_walk(data):
                return
        if data is None:
            yield data
        elif isinstance(data, (bool, int, float, str)):
            yield data
        elif isinstance(data, list):
            for value in data:
                yield from self.walk_values(value, should_walk=should_walk)
        elif isinstance(data, dict):
            for value in data.values():
                yield from self.walk_values(value, should_walk=should_walk)
        else:
            raise TypeError(f"Expected JSON type, but got {type(data)}")

Challenge.main()
challenge = Challenge()


# Sample code from
# https://pymotw.com/2/socket/udp.html#client-and-server-together

import socket
import sys

# Get my IP address
my_ip = socket.gethostbyname(socket.gethostname())
#my_ip = '127.0.0.1'

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = (my_ip, 10000)
message = 'This is the message.  It will be repeated.'

try:

    # Send data
    print >>sys.stderr, 'sending "%s"' % message
    sent = sock.sendto(message, server_address)

    # Receive response
    print >>sys.stderr, 'waiting to receive'
    data, server = sock.recvfrom(4096)
    print >>sys.stderr, 'received "%s"' % data

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()


import pyHook
import pythoncom
import pyautogui
import time

def onclick(event):
    print(event.Position)               #prints coordinates of the mouse click
    print(pyautogui.screenshot().getpixel(event.Position))      #prints out RGB of a pixel that was clicked on
    print(str(time.time())+"\n")        #prints out the current system time
    return True

hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(onclick)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()


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

'''
@File       : tfidf_model.py
@Author     : HW Shen
@Date       : 2020/5/25
@Desc       :
'''

import time

from gensim import corpora, models, similarities

from ServiceOrientedChatbot.reader.data_helper import load_corpus_file
from ServiceOrientedChatbot.utils.logger import logger

class TfidfModel(object):

    def __init__(self, corpus_file, word2index):

        time_s = time.time()
        self.contexts, self.responses = load_corpus_file(corpus_file, word2index, size=50000)

        self._train_tfidf_model()  # 获得了 self.tfidf_model
        self.corpus_mm = self.tfidf_model[self.corpus]  # 生成tfidf向量化的语料库
        # self.index[docno] = simi_vector, 相似度矩阵
        self.index = similarities.MatrixSimilarity(self.corpus_mm)  # 注意：MatrixSimilarity() 需要全部放入内存中

        logger.debug("Time to build tfidf model by %s: %2.f seconds." % (corpus_file, time.time() - time_s))

    def _train_tfidf_model(self, min_freq=1):
        """
        生成tfidf模型
        """
        # Create tfidf model
        self.dct = corpora.Dictionary(self.contexts)  # contexts: [[],[],[]...]
        # Filter low frequency words
        low_freq_ids = [id_ for id_, freq in self.dct.dfs.items() if freq <= min_freq]  # min_freq自定义
        self.dct.filter_tokens(low_freq_ids)
        self.dct.compactify()
        # Build tfidf model
        self.corpus = [self.dct.doc2bow(s) for s in self.contexts]  # bag of words
        self.tfidf_model = models.TfidfModel(self.corpus)

    def _text2vec(self, text):

        bow = self.dct.doc2bow(text)  # bag of words

        return self.tfidf_model[bow]

    def similarity(self, query, size=10):

        vec = self._text2vec(query)  # 得到query的tfidf向量化的vector
        sims = self.index[vec]
        sim_sort = sorted(list(enumerate(sims)), key=lambda item:item[1], reverse=True)

        return sim_sort[:size]

    def get_docs(self, sim_items):

        docs = [self.contexts[id_] for id_, score in sim_items]
        answers = [self.responses[id_] for id_, score in sim_items]

        return docs, answers

if __name__ == '__main__':

    text = ["今天","天气","非常","好"]
    print()


# encoding: utf-8
import multiprocessing

def download_from_web(q):
  #模拟从网上下载数据
  data = [11,22,33,44]
  #向队列里写入数据
  for temp in data:
    q.put(temp)
  print("-------下载器已经下载数据并且保存在队列中-----")

def analysis_data(q):
  waitting_data = list()
  #从队列中取数据
  while True:
    data = q.get()
    waitting_data.append(data)
    if q.empty():
      break
  #模拟数据处理
  print(waitting_data)

def main():
  #1.创建一个队列
  q = multiprocessing.Queue()
  #2.创建多个进程将队列得引用当作实参传递到里面
  p1 = multiprocessing.Process(target=download_from_web(q))
  p2 = multiprocessing.Process(target=analysis_data(q))
  p1.start()
  p2.start()

if __name__ == "__main__":
  main()

多任务/02.多进程之间通过queue实现数据共享.py

02.多进程之间通过queue实现数据共享.py


import logging

import pytest

from test_tools.stubber_factory import stubber_factory

logger = logging.getLogger(__name__)

def pytest_configure(config):
    config.addinivalue_line(
        "markers",
        "integ: integration test that requires and uses AWS resources. " "",
    )

@pytest.fixture(name="make_stubber")
def fixture_make_stubber(request, monkeypatch):
    """
    Return a factory function that makes an object configured either
    to pass calls through to AWS or to use stubs.
    """

    def _make_stubber(service_client):
        """
        Create a class that wraps the botocore Stubber and implements a variety of
        stub functions that can be used in unit tests for the specified service client.

        After tests complete, the stubber checks that no more responses remain in its
        queue. This lets tests verify that all expected calls were actually made during
        the test.

        When tests are run against an actual AWS account, the stubber does not
        set up stubs and passes all calls through to the Boto 3 client.
        """
        fact = stubber_factory(service_client.meta.service_model.service_name)
        stubber = fact(service_client)

        def fin():
            stubber.assert_no_pending_responses()
            stubber.deactivate()

        request.addfinalizer(fin)
        stubber.activate()

        return stubber

    return _make_stubber


#Cree una lista para almacenar nombres. El proceso se realiza preguntando si desea seguiringresando. Si la respuesta es NO, se detiene el proceso.Luego, elimine el nombre con la menor cantidad de caracteres.

lista = []

opcion = "si"

while opcion!= "no":
    nombre = input("Ingrese su nombre: ")
    lista.append(nombre)
    opcion = input("Desea seguir ingresando nombres? \n Si \n No \n").lower()

lista.sort(key(len))
lista.pop(0)
print(lista)

Programación de Algoritmos Duoc/Primer semestre/Clase 6/Ejercicio 6.py


import boto3
import json
from TwitterAPI import TwitterAPI, TwitterConnectionError, TwitterRequestError

kinesis=boto3.client('kinesis',
                     aws_access_key_id='xxxxxxxxxxxxxxxxxxxxxx',
                     aws_secret_access_key='xxxxxxxxxxxxxxxxxxxxxx',
                     region_name='us-east-1'
                     )

twitter=TwitterAPI(
                   'xxxxxxxxxx','xxxx',
                   'xxxxxxxxxx','xxxxxxxxxxxxxxxxxxxxxxxx'
                  )

usersToFollow= [82221567926100480]
params={ 'follow': usersToFollow, 'extended': True }
stream=twitter.request('statuses/filter',params)

for item in stream:
    kinesis.put_record(StreamName='twitter',
                       Data=json.dumps(item),PartitionKey="filler")


import numpy as np

from hmm_inference import (backward_algorithm, fixed_lag_smoothing,
                           forward_algorithm, forward_backward_algorithm,
                           improved_forward_backward)
from initialize_hmm import init
from sampling import sampling
from utils import write_to_file, dict_to_matrix

# 1. Instantiate Hidden Markov Model[HMM]
(states, observations, transition_model, observation_model,
 observation_true_model, observation_false_model) = init()

# values of the initialization are saved with no labels
# so just map them from method params in same order
write_to_file((states, observations, transition_model, observation_model,
               observation_true_model, observation_false_model),
              "Markov Model initialization.txt")

# 2. Get samples from our model
iterations = 15
sequence_length = 20
sampling(iterations, sequence_length, states,
         observations, transition_model, observation_model)

# 3. Implement forward-backward algorithm
start_distribution_probability = np.array([0.5, 0.5])
forward_list, last_forward_list = forward_algorithm(iterations,
                                                    transition_model,
                                                    observation_true_model,
                                                    observation_false_model,
                                                    start_distribution_probability)

write_to_file(forward_list, 'Forward algorithm output.txt')
write_to_file(last_forward_list,
              'Forward algorithm output(Last entry per sequence).txt')

backward_list = backward_algorithm(iterations, sequence_length,
                                   transition_model,
                                   observation_true_model,
                                   observation_false_model)

write_to_file(backward_list, "Backward algorithm output.txt")

forward_backward_list = forward_backward_algorithm(
    forward_list, backward_list, start_distribution_probability)

write_to_file(forward_backward_list, "Forward Backward algorithm output.txt")

# Changing structure of the parameters to be matrices
trans_mdl = dict_to_matrix(transition_model)
obs_true_mdl = dict_to_matrix(observation_true_model)
obs_false_mdl = dict_to_matrix(observation_false_model)
forward_lst = np.array(forward_list)

time_space_indpndt_mem = improved_forward_backward(iterations=iterations,
                                                   last_forward_storage=last_forward_list,
                                                   transition_model=trans_mdl,
                                                   true_observation_model=obs_true_mdl,
                                                   false_observation_model=obs_false_mdl)

write_to_file(time_space_indpndt_mem,
              "Improved Forward Backward algorithm output.txt")

fixed_lag_smoothing = fixed_lag_smoothing(iterations,
                                          start_distribution_probability,
                                          obs_true_mdl,
                                          obs_false_mdl,
                                          10, trans_mdl, forward_lst)

write_to_file(fixed_lag_smoothing, "Fixed lag smoothing algorithm output.txt")


import random
n=random.randint(1,10)
count=0
status =1
while status<=5:
    try:
        x=int(input("enter a number from 1 to 10  : "))
        if x==n:
            count+=1
            print("number is match")

            break

        elif x>n and x<=10:
            count+=1
            print("high your number,again enter number")
        elif x<n and x>0:
            count += 1
            print("low your number,again enter number")
        elif x>n or x<n:
            count += 1
            print("out of range input")
    except :
        print("invailid input")
    status+=1
print("your try chance attemp",count)


from random import sample, random

# CONSTANT
POPULATION = 100
PAIRS = int(POPULATION / 2)
TOP_N = 30
CROSSOVER_RATE = 0.7
MUTATION_RATE = 0.5
CONVERGED_NUMBER = 15

# Types
# Chromosome: (number (fitness), ChromosomeValue)
# ChromosomeValue: number[number (index)]
# Restaurant: {
#     _id: string
#     profile: {
#         categories: {
#             name_en: string
#         }[]
#         price_range: number
#     }
# }
# User: {
#     categories: string[]
#     price_range: number
# }

def genetic_algorithm_recommender(restaurants, users, genes=6, get_matrix=False, varbose=False):

    # return: number
    def compute_fitness(chromosome_value):
        group_score = 0

        for user in users:
            score_sum = 0
            user_categories = user['categories']
            user_categories_set = set(user_categories)
            user_price_range = user.get('price_range', -1)
            if not user_price_range:
                user_price_range = None

            for index in chromosome_value:
                score = 0
                restaurant = restaurants[index]
                restaurant_categories = [category['name_en'] for category in restaurant['profile']['categories']]
                restaurant_price_range = restaurant['profile']['price_range'] or None
                restaurant_distance = restaurant['dist']['calculated'] or None

                preference_score = 0
                for index, user_category in enumerate(user_categories):
                    if user_category in restaurant_categories:
                        preference_score += len(user_categories) - index

                price_score = 0
                if user_price_range and restaurant_price_range:
                    gap = user_price_range - restaurant_price_range
                    if gap == 0:
                        price_score = 3
                    elif gap == 1:
                        price_score = 2
                    elif gap == 2:
                        price_score = 1

                # distance_score = 0
                # if restaurant_distance:
                #     if restaurant_distance < 1000:
                #         distance_score = 3
                #     elif restaurant_distance < 3000:
                #         distance_score = 1

                history_score = 0

                score = preference_score + price_score + history_score
                score_sum += score

            group_score += score_sum

        return group_score

    def get_top_n_chromosomes(chromosomes, top_n):
        return sorted(chromosomes, key= lambda chromosome: chromosome[0], reverse=True)[:top_n]

    # return: [(list[] (ChromosomeValue), list (ChromosomeValue))]
    def get_pairs(chromosomes):
        pairs = []
        selected_chromosomes = get_top_n_chromosomes(chromosomes, TOP_N)
        for _ in range(PAIRS):
            new_pair = []
            count = 0
            while True:
                if count == 100:
                    break
                new_pair = sample(range(len(selected_chromosomes)), 2)
                count += 1
                if new_pair not in pairs:
                    pairs.append(new_pair)
                    break
        return [(selected_chromosomes[pair[0]], selected_chromosomes[pair[1]]) for pair in pairs]

    # return [ChromosomeValue, ChromosomeValue]
    def crossover(chromosome_value_1, chromosome_value_2):
        new_chromosome_value_1 = []
        new_chromosome_value_2 = []
        for index, each_chromosome_value_1 in enumerate(chromosome_value_1):
            each_new_chromosome_value_1 = chromosome_value_1[index]
            each_new_chromosome_value_2 = chromosome_value_2[index]

            is_swap = False
            if each_chromosome_value_1 not in chromosome_value_2 and each_new_chromosome_value_2 not in chromosome_value_1 and random() < CROSSOVER_RATE:
                is_swap = True

            if is_swap:
                each_new_chromosome_value_1 = chromosome_value_2[index]
                each_new_chromosome_value_2 = chromosome_value_1[index]

            new_chromosome_value_1.append(each_new_chromosome_value_1)
            new_chromosome_value_2.append(each_new_chromosome_value_2)
        return [new_chromosome_value_1, new_chromosome_value_2]

    def mutate(chromosome_value):
        if random() < MUTATION_RATE:
            mutated_chromosome_value = []
            for each_chromosome_value in chromosome_value:
                if random() < MUTATION_RATE:
                    count = 0
                    while True:
                        if count == 100:
                            mutated_chromosome_value.append(each_chromosome_value)
                            break
                        else:
                            count += 1
                            new_gene = sample(range(len(restaurants)), 1)[0]
                            if new_gene not in chromosome_value and new_gene not in mutated_chromosome_value:
                                mutated_chromosome_value.append(new_gene)
                                break
                else:
                    mutated_chromosome_value.append(each_chromosome_value)
            return [chromosome_value, mutated_chromosome_value]

        return [chromosome_value]

    def generate_new_generation(pairs):
        crossovered_chromosomes = []
        new_generation = []
        # crossover
        for pair in pairs:
            chromosome_1 = pair[0]
            chromosome_2 = pair[1]
            new_chromosomes = crossover(chromosome_1[1], chromosome_2[1])
            crossovered_chromosomes += new_chromosomes
        # mutation
        for chromosome_value in crossovered_chromosomes:
            new_generation += mutate(chromosome_value)
        return new_generation

    def add_fitness_for_population(chromosome_values):
        return [(compute_fitness(chromosome_value), chromosome_value) for chromosome_value in chromosome_values]

    def initialize_population():
        # check for dup
        return [sample(range(len(restaurants)), genes) for _ in range(POPULATION)]

    def is_converged(generation_highest_fitnesses):
        return len(generation_highest_fitnesses) > CONVERGED_NUMBER and generation_highest_fitnesses[-1] == generation_highest_fitnesses[-CONVERGED_NUMBER]

    population = initialize_population()
    generation_highest_fitnesses = []
    generation_highest_fitness_chromosomes = []

    number_of_generation = 0
    while True:
        number_of_generation += 1
        chromosomes = add_fitness_for_population(population)
        highest_fitness_chromosome = max(chromosomes)
        generation_highest_fitnesses.append(highest_fitness_chromosome[0])
        generation_highest_fitness_chromosomes.append(highest_fitness_chromosome)
        print(f"Gen#{number_of_generation}: {highest_fitness_chromosome}") if varbose else ''
        if number_of_generation == 100 or is_converged(generation_highest_fitnesses):
            print(f"Result: {highest_fitness_chromosome}") if varbose else ''
            recommended_restaurants = [restaurants[index] for index in highest_fitness_chromosome[1]]
            return recommended_restaurants if not get_matrix else (recommended_restaurants, generation_highest_fitness_chromosomes)
        pairs = get_pairs(chromosomes)
        population = generate_new_generation(pairs)

def test_genetic_algorithm_recommender(count=6):
    # import requests

    import json
    # response = requests.get('https://neutron-dot-restaurant-recommender-system.et.r.appspot.com/api/restaurants/nearby?lat=13.64999266005064&lon=100.49433963566172&dist=10000&limit=100')
    with open('recommender/test_data/nearby_restaurants.json') as f:
        response = json.load(f)
    input_restaurants = response
    input_restaurants = [restaurant for restaurant in input_restaurants if restaurant['type'] == 'restaurant']
    user_irin = {
        "name": "irin",
        "categories": ['Korean', 'Japanese', 'Italian', 'Thai', 'Shabu Shabu'],
        "price_range": 2,
    }
    user_north = {
        "name": "north",
        "categories": ['Thai', 'Japanese', 'Korean', 'Shabu Shabu', 'Steakhouse'],
        "price_range": 3,
    }
    users = [user_irin, user_north]
    recommended_retaurants, generations = genetic_algorithm_recommender(input_restaurants, users, get_matrix=True, genes=count)
    # pprint([(restaurant['name'], [cat['name_en'] for cat in restaurant['profile']['categories']]) for restaurant in recommended_retaurants])
    return recommended_retaurants, generations

if __name__ == '__main__':
    from pprint import pprint
    recommended_retaurants, generations = test_genetic_algorithm_recommender(count=6)
    pprint(generations)
    pprint([(restaurant['name'], [cat['name_en'] for cat in restaurant['profile']['categories']], restaurant['profile']['price_range']) for restaurant in recommended_retaurants])