from django import template
from km.models import UserNotification

register = template.Library()

@register.simple_tag
def show_unread_notifi_num(user):
    notifis = UserNotification.objects.filter(
        user=user,
        is_seen=False
    )
    if notifis:
        return len(notifis)
    else:
        return 0


import numpy as np
from scipy.misc import imread, imresize
from keras.models import load_model

loaded_model = load_model('model.hdf5')
print("Loaded Model from disk")

loaded_model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
x = imread('output.png',mode='L')
x = np.invert(x)
x = imresize(x,(28,28))
import matplotlib.pyplot as plt
plt.imshow(np.uint8(x))
plt.show()
x = x.reshape(1,28,28,1)
x = x/255
out = loaded_model.predict(x)
print(out)
print(np.argmax(out,axis=1))


import argparse
import json
import socket
import time

import settings

def send_data(connection, data):
    js_message = json.dumps(data)
    message = js_message.encode(settings.ENCODING_VAR)
    connection.send(message)

def receive_data(connection):
    """
    receive data from server
    :param
    connection: connection to server
    :return:
    json_data: json
        return json data
    """
    data = connection.recv(settings.MAX_DATA_LENGTH)
    json_data = json.loads(data.decode(settings.ENCODING_VAR))
    return json_data

def create_presence(account_name):
    """
    :param
    account_name: str
        username for presence message
    :return:
    msg: dict
        return presence message
    """
    msg = {
        "action": "presence",
        "time": time.time(),
        "type": "status",
        "user": {"account_name": account_name, "status": "Yep, I am here!"},
    }
    return msg

def process_data(data):
    """
    :param
    data: json
        answer from server
    :return: str
        decode answer from server
    """
    if "response" in data:
        if data["response"] == 200:
            return "200, OK"
        return f"{data['response']}, {data['error']}"
    return "Can't decode server answer"

def get_params():
    """
    return params from console
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(dest="port", type=int, help="Add port please", default=settings.DEFAULT_SERVER_PORT, nargs="?")
    parser.add_argument(dest="server_ip", type=str, help="Add server ip address please")
    args = parser.parse_args()
    if args.port < 1024 or args.port > 65535:
        parser.error("Error starting client. The port must be between 1024 and 65535")

    return args.port, args.server_ip

def main():
    """
    run client with python3 client.py -h
    """
    server_port, server_ip = get_params()

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((server_ip, server_port))
        msg = create_presence("Nik")
        send_data(s, msg)
        data = process_data(receive_data(s))
        print(f"Received: {data}")

if __name__ == "__main__":
    main()


import torch
from torch import nn
import torch.functional as F
from models.base_encoder import Encoder
from evaluations.utils import classification_acc
from models.base_linear_model import LinearModel

class InverseModel(nn.Module):
    def __init__(self, embed_len=32, num_actions=3, **kwargs):
        super(InverseModel,self).__init__()
        self.embed_len = embed_len
        self.encoder = Encoder(embed_len=embed_len, **kwargs)
        self.action_predictor = LinearModel(in_feat=2*self.embed_len,
                                            out_feat=num_actions)

    def forward(self,xs):
        f0 = self.encoder(xs[:,0])
        f1 = self.encoder(xs[:,1])
        fboth = torch.cat([f0,f1],dim=-1)
        return self.action_predictor(fboth)

    def loss_acc(self, trans):
        # for now just select the first two frames (but maybe in the future we could select every possible frame
        # or some combination of them)
        pred = self.forward(trans.xs)
        true = trans.actions[:,0]
        acc = classification_acc(logits=pred,true=true)
        loss = nn.CrossEntropyLoss()(pred,true)
        return loss, acc


from torch import nn

from ..builder import RECOGNIZERS
from .base import BaseRecognizer

@RECOGNIZERS.register_module()
class Recognizer2D(BaseRecognizer):
    """2D recognizer model framework."""

    def forward_train(self, imgs, label, **kwargs):
        """Defines the computation performed at every call when training."""

        assert self.with_cls_head
        batches = imgs.shape[0]
        imgs = imgs.reshape((-1, ) + imgs.shape[2:])
        num_segs = imgs.shape[0] // batches

        losses = dict()

        x = self.extract_feat(imgs)
        x = x.reshape((batches, num_segs) + x.shape[1:])

        cls_score = self.cls_head(x)
        gt_label = label.squeeze()
        loss_cls = self.cls_head.loss(cls_score, gt_label, **kwargs)
        losses.update(loss_cls)

        return losses

    def forward_test(self, imgs, **kwargs):
        """Defines the computation performed at every call when evaluation and testing."""
        batches = imgs.shape[0]
        imgs = imgs.reshape((-1, ) + imgs.shape[2:])

        x = self.extract_feat(imgs)
        assert 'num_segs' in self.test_cfg
        num_segs = self.test_cfg['num_segs']
        assert x.shape[0] % (batches * num_segs) == 0
        num_crops = x.shape[0] // (batches * num_segs)

        if self.test_cfg.get('feat_ext', False):
            # perform spatial pooling
            avg_pool = nn.AdaptiveAvgPool2d(1)
            x = avg_pool(x)
            # squeeze dimensions
            x = x.reshape((batches, num_crops, num_segs, -1))
            # temporal average pooling
            x = x.mean(axis=1).mean(axis=1)
            return x.cpu().numpy()

        x = x.reshape((batches * num_crops, num_segs) + x.shape[1:])
        cls_score = self.cls_head(x)
        cls_score = cls_score.reshape(batches, num_crops, cls_score.shape[-1])
        # calculate num_crops automatically
        cls_score = self.average_clip(cls_score)
        return cls_score.cpu().numpy()


import string
import random
from Bio.SubsMat import MatrixInfo

aminoacid_list = "ARNDCEQGHILKMFPSTWYV"

aminoacid_prob = {
   "A": 0.0826,
   "Q": 0.0393,
   "L": 0.0965,
   "S": 0.0660,
   "R": 0.0553,
   "E": 0.0674,
   "K": 0.0582,
   "T": 0.0535,
   "N": 0.0406,
   "G": 0.0708,
   "M": 0.0241,
   "W": 0.0109,
   "D": 0.0546,
   "H": 0.0227,
   "F": 0.0386,
   "Y": 0.0292,
   "C": 0.0137,
   "I": 0.0593,
   "P": 0.0472,
   "V": 0.0686
}

def generate_string(size=10, chars=string.ascii_uppercase + string.digits):
    """
    Generates a random string.
    Taken from: http://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python

    Parameters
    ----------
    size: int
        Size of the string to be generated

    chars: array-like
        Set of characters to use to generate the string

    Returns
    ----------
    string
    """
    return ''.join(random.SystemRandom().choice(chars) for _ in range(size))

def sub_matrices_distance(c1, c2, matrix=MatrixInfo.pam120):
    """
    Get the substitution score for c1 and c2
    according to the provided substitution matrix.

    Parameters
    ----------
    c1, c2: char

    matrix: Bio.SubsMat.MatrixInfo.available_matrix, optional
        The substitution matrix to be used, among the ones available in Bio.SubsMat.MatrixInfo

    Returns
    ----------
    int
        the score for substituting c1 with c2.
    """
    return matrix[(c1, c2)] if (c1, c2) in matrix else matrix[(c2, c1)]

def gap_function(gap_penalty, gap_opening_penalty, k):
    """
	Compute the cost of a gap given the input parameters.

	Parameters
    ----------
	gap_penalty: int
		Cost for extending a gap.

	gap_opening_penalty: int
		Additional cost for opening a gap.

	k: int
		Lenght of the current gap

	Returns
    ----------
    int
        the gap penalty value.
	"""
    return gap_opening_penalty + (k * gap_penalty)

class Alignment:
    """
    Class used to store an alignment of 2 sequences, stored as s1 and s2.
    It also contains a matching string, where for each aminoacid in s1 and s2, we have:\n
        ":" Perfect match of aminoacids.\n
        "." A substitution/mutation occured.\n
        " " A gap was introduced.
    It is possible to add the score of the alignment, if available.
    """

    def __init__(self, s1="", s2="", match="", score=0):
        self.s1 = str(s1)
        self.s2 = str(s2)
        self.match = match
        self.score = 0

    def __add__(self, other):
        if isinstance(other, self.__class__):
            return Alignment(s1 = self.s1 + other.s1, s2 = self.s2 + other.s2, match = self.match + other.match)
        return NotImplemented

    def __iadd__(self, other):
        if isinstance(other, self.__class__):
            self.s1 += other.s1
            self.s2 += other.s2
            self.match += other.match
        else:
             return NotImplemented

    def __str__(self):
        return self.s1 + "\n" + self.match + "\n" + self.s2 + "\n\n----- SCORE:" + str(self.score)

    def __repr__(self):
        return str(self)


# -*- coding=utf8 -*-
"""
    词语匹配
"""
import os

from sqlalchemy import Column, String, Integer, Float, create_engine, desc
from sqlalchemy.orm import sessionmaker

from pinyin.model.common import current_dir, BaseModel

db_name = os.path.join(current_dir, 'phrase.sqlite')
engine = create_engine('sqlite:///{}'.format(db_name))
PhraseSession = sessionmaker(bind=engine)

class PhrasePinyin(BaseModel):

    __tablename__ = 'phrase_pinyin'

    id = Column(Integer, primary_key=True)
    phrase = Column(String(20), nullable=False)
    pinyin = Column(String(100), nullable=False)
    probability = Column(Float, nullable=False)

    @classmethod
    def add(cls, phrase, pinyin, probability):
        session = PhraseSession()
        record = cls(phrase=phrase, pinyin=pinyin, probability=probability)
        session.add(record)
        session.commit()
        return record

    @classmethod
    def get_by_pinyin(cls, pinyin):
        session = PhraseSession()
        result = session.query(cls).filter(cls.pinyin == pinyin).all()
        session.commit()
        return {x.phrase: x.probability for x in result}

class Pinyin(BaseModel):

    __tablename__ = 'pinyin'

    id = Column(Integer, primary_key=True)
    pinyin = Column(String(100), nullable=False)
    probability = Column(Float, nullable=False)

    @classmethod
    def add(cls, pinyin, probability):
        session = PhraseSession()
        record = cls(pinyin=pinyin, probability=probability)
        session.add(record)
        session.commit()
        return record

    @classmethod
    def load(cls):
        session = PhraseSession()
        result = session.query(cls.pinyin, cls.probability).all()
        session.commit()
        return {py: prob for py, prob in result}

def init_phrase_tables():
    """
    创建表
    """
    if os.path.exists(db_name):
        os.remove(db_name)

    with open(db_name, 'w') as f:
        pass

    BaseModel.metadata.create_all(bind=engine, tables=[PhrasePinyin.__table__,
                                                       Pinyin.__table__])


import pickle
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import numpy as np

def binary_prob_plots(df):
    fig, ax = plt.subplots(1,2, figsize=(8,4))
    ax[0].plot(df.P_SLAB, df.SLAB, 'ok')
    ax[0].set_title('slab avalanche')
    ax[0].set_xlabel('p(slab)')
    ax[0].set_ylabel('events')
    ax[1].plot(df.P_WET, df.WET, 'ok')
    ax[1].set_title('wet avalanche')
    ax[1].set_xlabel('p(wet)')
    plt.tight_layout()
    plt.show()

if __name__=='__main__':
    # load data
    df = pickle.load( open( "pkl/nsanjuan_data.p", "rb"))

    # test for nans
    na_per_col = np.sum(df.isna())

    # probability plots
    binary_prob_plots(df)

    # types of features
    cols = list(df.columns)
    binary = ['SLAB', 'WET']
    continuous = [x for x in cols if x not in binary]
    # feature histograms: binary
    for col in binary:
        data = df[col].dropna()
        plt.hist(data)
        plt.title(col)
        plt.savefig('feature_hists/{}.png'.format(col),dpi=250)
        plt.close()

    # feature histograms: continuous,data w/o nan
    for col in continuous:
        data = df[col].dropna()
        plt.hist(data, bins=20)
        plt.title(col)
        plt.savefig('feature_hists_nsj/{}.png'.format(col),dpi=250)
        plt.close()


from pypresence import Presence
import datetime

client_id = '702984897496875072'
RPC = Presence(client_id)
already_enabled = False
already_disabled = True
start_time = None
LastTrack = None

def enable_RPC():
    global already_enabled,already_disabled
    if already_enabled == False:
        RPC.connect()
        print('Connected with Discord')
        already_enabled = True
        already_disabled = False

def update_Status(track, title, artist, time_remaining, username, artwork, buttonIsEnabled):
    global start_time, LastTrack
    if len(title) < 2:
        title = title+"  "
    if LastTrack == track:
        return

    print("Now Playing: " + track)
    start_time = datetime.datetime.now().timestamp()
    LastTrack = track

    time_remaining = str(time_remaining)[0:3]
    if time_remaining == '0':
        time_remaining = None
    else:
        time_remaining = float(time_remaining) + start_time

    lastfmProfileButton = None
    if buttonIsEnabled:
        lastfmProfileButton = [{"label": "View Last.fm Profile", "url": str("https://www.last.fm/user/" + username)}]

    RPC.update(details=title, state=track, end=time_remaining,
               large_image=artwork, large_text=track, buttons=lastfmProfileButton)

def disable_RPC():
    global already_enabled
    global already_disabled
    global LastTrack
    if already_disabled == False:
        RPC.clear()
        RPC.close()
        print('Disconnected from Discord due to inactivity on Last.fm')
        already_disabled = True
        already_enabled = False
        LastTrack = None

def disconnect():
    global already_enabled
    global already_disabled
    if already_disabled == False:
        RPC.clear()
        RPC.close()
        print('Disconnected from Discord')
        already_disabled = True
        already_enabled = False


from collections import deque

materials = [int(el) for el in input().split()]
magic_levels = deque(int(el) for el in input().split())

total_gifts = {"Diamond Jewellery": 0, "Gemstone": 0, "Gold": 0, "Porcelain Sculpture": 0}

def checker(total):
    if 100 <= total <= 199:
        total_gifts["Gemstone"] += 1
    elif 200 <= total <= 299:
        total_gifts["Porcelain Sculpture"] += 1
    elif 300 <= total <= 399:
        total_gifts["Gold"] += 1
    elif 400 <= total <= 499:
        total_gifts["Diamond Jewellery"] += 1
    else:
        return False
    materials.pop()
    magic_levels.popleft()
    return True

while materials and magic_levels:
    current_sum = materials[-1] + magic_levels[0]
    if not checker(current_sum):
        if current_sum < 100:
            if current_sum % 2 == 0:
                current_sum = materials[-1] * 2 + magic_levels[0] * 3
                if not checker(current_sum):
                    materials.pop()
                    magic_levels.popleft()
            else:
                current_sum = materials[-1] * 2 + magic_levels[0] * 2
                if not checker(current_sum):
                    materials.pop()
                    magic_levels.popleft()
        elif current_sum > 499:
            current_sum = (materials[-1] + magic_levels[0]) / 2
            if not checker(current_sum):
                materials.pop()
                magic_levels.popleft()
        else:
            materials.pop()
            magic_levels.popleft()

if (total_gifts["Gemstone"] > 0 and total_gifts["Porcelain Sculpture"] > 0) or (
        total_gifts["Gold"] > 0 and total_gifts["Diamond Jewellery"] > 0):
    print("The wedding presents are made!")
else:
    print("Aladdin does not have enough wedding presents.")
if materials:
    print(f"Materials left: {', '.join(map(str, materials))}")
if magic_levels:
    print(f"Magic left: {', '.join(map(str, magic_levels))}")

for key, value in total_gifts.items():
    if not value == 0:
        print(f"{key}: {value}")


import os
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from wordcloud import WordCloud

java_path = "C:/Program Files/Java/jdk1.8.0_161/bin/java.exe"
os.environ['JAVAHOME'] = java_path

def exploratory_data_analysis(df):
    print("exploratory_data_analysis")
    print("Number of reviews:", len(df))

    # Score Distribution
    ax = plt.axes()
    sns.countplot(df.score, ax=ax)
    ax.set_title('Score Distribution')
    plt.show()

    print("Average Score: ", np.mean(df.score))
    print("Median Score: ", np.median(df.score))

    # creo un nuovo attributo opinion
    # negative = review(1-3), positive=(4-5)
    df.ix[df.score == 3, 'opinion'] = "neutral"
    df.ix[df.score > 3, 'opinion'] = "positive"
    df.ix[df.score < 3, 'opinion'] = "negative"

    ## opinion distribution
    ax = plt.axes()
    sns.countplot(df.opinion, ax=ax)
    ax.set_title('Sentiment Positive vs Negative Distribution')
    plt.show()

    ## userid distribution
    # ax = plt.axes()
    # sns.countplot(df.userid, ax=ax)
    # ax.set_title('UserId Distribution')
    # plt.show()
    #
    # ## productid distribution
    # ax = plt.axes()
    # sns.countplot(df.userid, ax=ax)
    # ax.set_title('ProductId Distribution')
    # plt.show()

    print("Proportion of positive review:", len(df[df.opinion == "positive"]) / len(df))
    print("Proportion of neutral review:", len(df[df.opinion == "neutral"]) / len(df))
    print("Proportion of negative review:", len(df[df.opinion == "negative"]) / len(df))
    # 77% of the fine food reviews are considered as positive
    # and 23% of them are considered as negative.

    reviews = df.text.values
    labels = df.opinion.values

    ### TEXT REVIEWS ###

    if df.opinion[1] == "positive":
        print("positive" + "\t" + reviews[1][:90] + "...")
    else:
        print("negative" + "\t " + reviews[1][:90] + "...")

    ### Exploratory Visualization ###
    # mi creo un vocabolario di parole positive e negative

    positive_reviews = [reviews[i] for i in range(len(reviews)) if labels[i] == "positive"]
    negative_reviews = [reviews[i] for i in range(len(reviews)) if labels[i] == "negative"]

    ### positive
    cnt_positve = Counter()

    for row in positive_reviews:
        cnt_positve.update(row.split(" "))
    print("Vocabulary size for positve reviews:", len(cnt_positve.keys()))

    ### negative
    cnt_negative = Counter()

    for row in negative_reviews:
        cnt_negative.update(row.split(" "))
    print("Vocabulary size for positve reviews:", len(cnt_negative.keys()))

    cnt_total = Counter()

    for row in reviews:
        cnt_total.update(row.split(" "))

    pos_neg_ratio = Counter()
    vocab_pos_neg = (set(cnt_positve.keys())).intersection(set(cnt_negative.keys()))
    for word in vocab_pos_neg:
        if cnt_total[word] > 100:
            ratio = cnt_positve[word] / float(cnt_negative[word] + 1)
            if ratio > 1:
                pos_neg_ratio[word] = np.log(ratio)
            else:
                pos_neg_ratio[word] = -np.log(1 / (ratio + 0.01))

    positive_dict = {}
    for word, cnt in pos_neg_ratio.items():
        if (cnt > 1):
            positive_dict[word] = cnt

    #### positive WORDCLOUD #####
    # wordcloud = WordCloud()
    # wordcloud.generate_from_frequencies(frequencies=positive_dict)
    #
    # plt.figure(figsize=(10, 10))
    # plt.imshow(wordcloud, interpolation="bilinear")
    # plt.axis("off")
    # ax = plt.axes()
    # ax.set_title('Word Cloud with the Highest Positive/Negative Ratio')
    # plt.show()

    negative_dict = {}
    for word, cnt in pos_neg_ratio.items():
        if (cnt < 1) & (cnt > 0):
            negative_dict[word] = -np.log(cnt)

    #### negative WORDCLOUD #####
    wordcloud = WordCloud(background_color='black',
                          width=2500,
                          height=2000)
    wordcloud.generate_from_frequencies(frequencies=negative_dict)
    plt.figure(figsize=(10, 10))
    plt.imshow(wordcloud, interpolation="bilinear")
    plt.axis("off")
    ax = plt.axes()
    ax.set_title('Word Cloud with the Lowest Positive/Negative Ratio')
    plt.show()

    print("END EXPLORATORY ANALYSIS")

# vocabulary reduction function to reduce
# the vocabulary based on min frequency or polarity.
def vocabulary_reduction(reviews, labels, min_freq=10, polarity_cut_off=0.1):
    pos_count = Counter()
    neg_count = Counter()
    tot_count = Counter()

    for i in range(len(reviews)):
        for word in reviews[i].split():
            tot_count[word] += 1
            if labels[i] == 1:
                pos_count[word] += 1
            else:
                neg_count[word] += 1

                # Identify words with frequency greater than min_freq
    vocab_freq = []
    for word in tot_count.keys():
        if tot_count[word] > min_freq:
            vocab_freq.append(word)

            # Use polarity to reduce vocab
    pos_neg_ratio = Counter()
    vocab_pos_neg = (set(pos_count.keys())).intersection(set(neg_count.keys()))
    for word in vocab_pos_neg:
        if tot_count[word] > 100:
            ratio = pos_count[word] / float(neg_count[word] + 1)
            if ratio > 1:
                pos_neg_ratio[word] = np.log(ratio)
            else:
                pos_neg_ratio[word] = -np.log(1 / (ratio + 0.01))

    mean_ratio = np.mean(list(pos_neg_ratio.values()))

    vocab_polarity = []
    for word in pos_neg_ratio.keys():
        if (pos_neg_ratio[word] < (mean_ratio - polarity_cut_off)) or (
                pos_neg_ratio[word] > (mean_ratio + polarity_cut_off)):
            vocab_polarity.append(word)

    vocab_rm_polarity = set(pos_neg_ratio.keys()).difference(vocab_polarity)
    vocab_reduced = (set(vocab_freq)).difference(set(vocab_rm_polarity))

    reviews_cleaned = []

    for review in reviews:
        review_temp = [word for word in review.split() if word in vocab_reduced]
        reviews_cleaned.append(' '.join(review_temp))

    return reviews_cleaned

if __name__ == "__main__":
    df = pd.read_csv("Dataset/food.tsv", sep="\t", encoding='latin-1')

    # recensioni negative
    df = df.loc[df['score'] == (1 or 2)]
    df = df.groupby('productid').agg(['mean', 'count'])

    print("ciao")

    asd = df.groupby('productid').score.mean().reset_index()
    asd2 = df.productid.value_counts()
    asd3 = asd.merge(asd2.to_frame(), left_on='productid', right_index=True)
    asd4 = asd3.sort_values('score')
    df1 = df.loc[df['productid'] == "B001E96JY2"]

    # df = pd.read_csv("cleanedTextCSV.csv", sep="\t", encoding='latin-1')
    exploratory_data_analysis(df)
    # data_preprocessing(df)
    # df_train = df = df.loc[df['productid'] == "B000DZFMEQ"]
    # df_test = df[201:250]
    # # aspect_based(df_train, df_test)
    # aspect2(df_train, df_test)


import unittest
import traceback
from datetime import datetime

from tests import resources
from pyfootball.models.fixture import Fixture

class TestFixture(unittest.TestCase):
    def test_init(self):
        try:
            Fixture(resources.FIXTURE)
            Fixture(resources.FIXTURE_WITH_HT_AND_ODDS)
        except:
            self.fail()

    def test_init_bad_data(self):
        with self.assertRaises(KeyError):
            Fixture({"a": "dict", "that": "has", "bad": "data"})

class TestFixtureAfterInit(unittest.TestCase):
    def setUp(self):
        try:
            self.fixture = Fixture(resources.FIXTURE)
            self.fixture_full = Fixture(resources.FIXTURE_WITH_HT_AND_ODDS)
        except Exception:
            print("Setting up Fixture object(s) failed:")
            traceback.print_exc()
            self.skipTest(TestFixtureAfterInit)

    def tearDown(self):
        self.fixture = None
        self.fixture_full = None

    def test_data_types(self):
        integers = ['matchday', 'home_team_id', 'away_team_id',
                    'competition_id', 'home_team_goals, away_team_goals']
        dicts = ['result', 'odds']
        datetimes = ['date']

        for attr, val in self.fixture.__dict__.items():
            if attr in integers:
                self.assertIsInstance(val, int)
            elif attr in datetimes:
                self.assertIsInstance(val, datetime)
            elif attr in dicts:
                if attr is 'result':
                    for key, value in val.items():  # results dict
                        if key is 'half_time':
                            self.assertEqual(value, None)
                        else:
                            self.assertIsInstance(value, int)
                elif attr is 'odds':
                    self.assertEqual(val, None)
            else:  # Strings
                self.assertIsInstance(val, str)

        for attr, val in self.fixture_full.__dict__.items():
            if attr in integers:
                self.assertIsInstance(val, int)
            elif attr in datetimes:
                self.assertIsInstance(val, datetime)
            elif attr in dicts:
                if attr is 'result':
                    for key, value in val.items():  # results dict
                        if key is 'half_time':
                            for k, v in value.items():  # half_time dict
                                self.assertIsInstance(v, int)
                        else:
                            self.assertIsInstance(value, int)
                elif attr is 'odds':
                    for key, value in val.items():  # odds dict
                        self.assertIsInstance(value, float)
            else:  # Strings
                self.assertIsInstance(val, str)

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


import pandas as pd
import copy
import viz
import numpy as np
import sklearn.linear_model as lm
import evaluation
import features
import features.prep
import trees
from matplotlib import pyplot as plt
import classifier
import seaborn as sns
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree._tree import TREE_LEAF
from sklearn.tree import _tree

pd.set_option('display.max_columns',30)

if __name__ == '__main__':
    data = pd.read_csv("../data/housing.csv",sep=";")
    #print(data.head())
    data = features.prep.dropnans_newidx(data)
    #print(data.shape)
    data = features.prep.convert_cats(data,"ocean_proximity")

    #####################
    data = data.sample(1000)
    #data = data[["latitude","longitude","median_income","median_house_value"]]
    ###################

    train_X, val_X, test_X, train_y, val_y, test_y = features.prep.split_train_val_test(data, "median_house_value",[0.6,0.2,0.2])
    #print(train_X.shape)
    #print(val_X.shape)
    #print(test_X.shape)
    # train_X, test_X, train_y, test_y = features.prep.split_train_test(data, "median_house_value")
    # scaling is not necessary for decision trees
    small_dTree = DecisionTreeRegressor(max_depth=2)
    small_dTree.fit(train_X[["median_income"]], train_y)
    small_dTree_pred = small_dTree.predict(train_X[["median_income"]])
    trees.tree_to_code(small_dTree, ['median_income'])
    print(f" feature: {small_dTree.tree_.feature}")
    print(f" child left: {small_dTree.tree_.children_left}")
    print(f" child left idx 4: {small_dTree.tree_.children_left[4]}")
    print(f" child right: {small_dTree.tree_.children_right}")
    print(f" tree value idx 2: {small_dTree.tree_.value[2]}")
    print(f" ")
    rss = np.sum((train_y - small_dTree_pred) ** 2)
    print(f" rss: {rss}")
    thresh_node = small_dTree.tree_.threshold[1]
    samples_in_leaves = small_dTree.apply(train_X[["median_income"]]) # gibt pro Zeile die Zugehörigkeit zum terminal node aus
    print(samples_in_leaves)
    print(pd.value_counts((samples_in_leaves)))
    #use impurity measure?
    print(small_dTree.tree_.children_left[2])
    print(f" child left idx 2: {small_dTree.tree_.children_left[2]}")
    print(f" tree leaf: {_tree.TREE_LEAF}")
    trees.depth_first(small_dTree.tree_, 0)
    print(f" node count: {small_dTree.tree_.node_count}") # total nr of nodes
    print(f" node impurity idx 0: {small_dTree.tree_.impurity[0]}")
    print(f" impurity * n samples at idx 0: {small_dTree.tree_.impurity[0]*small_dTree.tree_.n_node_samples[0]}")
    rss_mean = np.sum((train_y - np.mean(train_y))**2)
    print(f" pred error of mean: {rss_mean}")
    print(f" n node samples idx 2: {small_dTree.tree_.n_node_samples[2]}")

    print(f" n node samples idx 1: {small_dTree.tree_.n_node_samples[1]}")
    print(f" n node samples idx 3: {small_dTree.tree_.n_node_samples[3]}")

    # finding alpha --> best subtree from pruning

    dTree = DecisionTreeRegressor(min_samples_leaf=3)
    dTree.fit(train_X, train_y)

    tree_array = [dTree]

    num_nodes = dTree.tree_.capacity
    index = 0
    alpha = 0
    k = 1

    alpha_array = [alpha]
    num_nodes_array = [num_nodes]

    while num_nodes > 1:
        tree_array.append(copy.deepcopy(tree_array[k - 1]))

        min_node_idx, min_gk = trees.determine_alpha(tree_array[k].tree_)
        alpha_array.append(min_gk)
        trees.prune(tree_array[k].tree_, min_node_idx)

        num_nodes = sum(1 * (tree_array[k].tree_.n_node_samples != 0))
        num_nodes_array.append(num_nodes)
        k += 1
    print("alpha array:")
    print(alpha_array)

    error_rates = pd.DataFrame(columns=["tree", "MSE", "RMSE", "R2", "RMSE % of mean", "Cali"])
    error_rates_tr = pd.DataFrame(columns=["tree", "MSE", "RMSE", "R2", "RMSE % of mean", "Cali"])

    for t in tree_array:
        pred = t.predict(val_X)
        pred_training = t.predict(train_X)
        idx = tree_array.index(t)
        new_row = pd.concat([pd.Series(idx,name="tree"),evaluation.save_errors(val_y, pred)],axis=1)
        new_row_tr = pd.concat([pd.Series(idx,name="tree"),evaluation.save_errors(train_y, pred_training)],axis=1)
        error_rates = error_rates.append(new_row,sort=False)
        error_rates_tr = error_rates_tr.append(new_row_tr,sort=False)

    print("sorted error rates for val:\n")
    err_sorted = error_rates.sort_values(["R2","RMSE"],ascending=[False,True])
    print(err_sorted)
    best_tree_nr = err_sorted.iloc[0,0]
    best_tree = tree_array[best_tree_nr]

    fig2, axes2 = plt.subplots(3,2,figsize=(10,12))
    ax2 = axes2.flatten()
    for col in error_rates.iloc[:,1:6].columns.values:
        axnr = error_rates.columns.get_loc(col) -1
        ax2[axnr].set_title(col)
        ax2[axnr].scatter(alpha_array, error_rates[col],label="val")
        ax2[axnr].scatter(alpha_array, error_rates_tr[col],label="train")
        ax2[axnr].legend()
        ax2[axnr].set_xlim([0, 0.6e+12])
        ax2[axnr].set_xlabel("alpha")
    fig2.tight_layout()
    a = viz.ScrollableWindow(fig2)

    fig3, axes3 = plt.subplots(3, 2, figsize=(10, 12))
    ax3 = axes3.flatten()
    for col in error_rates.iloc[:, 1:6].columns.values:
        axnr = error_rates.columns.get_loc(col) - 1
        ax3[axnr].set_title(col)
        ax3[axnr].set_xlabel("subtree")
        ax3[axnr].scatter(error_rates["tree"],error_rates[col],label="val")
        ax3[axnr].scatter(error_rates_tr["tree"], error_rates_tr[col],label="train")
        ax3[axnr].legend()

    fig3.tight_layout()
    b = viz.ScrollableWindow(fig3)

    random_dTree = RandomForestRegressor(min_samples_leaf=3)
    random_dTree.fit(train_X, train_y)
    rand_pred_val = random_dTree.predict(val_X)
    rand_pred_tr = random_dTree.predict(train_X)
    rand_error_rates_val = pd.DataFrame(columns=["type", "MSE", "RMSE", "R2", "RMSE % of mean", "Cali"])
    rand_error_rates_tr = pd.DataFrame(columns=["type", "MSE", "RMSE", "R2", "RMSE % of mean", "Cali"])

    rand_new_row = pd.concat([pd.Series("val",name="type"),evaluation.save_errors(val_y, rand_pred_val)],axis=1)
    rand_new_row_tr = pd.concat([pd.Series("train",name="type"),evaluation.save_errors(train_y, rand_pred_tr)],axis=1)
    rand_error_rates = rand_error_rates_val.append(rand_new_row,sort=False)
    rand_error_rates_tr = rand_error_rates_tr.append(rand_new_row_tr,sort=False)
    print("pruned error rates val:")
    print(error_rates.loc[error_rates["tree"] == best_tree_nr,:])
    print("pruned error rates train:")
    print(error_rates_tr.loc[error_rates["tree"] == best_tree_nr,:])
    print("rand error rates val:")
    print(rand_error_rates)
    print("rand error rates train:")
    print(rand_error_rates_tr)

    fig5, axes5 = plt.subplots(3, 2, figsize=(10, 12))
    ax5 = axes5.flatten()
    for col in error_rates.iloc[:, 1:6].columns.values:
        axnr = error_rates.columns.get_loc(col) - 1
        ax5[axnr].set_title(col)
        ax5[axnr].set_xlabel("subtree")
        ax5[axnr].scatter("pruned",error_rates.loc[error_rates["tree"] == best_tree_nr,col],label="pruned val")
        ax5[axnr].scatter("pruned", error_rates_tr.loc[error_rates["tree"] == best_tree_nr,col],label="pruned train")
        ax5[axnr].scatter("rand", rand_error_rates[col], label="rand val")
        ax5[axnr].scatter("rand", rand_error_rates_tr[col], label="rand train")

        ax5[axnr].legend()

    fig5.tight_layout()
    b = viz.ScrollableWindow(fig5)

"""
    fig4, axes4 = plt.subplots(3, 2, figsize=(10, 12))
    ax4 = axes4.flatten()
    for col in error_rates.iloc[:, 1:6].columns.values:
        axnr = error_rates.columns.get_loc(col) - 1
        ax4[axnr].set_title(col)
        ax4[axnr].set_xlabel("num nodes")
        ax4[axnr].scatter(num_nodes_array,error_rates[col])

    fig4.tight_layout()
    c = viz.ScrollableWindow(fig4)

    #dForest = RandomForestRegressor(max_depth=max_depth, n_estimators=n_estimators)

    fig, axes = plt.subplots(10,2,figsize=(10, 30))
    ax = axes.flatten()
    ax[0].set_title('Real Test Values')
    ax[0].scatter(x=test_X['longitude'], y=test_X['latitude'], c=test_y, s=10, cmap='hot');
    error_rates = pd.DataFrame(columns =["max_depth","MSE","RMSE","R2","RMSE % of mean","Cali"])

    for max_depth in range(1,20):
        dTree = DecisionTreeRegressor(max_depth=max_depth)
        dTree.fit(train_X, train_y)
        pred = dTree.predict(test_X)
        title = "Tree Prediction for max_depth = " + str(max_depth)
        ax[max_depth].set_title(title)
        ax[max_depth].scatter(x=test_X['longitude'], y=test_X['latitude'], c=pred, s=10, cmap='hot');
        new_row = pd.concat([pd.Series(max_depth,name="max_depth"),evaluation.save_errors(test_y, pred)],axis=1)
        error_rates = error_rates.append(new_row,sort=False)
    fig.tight_layout()
    a = viz.ScrollableWindow(fig)
    print(error_rates)
    plt.close("all")

    fig2, axes2 = plt.subplots(3,2,figsize=(10,12))
    ax2 = axes2.flatten()
    for col in error_rates.iloc[:,1:6].columns.values:
        axnr = error_rates.columns.get_loc(col) -1
        ax2[axnr].set_title(col)
        ax2[axnr].set_xlabel("max_depth")
        ax2[axnr].plot(error_rates["max_depth"],error_rates[col])

    fig2.tight_layout()
    b = viz.ScrollableWindow(fig2)
    #plt.show()
    plt.close('all')
    """


"""
Language modeling evaluation script
"""
import json
import logging
import math
import os
import sys
from time import time

import pandas as pd
from transformers import (
    AutoConfig,
    AutoModelWithLMHead,
    DataCollatorForPermutationLanguageModeling,
    HfArgumentParser,
    set_seed,
)

from terminator.args import CustomTrainingArguments, EvalArguments
from terminator.collators import (
    ConditionalGenerationEvaluationCollator,
    PropertyCollator,
)
from terminator.datasets import get_dataset
from terminator.evaluator import Evaluator
from terminator.property_predictors import PREDICT_FACTORY
from terminator.tokenization import ExpressionBertTokenizer
from terminator.trainer import get_trainer_dict
from terminator.utils import (
    disable_rdkit_logging,
    find_safe_path,
    get_latest_checkpoint,
    get_equispaced_ranges,
)

logger = logging.getLogger(__name__)

# setup logging
logging.basicConfig(stream=sys.stdout, level=logging.INFO)

def main():

    parser = HfArgumentParser((CustomTrainingArguments, EvalArguments))
    training_args, eval_args = parser.parse_args_into_dataclasses()

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO,
    )

    with open(eval_args.param_path, "r") as f:
        eval_params = json.load(f)

    param_filename = eval_args.param_path.split("/")[-1].split(".json")[0]

    # Wrap into args to be safe
    eval_args.__dict__.update(eval_params)

    # NOTE: Results will be stored in model folder
    model_dir = training_args.output_dir
    if "checkpoint" not in model_dir:
        model_dir = get_latest_checkpoint(
            model_dir, must_contain=eval_params.get("checkpoint-str", "best")
        )

    config_name = os.path.join(model_dir, "config.json")
    with open(config_name, "r") as f:
        model_params = json.load(f)

    config = AutoConfig.from_pretrained(
        config_name, mem_len=model_params.get("mem_len", 1024)
    )

    tokenizer = ExpressionBertTokenizer.from_pretrained(model_dir)
    sep = tokenizer.expression_separator

    model = AutoModelWithLMHead.from_pretrained(
        model_dir, from_tf=bool(".ckpt" in model_dir), config=config
    )
    logger.info(f"Model restored from {model_dir}")

    model.resize_token_embeddings(len(tokenizer))

    if eval_params.get("block_size", -1) <= 0:
        eval_params["block_size"] = tokenizer.max_len
        # Our input block size will be the max possible for the model
    else:
        eval_params["block_size"] = min(training_args.block_size, tokenizer.max_len)

    # Get datasets
    eval_dataset = get_dataset(
        eval_args.eval_file,
        block_size=eval_params["block_size"],
        tokenizer=tokenizer,
        line_by_line=eval_params.get("line_by_line", True),
    )

    logger.info(f"Dataset size {len(eval_dataset)}.")
    num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    logger.info(f"Number of parameters {num_params} of type {type(model)}")

    plm_prob = eval_params["plm_probability"]
    perplexity_plm_prob = eval_params.get("perplexity_plm_prob", 0.2)
    # NOTE: This collator does not provide an attention mask (unlike the refined training
    # collators which prevent attention on padding), however, the model will largely
    # ignore the paddings.
    vanilla_collator = DataCollatorForPermutationLanguageModeling(
        tokenizer=tokenizer,
        plm_probability=perplexity_plm_prob,
        max_span_length=eval_params["max_span_length"],
    )

    custom_trainer_params = get_trainer_dict(model_params)

    # Initialize our Evaluator
    evaluator = Evaluator(
        model=model,
        args=training_args,
        eval_params=eval_params,
        data_collator=vanilla_collator,
        eval_dataset=eval_dataset,
        tokenizer=tokenizer,
        prediction_loss_only=False,
        **custom_trainer_params,
    )

    # Evaluation
    result_dir = os.path.join(model_dir, "results")
    os.makedirs(result_dir, exist_ok=True)
    eval_filename = eval_args.eval_file.split("/")[-1].split("_")[-1].split(".")[0]
    logger.info("*** Evaluate perplexity ***")

    with open(eval_args.eval_file, "r") as f:
        prefix = sep.join(f.readline().split(sep)[:-1]) + sep

    # Set seed
    if eval_params.get("set_seed", True):
        set_seed(eval_params.get("seed", int(time())))

    eval_output = evaluator.evaluate()
    perplexity = math.exp(eval_output["eval_loss"])
    results = {"perplexity": perplexity}
    path = os.path.join(
        result_dir, f"{eval_filename}_perplexity_plm_{perplexity_plm_prob}.txt"
    )

    with open(find_safe_path(path), "w") as writer:
        logger.info("***** Eval results *****")
        for key in sorted(results.keys()):
            logger.info("  %s = %s", key, str(results[key]))
            writer.write("%s = %s\n" % (key, str(results[key])))

    disable_rdkit_logging()
    property_results = []
    properties = eval_params["property_tokens"]
    orders = eval_params.get("property_token_masking_order", None)
    tokens_to_mask = eval_params.get("property_tokens_to_mask", None)
    conditioning_ranges = eval_params.get(
        "conditioning_range",
        get_equispaced_ranges(
            eval_args.eval_file,
            properties,
            precisions=eval_params.get("property_precisions", [2] * len(properties)),
        ),
    )
    logger.info(f"Conditioning range is {conditioning_ranges}")

    # If the token masking orders is not specified we just evaluate all properties together
    if not orders:
        property_collator = PropertyCollator(
            tokenizer=tokenizer,
            property_tokens=properties,
            num_tokens_to_mask=tokens_to_mask,
            mask_token_order=orders,
        )
        ps, rs = evaluator.multi_property_prediction(
            property_collator,
            save_path=os.path.join(result_dir, eval_filename),
            rmse_factor=eval_params.get("rmse_factor", 1),
        )
    else:

        for prop, order, mask in zip(properties, orders, tokens_to_mask):
            logger.info(f"*** Evaluate property {prop} ***")

            for to_mask in mask:

                # We iteratively make the task harder by masking 1-4 tokens.
                # The order of this is determined by `property_token_masking_order`.
                property_collator = PropertyCollator(
                    tokenizer=tokenizer,
                    property_tokens=[prop],
                    num_tokens_to_mask=[to_mask],
                    mask_token_order=[order],
                )
                print(f"Masking {to_mask} in order {order}")
                ps, rs, ss = evaluator.property_prediction(
                    property_collator,
                    save_path=os.path.join(
                        result_dir, f"{prop[1:-1]}_{eval_filename}_mask_{to_mask}.csv"
                    ),
                    rmse_factor=eval_params.get("rmse_factor", 1),
                )
                for p, r, s, n in zip(ps, rs, ss, ["Greedy", "Sampling", "Beam"]):
                    prop_res_dict = {
                        "prop": prop[1:-1],
                        "pearson": p,
                        "spearman": s,
                        "rmse": r,
                        "search": n,
                        "num_masked": to_mask,
                    }
                    property_results.append(prop_res_dict)

            pd.DataFrame(property_results).to_csv(
                os.path.join(result_dir, f"property_prediction_{eval_filename}.csv")
            )
    for prop, cr in zip(properties, conditioning_ranges):
        logger.info(f"Evaluating conditional generation for {prop} with {cr}")
        conditional_generation_collator = ConditionalGenerationEvaluationCollator(
            tokenizer=tokenizer,
            property_token=prop,
            conditioning_range=cr,
            plm_probability=plm_prob,
            max_span_length=eval_params["max_span_length"],
            entity_to_mask=eval_params.get("entity_to_mask", None),
            entity_separator_token=eval_params.get("entity_separator_token", None),
        )

        # Retrieve the property prediction function from dictionary
        if prop[1:-1] in PREDICT_FACTORY.keys():
            evaluate_fn = PREDICT_FACTORY[prop[1:-1]]
            logger.info(f"Found property predictor for {prop}")
            property_collator = None
        else:
            # If unavailable property is predicted
            evaluate_fn = None

            if orders:
                # In single property prediction mode we just mask the property
                property_collator = PropertyCollator(
                    tokenizer=tokenizer,
                    property_tokens=[prop],
                    num_tokens_to_mask=[-1],
                    mask_token_order=None,
                )
            else:
                # in this case, we use the property predictor from above where all tokens are masked
                pass

            logger.info(
                f"No property predictor for {prop}, using model itself for evaluation"
            )

        evaluator.conditional_generation(
            conditional_generation_collator,
            save_path=os.path.join(
                result_dir,
                f"{prop[1:-1]}_conditional_generation_{param_filename}_{eval_filename}.csv",
            ),
            passed_eval_fn=evaluate_fn,
            property_collator=property_collator,
            denormalize_params=eval_params.get("denormalize", {}).get(prop, None),
            prefix=prefix,
        )

    print("Done, shutting down.")

if __name__ == "__main__":
    main()


# -*- encoding: utf-8 -*-
from odoo import fields, models, _
from odoo.exceptions import Warning
import logging
log = logging.getLogger(__name__)

class StockMove(models.Model):
	_inherit = 'stock.move'

	def get_despatch_product_name(self):
		return self.description_picking if self.description_picking else self.product_id.display_name

class StockPicking(models.Model):
	_inherit = 'stock.picking'

	expedition_id = fields.Many2one('logistic.expedition', string='Expedition')
	despatch_id = fields.Many2one('logistic.despatch', string='Despatch', copy=False)

	def _prepare_despatch(self):
		picking = self
		partner_id = picking.partner_id.id
		delivery_address_id = picking.partner_id.id

		if picking.partner_id.parent_id:
			partner_id = picking.partner_id.parent_id.id
			delivery_address_id = picking.partner_id.parent_id.id

		if not picking.partner_id:
			delivery_address_id = picking.picking_type_id.warehouse_id.partner_id.id

		journal_id = self.env['account.journal'].search([('type','=','general'),('code','=like','T%')])
		if not journal_id:
			journal_id = self.env['account.journal'].search([('type','=','general')])

		if not journal_id:
			raise Warning('Journals not found for despatchs')

		origin_address_id = False
		delivery_address_id = False
		warehouse_id = False
		if picking.picking_type_id.code=='incoming':
			origin_address_id = picking.partner_id.id
			delivery_address_id = picking.picking_type_id.warehouse_id.partner_id.id
			warehouse_id = picking.picking_type_id.warehouse_id
		elif picking.picking_type_id.code=='outgoing':
			origin_address_id = picking.picking_type_id.warehouse_id.partner_id.id
			delivery_address_id = picking.partner_id.id
			warehouse_id = picking.picking_type_id.warehouse_id
		elif picking.picking_type_id.code=='internal':
			origin_address_id = picking.picking_type_id.warehouse_id.partner_id.id
			delivery_address_id = origin_address_id
			warehouse_id = picking.picking_type_id.warehouse_id
		despatch = {
			'warehouse_id': warehouse_id.id if warehouse_id else False,
			'journal_id': warehouse_id.despatch_journal_ids[0].id if warehouse_id.despatch_journal_ids else journal_id[0].id,
			'company_id':picking.company_id.id,
			'partner_id':partner_id,
			'origin_address_id':origin_address_id,
			'delivery_address_id':delivery_address_id,
			'line_ids':[],
			'picking_ids':[[6,0,[picking.id]]]
		}
		if picking.note:
			despatch['note'] = picking.note
		if self._context.get('force_issue_date', False):
			despatch['issue_date'] = self._context.get('force_issue_date')
		if self._context.get('force_start_date', False):
			despatch['start_date'] = self._context.get('force_start_date')
		if self._context.get('force_journal_id', False):
			despatch['journal_id'] = self._context.get('force_journal_id')
		if self._context.get('force_shipment_reason', False):
			despatch['shipment_reason'] = self._context.get('force_shipment_reason')
		if self._context.get('force_carrier_id', False):
			despatch['carrier_id'] = self._context.get('force_carrier_id')
		if self._context.get('force_vehicle_id', False):
			despatch['vehicle_id'] = self._context.get('force_vehicle_id')
		if self._context.get('force_driver_id', False):
			despatch['driver_id'] = self._context.get('force_driver_id')
		if self._context.get('force_origin_address_id', False):
			despatch['origin_address_id'] = self._context.get('force_origin_address_id')
		if self._context.get('force_delivery_address_id', False):
			despatch['delivery_address_id'] = self._context.get('force_delivery_address_id')
		if self._context.get('force_internal_number', False):
			despatch['internal_number'] = self._context.get('force_internal_number')

		weight_total = 0
		for line in picking.move_lines:
			product_name = line.get_despatch_product_name()
			'''if line.sale_line_id:
				product_name = line.sale_line_id.name'''
			despatch['line_ids'].append([0,0,{
				'product_id':line.product_id.id,
				'name':product_name,
				'quantity':line.product_uom_qty,
				'uom_id':line.product_uom.id,
				'weight':line.product_id.weight*line.product_uom_qty ,
				'volume': line.product_id.volume * line.product_uom_qty
				}])
		return despatch

	def generate_logistic_despatch(self):
		despatch_ids = []
		for picking in self:
			if picking.company_id.logistic_picking_done_restrict and picking.state!='done':
				raise Warning(_('Picking status is not done'))
			if picking.despatch_id:
				if picking.despatch_id.state=='open':
					raise Warning(_('This document already has a remission guide'))
			despatch = picking._prepare_despatch()
			_despatch = self.env['logistic.despatch'].create(despatch)
			picking.write({'despatch_id':_despatch.id})
			despatch_ids.append(_despatch.id)
		if self._context.get('force_return_array', False):
			return despatch_ids
		return {
			'type': 'ir.actions.act_window',
			'res_model': 'logistic.despatch',
			'view_mode': 'form',
			'res_id': _despatch.id,
			'target': 'current',
			'flags': {'form': {'action_buttons': True, 'options': {'mode': 'edit'}}}
		}


from django.shortcuts import render, redirect
#from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegistrationForm, UserUpdateForm, ProfileUpdateForm

# Create your views here.
def register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            #messages.success(request, f'Account created for {username}!')
            messages.success(request, f'Your account has been created! You are now able to log in')
            return redirect('login')
    else:
        form = UserRegistrationForm()

    return render(request, 'users/register.html',{'form': form})

@login_required
def profile(request):
    if request.method == 'POST':
        u_from = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST,request.FILES, instance=request.user.profile)
        #a = request.POST.get('username')

        if u_from.is_valid() and p_form.is_valid():
            u_from.save()
            p_form.save()

            messages.success(request, f'Your Profile has been updated Successfully')
            return redirect('login')
    else:
        u_from = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form' : u_from,
        'p_form' : p_form
    }
    return render(request, 'users/profile.html', context)


import pandas as pd
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, plot_confusion_matrix
import graphviz
import matplotlib.pyplot as plt
import seaborn as sns

def readInputFile():
    df = pd.read_csv('D:\Homework\Artificial Intelligent\Lab2\mushrooms.csv', sep=',') # read mushrooms.csv to dataframe
    data = df.drop('class', axis=1).copy()
    X = pd.get_dummies(data, columns=data.columns.values.tolist()).copy()

    res = df['class'].copy()
    res[res == 'p'] = 1 # change p to 1
    res[res == 'e'] = 0 # change e to 0
    y = res.values.tolist()

    return X, y

def prepDataset(X, y):
    ratio = [0.4, 0.6, 0.8, 0.9] # list of ratios (train/total) using for prepare the subsets
    subsetItems = []

    for r in ratio:
        X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=r)
        subsetItems.append({'X_train': X_train, 'X_test': X_test, 'y_train': y_train, 'y_test': y_test})

    return subsetItems # list of objects which contain train sets and test sets for each ratio above

def constructDecisionTree(trainTestSubsets):
    split_ratio = [0.4, 0.6, 0.8, 0.9]
    decisionTrees = []

    for subset, ratio in zip(trainTestSubsets, split_ratio):
        clf = tree.DecisionTreeClassifier(criterion='entropy')
        clf = clf.fit(subset['X_train'], subset['y_train'])
        dot_data = tree.export_graphviz(clf, out_file=None, feature_names=subset['X_train'].columns, class_names=['e','p'], rounded=True, filled=True)
        graph = graphviz.Source(dot_data)
        graph.render('output/train_{}_decision_tree'.format(ratio), cleanup=True) #file's name and file's path for exporting
        decisionTrees.append(clf)

    return decisionTrees # list of decision trees

def printOutAllReports(decisionTrees, testSets):
    for tree, test, i in zip(decisionTrees, testSets, range(1, len(decisionTrees) + 1)):
        y_pred = tree.predict(test['X_test'])
        print("Report #{}: \n".format(i), classification_report(test['y_test'], y_pred, target_names=['e','p']))
        plot_confusion_matrix(tree, test['X_test'], test['y_test'])
        filename = 'output/confusion_matrix_{}.png'.format(i) #file's name and file's path for exporting
        print('Confusion matrix was saved as {}\n'.format(filename))
        plt.savefig(filename)
        plt.clf()
    return

def constructDecisionTreeWithDepth(subsets):
    depthList = [None, 2, 3, 4, 5, 6, 7]
    for depth in depthList:
        # build dicision trees of different depths
        clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=depth)
        clf = clf.fit(subsets[2]['X_train'], subsets[2]['y_train'])
        dot_data = tree.export_graphviz(clf, out_file=None, feature_names=subsets[2]['X_train'].columns, class_names=['e','p'], rounded=True, filled=True)
        graph = graphviz.Source(dot_data)
        graph.render('output/decision_tree_with_depth_{}'.format(depth), cleanup=True) #file's name and file's path for exporting

        # print out classification report and confusion matrix for each tree with corresponding test sets
        y_pred = clf.predict(subsets[2]['X_test'])
        print("Report (depth = {}): \n".format(depth), classification_report(subsets[2]['y_test'], y_pred, target_names=['e','p']))
        plot_confusion_matrix(clf, subsets[2]['X_test'], subsets[2]['y_test'])
        filename = 'output/confusion_matrix_depth_{}.png'.format(depth) #file's name and file's path for exporting
        print('Confusion matrix was saved as {}\n'.format(filename))
        plt.savefig(filename)
        plt.clf()
    return


import os
import numpy as np
import json
import cv2
from tqdm import tqdm
import random

def create_dict():
    dict = {"instruction": [], "input": [], "output": []}
    return dict

def get_obj_list(scene_name_all, scene_name_list_one):

    diff_list = list(set(scene_name_all).difference(set(scene_name_list_one)))
    scene_name_list_one_copy = scene_name_list_one.copy()
    change_number = random.randint(1, int(len(scene_name_list_one_copy) / 2))
    random.shuffle(scene_name_list_one_copy)
    if change_number < len(diff_list):
        new_obj_name_list = scene_name_list_one_copy[:-change_number] + random.sample(diff_list, change_number)
    else:
        new_obj_name_list = scene_name_list_one_copy[:-len(diff_list)] + diff_list

    if len(scene_name_list_one_copy) <= 40:
        change_number = random.randint(int(len(scene_name_list_one_copy) / 2), len(scene_name_list_one_copy))
    else:
        change_number = random.randint(int(len(scene_name_list_one_copy) / 2), 40)
    new_obj_name_list = random.sample(new_obj_name_list, change_number)

    return new_obj_name_list

base_path = os.getcwd()
data_path = os.path.join(base_path, "dataset", "train")
scene_file_list = os.listdir(data_path)

scene_name_list = []

kitchens_train = [f"FloorPlan{i}" for i in range(1, 21)]
living_rooms_train = [f"FloorPlan{200 + i}" for i in range(1, 21)]
bedrooms_train = [f"FloorPlan{300 + i}" for i in range(1, 21)]
bathrooms_train = [f"FloorPlan{400 + i}" for i in range(1, 21)]

scene_name_list.append(kitchens_train)
scene_name_list.append(living_rooms_train)
scene_name_list.append(bedrooms_train)
scene_name_list.append(bathrooms_train)

save_path = "alpaca_6k.json"
json_list = []
for scene_name_one in tqdm(scene_name_list):
    scene_name_all = []
    scene_name_all_list = []
    for scene_one in scene_name_one:
        scene_one_path = os.path.join(data_path, scene_one + "_" + str(0))
        scene_one_name_txt = os.path.join(scene_one_path, "name.txt")
        with open(scene_one_name_txt, "r") as tf:
            name_list_one = tf.read().split(',')
        scene_name_all.extend(name_list_one)
        scene_name_all_list.append(name_list_one)

    scene_name_all = list(set(scene_name_all))
    for scene_ind in range(len(scene_name_one)):
        for i in range(80):
            new_obj_list = get_obj_list(scene_name_all, scene_name_all_list[scene_ind])
            new_dict = create_dict()
            new_obj_list = ",".join(new_obj_list)
            new_obj_list = "[" + new_obj_list.strip() + "]"
            new_dict["input"] = new_obj_list.strip()
            json_list.append(new_dict)

with open(save_path, 'w') as f:
    json_str = json.dumps(json_list, indent=2)
    f.write(json_str)
    f.write('\n')


import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from collections import defaultdict

gg_data = pd.read_csv('adata_24dec.csv')

gg_data.head()

gg_data.nunique()

gg_data.account_status.value_counts()

gg_data.loc[gg_data['account_status'] == 'active'];

mydict = {}
for index, row in gg_data.iterrows():
    try:
        val = mydict[row['friend_id']]
        val.append(row['user_id'])
        mydict.update({row['friend_id']:val})
    except KeyError:
        val = [row['user_id']]
        mydict.update({row['friend_id']:val})

hist_dict = {}
for key, value in mydict.iteritems():
    try:
        new_key = len(mydict[key])
        count   = hist_dict[new_key]
        count+=1
        hist_dict.update({new_key:count})
    except KeyError:
        new_key = len(mydict[key])
        hist_dict.update({new_key:1})

len(hist_dict)

hist_dict
del hist_dict[1]
del hist_dict[2]

plt.bar(hist_dict.keys(), hist_dict.values(), 0.5, color='g', align='center')
plt.ylabel('Number of users')
plt.xlabel('Number of Active Contacts')
plt.show()


import torch
from torch import einsum
import numpy as np
from transformers import CLIPTokenizer
from einops import rearrange, repeat

from ldm.modules.attention import default, exists

def get_word_inds(text, words, tokenizer):
    split_text = text.split(" ")
    if type(words) is int:
        words = [words]
    elif type(words) is str:
        words = words.split(" ")
    if type(words[0]) is str:
        words = [i for i, word in enumerate(split_text) if word in words]
    out = []
    if len(words) > 0:
        words_encode = [tokenizer.decode([item]).strip("#") for item in tokenizer.encode(text)][1:-1]
        cur_len, ptr = 0, 0
        for i in range(len(words_encode)):
            cur_len += len(words_encode[i])
            if ptr in words:
                out.append(i + 1)
            if cur_len >= len(split_text[ptr]):
                ptr += 1
                cur_len = 0
    return np.array(out)

def cross_attention_masks(mask=None, tokens_in=None, tokens_out=None, w_in=1., w_out=1., max_tokens=77):
    if mask is not None:
        mask = mask[:, 0, :, :]
        b, h, w = mask.shape
        token_mask = torch.zeros((b, max_tokens, h, w)).to(mask)
        if tokens_in is not None:
            for token in tokens_in:
                token_mask[:, token, ...] = mask * w_in
        if tokens_out is not None:
            for token in tokens_out:
                token_mask[:, token, ...] = (1 - mask) * w_out
    else:
        token_mask = None
    return token_mask

def register_masked_cross_attention(model, token_mask=None, stop_masking=0):
    """
    Replaces all cross-attention layers in model with new forward that implements paint-by-word for localized token
    enhancement.
    """
    def get_forward(self):
        def forward(x, context=None, mask=None):
            h = self.heads

            q = self.to_q(x)
            context, t, beta = default(context, (x, 0, 0))
            k = self.to_k(context)
            v = self.to_v(context)

            q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))

            sim = einsum('b i d, b j d -> b i j', q, k) * self.scale

            if exists(mask):
                mask = rearrange(mask, 'b ... -> b (...)')
                max_neg_value = -torch.finfo(sim.dtype).max
                mask = repeat(mask, 'b j -> (b h) () j', h=h)
                sim.masked_fill_(~mask, max_neg_value)

            if exists(token_mask) and t.min().item() >= stop_masking:
                b = token_mask.shape[0]
                latent_size = int(sim.shape[1] ** 0.5)  # using -b*h as indexing makes it work in classifier-free guidance case (batch*2)
                token_mask_r = torch.nn.functional.interpolate(token_mask, size=latent_size)
                token_mask_r = rearrange(token_mask_r, 'b T h w -> b (h w) T')
                token_mask_r = repeat(token_mask_r, 'b ... -> (b h) ...', h=h)
                beta = repeat(beta[-b:], 'b ... -> (b h) 1 1 ...', h=h)
                wA = torch.log(1 + beta) * sim.max() * token_mask_r  # paint by word scheduling weight
                sim[-b*h:] = sim[-b*h:] + wA.type(sim.dtype)  # paint by word (only for cond samples)

            # attention, what we cannot get enough of
            attn = sim.softmax(dim=-1)

            out = einsum('b i j, b j d -> b i d', attn, v)
            out = rearrange(out, '(b h) n d -> b n (h d)', h=h)
            return self.to_out(out)

        return forward

    def register_ca(name_, net_, count):
        if name_ == 'attn2' and net_.__class__.__name__ == 'CrossAttention':
            net_.forward = get_forward(net_)
            return count + 1
        elif hasattr(net_, 'named_children'):
            for name__, net__ in net_.named_children():
                count = register_ca(name__, net__, count)
        return count

    cross_att_count = 0
    for name, net in model.model.named_children():
        cross_att_count += register_ca(name, net, 0)

def masked_cross_attention(model, prompts, words_in, words_out, mask, tokenizer, w_in=0., w_out=0., stop_masking=0):
    if exists(mask) and (exists(words_in) or exists(words_out)):
        token_mask = []
        for i, prompt in enumerate(prompts):
            tokens_in = get_word_inds(prompt, words_in, tokenizer) if exists(words_in) else list()
            tokens_out = get_word_inds(prompt, words_out, tokenizer) if exists(words_out) else list()
            token_mask.append(cross_attention_masks(mask[i:i+1], tokens_in, tokens_out, w_in, w_out))
        token_mask = torch.concat(token_mask, dim=0)
        register_masked_cross_attention(model, token_mask, stop_masking)

if __name__ == "__main__":
    tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
    print(get_word_inds("A photograph of a sofa in a living room", ["living", "room"], tokenizer))


from tkinter import *
import sqlite3

"""
Jak maji tlacitka pracovat:
dHledat:
    - Pokud v DB existuje zaznam se shodujicim se jmenem spolecnosti nebo tel. cisla
    - Vypise jej
dUlozit jako nove:
    - Ulozi vstup jako novy pohovor, pokud je vyplnena alespon spolecnost nebo tlf
dUlozit zmeny:
    - Ulozi zmeny v nactenem pohovoru
dVypis vse:
    - Do pozn8mky vypise Spolecnost, pohovor (dle ulozeni v db)
dVypis dle data:
    - Do pozn8mky vypise Spolecnost, pohovor (dle nejblizsiho data)
dVycisti:
    - Vycisti vstupni pole
dVymaz oznacene:
    - Vymaze oznaceny radek z DB
dVypnout:
    - Vypinaci tlacitko
TODO:
-pokud budes mit volno (ach, kez by :-)) predelej vypis dle data tak, aby to bralo a pocitalo jen s realnym datumem
- predej to na dva fily - back+front end... bude to srani, ale ano, udelej to
"""
"""
BACKEND
"""

def smaz_oznacene():
    id_k_smazani = selected_row
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("DELETE FROM pohovory WHERE id = ?", str(id_k_smazani))
    spojeni.commit()
    spojeni.close()
    vloz_do_t1('Smazan pohovor s id: {}'.format(id_k_smazani), prepis = True)

def get_selected_row(event):
    global selected_row
    index = list1_vypis_polozek.curselection()[0]
    selected_row = list1_vypis_polozek.get(index)[0]#[0]
    vypsat_oznacene_do_poli()

def vycisti_pole():
    for pole in seznam_vstupnich_poli[:-1]:
        pole.delete(0, END)
    seznam_vstupnich_poli[-1].delete(1.0, END)

def spojeni_s_db():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("CREATE TABLE IF NOT EXISTS pohovory(id INTEGER PRIMARY KEY, firma text, pohovor text, osoba1 text, osoba2 text, osoba3 text, mail1 text, mail2 text, mail3 text, telefon1 text, telefon2 text, telefon3 text, poznamka1 text, poznamka2 text, poznamka3 text, poznamky text)")
    spojeni.commit()
    spojeni.close()

def vypsat_oznacene_do_poli():
    id_k_vypsani = selected_row
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("SELECT * FROM pohovory WHERE id = ?", str(id_k_vypsani))
    seznam_k_vypsani = kurzor.fetchall()
    vycisti_pole()
    for i, obj in enumerate(seznam_k_vypsani[0][1:]):
        seznam_vstupnich_poli[i].insert(END, obj)
    spojeni.close()

def vlozeni_noveho_zaznamu():
    if len(e1_firma_value.get()) == len(e9_number1_value.get()) ==  len(e2_pohovor_value.get()) == 0:
        vloz_do_t1('Neni co ukladat... co takhle se více snažit?')
        return
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("INSERT INTO pohovory VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (e1_firma_value.get(), e2_pohovor_value.get(), e3_osoba1_value.get(), e4_osoba2_value.get(), e5_osoba3_value.get(), e6_mail1_value.get(), e7_mail2_value.get(), e8_mail3_value.get(), e9_number1_value.get(), e10_number2_value.get(), e11_number3_value.get(), e12_popisek1_value.get(), e13_popisek2_value.get(), e14_popisek3_value.get(), t1_poznamky.get("1.0", 'end-1c')))
    spojeni.commit()
    spojeni.close()
    vloz_do_t1('Zaznam uspesne pridan :-)', True)

def vloz_do_t1(objekt, prepis = True):
    # Funkce, ktera do Poznamkoveho pole vklada hodnoty. Pokud prepis = True:smaze puvodni vypsany obsah pole
    if prepis:
        list1_vypis_polozek.delete(0, END)
    list1_vypis_polozek.insert(END, objekt)

def nacti_vse_dle_abecedy():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("SELECT id, firma, pohovor FROM pohovory ORDER BY firma ASC")
    vytah_z_db = kurzor.fetchall()
    # vycisti_pole()
    list1_vypis_polozek.delete(0, END)
    for objekt in vytah_z_db:
        vloz_do_t1(objekt, prepis = False)
    spojeni.close()

def nacti_vse_dle_data():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("SELECT id, firma, pohovor FROM pohovory ORDER BY pohovor ASC")
    vytah_z_db = kurzor.fetchall()
    list1_vypis_polozek.delete(0, END)
    for objekt in vytah_z_db:
        vloz_do_t1(objekt, prepis = False)
    spojeni.close()

def hledat():
    if len(e1_firma_value.get()) == len(e9_number1_value.get()) ==  len(e2_pohovor_value.get()) == 0:
        vloz_do_t1('Není dle čeho hledat. Víc se snaž')
        return
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute(
        "SELECT id, firma, pohovor FROM pohovory WHERE telefon1 = ? OR telefon2 = ? OR telefon3 = ?",
        (e9_number1_value.get(), e9_number1_value.get(),
         e9_number1_value.get()))
    vysledek = kurzor.fetchall()[0]
    vloz_do_t1(vysledek, prepis = True)
    spojeni.close()

def update_zapisu():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("UPDATE pohovory SET firma = ?, pohovor = ?, osoba1 = ?, osoba2 = ?, osoba3 = ?, mail1 = ?, mail2 = ?, mail3 = ?, telefon1 = ?, telefon2 = ?, telefon3 = ?, poznamka1 = ?, poznamka2 = ?, poznamka3 = ?, poznamky = ? WHERE firma = ?", (e1_firma_value.get(), e2_pohovor_value.get(), e3_osoba1_value.get(), e4_osoba2_value.get(), e5_osoba3_value.get(), e6_mail1_value.get(), e7_mail2_value.get(), e8_mail3_value.get(), e9_number1_value.get(), e10_number2_value.get(), e11_number3_value.get(), e12_popisek1_value.get(), e13_popisek2_value.get(), e14_popisek3_value.get(), t1_poznamky.get("1.0", 'end-1c'), e1_firma_value.get()))
    spojeni.commit()
    spojeni.close()

def vypnout():
    window.destroy()

spojeni_s_db()
"""
FRONTEND
"""

window = Tk()
window.wm_title('Pohovory v0.2')

l1_firma = Label(window, text = 'Společnost:')
l1_firma.grid(row = 0, column = 0, sticky = W+S)
e1_firma_value = StringVar()
e1_firma = Entry(window, textvariable = e1_firma_value)
e1_firma.grid(row = 1, column = 0, sticky = W+S+N)
e1_firma.config(font=("Times", 12, 'bold'))

l2_pohovor = Label(window, text = '      Pohovor:')
l2_pohovor.grid(row = 1, column = 1, columnspan = 2, sticky = W+S)
e2_pohovor_value = StringVar()
e2_pohovor = Entry(window, textvariable = e2_pohovor_value)
e2_pohovor.grid(row = 1, column = 1, columnspan = 2, sticky = S)

l3_osoba1 = Label(window, text = 'Osoba1:')
l3_osoba1.grid(row = 2, column = 0, sticky = S+W)
e3_osoba1_value = StringVar()
e3_osoba1 = Entry(window, textvariable = e3_osoba1_value)
e3_osoba1.grid(row = 3, column = 0, sticky = W)

l4_osoba2 = Label(window, text = 'Osoba2:')
l4_osoba2.grid(row = 2, column = 1, sticky = W+S)
e4_osoba2_value = StringVar()
e4_osoba2 = Entry(window, textvariable = e4_osoba2_value)
e4_osoba2.grid(row = 3, column = 1, sticky = W)

l5_osoba3 = Label(window, text = 'Osoba3:')
l5_osoba3.grid(row = 2, column = 2, sticky = S+W)
e5_osoba3_value = StringVar()
e5_osoba3 = Entry(window, textvariable = e5_osoba3_value)
e5_osoba3.grid(row = 3, column = 2, sticky = W)

l6_mail1 = Label(window, text = 'E-mail:')
l6_mail1.grid(row = 4, column = 0, sticky = S+W)
e6_mail1_value = StringVar()
e6_mail1 = Entry(window, textvariable = e6_mail1_value)
e6_mail1.grid(row = 5, column = 0, sticky = W)

e7_mail2_value = StringVar()
e7_mail2 = Entry(window, textvariable = e7_mail2_value)
e7_mail2.grid(row = 5, column = 1, sticky = W)

e8_mail3_value = StringVar()
e8_mail3 = Entry(window, textvariable = e8_mail3_value)
e8_mail3.grid(row = 5, column = 2, sticky = W)

l7_telefon1 = Label(window, text = 'Telefonní číslo:')
l7_telefon1.grid(row = 6, column = 0, sticky = S+W)
e9_number1_value = StringVar()
e9_number1 = Entry(window, textvariable = e9_number1_value)
e9_number1.grid(row = 7, column = 0, sticky = W)
e10_number2_value = StringVar()
e10_number2 = Entry(window, textvariable = e10_number2_value)
e10_number2.grid(row = 7, column = 1, sticky = W)
e11_number3_value = StringVar()
e11_number3 = Entry(window, textvariable = e11_number3_value)
e11_number3.grid(row = 7, column = 2, sticky = W)

l8_popisek1 = Label(window, text = 'Pozn.:')
l8_popisek1.grid(row = 8, column = 0, sticky = S+W)
e12_popisek1_value = StringVar()
e12_popisek1 = Entry(window, textvariable = e12_popisek1_value)
e12_popisek1.grid(row = 9, column = 0, sticky = W)
e13_popisek2_value = StringVar()
e13_popisek2 = Entry(window, textvariable = e13_popisek2_value)
e13_popisek2.grid(row = 9, column = 1, sticky = W)
e14_popisek3_value = StringVar()
e14_popisek3 = Entry(window, textvariable = e14_popisek3_value)
e14_popisek3.grid(row = 9, column = 2, sticky = W)

list1_vypis_polozek = Listbox(window, height = 14, width = 44)
list1_vypis_polozek.grid(row = 10, column = 0, rowspan = 14, columnspan = 2, sticky = W+N)

s1 = Scrollbar(window)
s1.grid(row = 18, column = 2, rowspan = 15, sticky = E+N+S)
list1_vypis_polozek.bind('<<ListboxSelect>>', get_selected_row)

t1_poznamky = Text(window, height = 15, width = 70)
t1_poznamky.configure(yscrollcommand = s1.set)
t1_poznamky.grid(row = 18, column = 0, columnspan = 3, sticky = W)
s1.configure(command = t1_poznamky.yview)

seznam_vstupnich_poli = [e1_firma, e2_pohovor, e3_osoba1, e4_osoba2, e5_osoba3, e6_mail1, e7_mail2, e8_mail3, e9_number1, e10_number2, e11_number3, e12_popisek1, e13_popisek2, e14_popisek3, t1_poznamky]

### Tlacitka

b1_find = Button(window, text = 'Hledat')
b1_find.config(height = 0, command = hledat)
b1_find.grid(row = 10, column = 2, sticky = W+E)
b2_save_new = Button(window, text = 'Uložit jako nové', command = vlozeni_noveho_zaznamu)
b2_save_new.grid(row = 11, column = 2, sticky = W+E)
b3_save_changes = Button(window, text = 'Uložit změny', command = update_zapisu)
b3_save_changes.grid(row = 12, column = 2, sticky = W+E)
b4_print_all = Button(window, text = 'Vypiš vše dle abecedy', command = nacti_vse_dle_abecedy)
b4_print_all.grid(row = 13, column = 2, sticky = W+E)
b5_sort_time = Button(window, text = 'Vypiš vše dle data', command = nacti_vse_dle_data)
b5_sort_time.grid(row = 14, column = 2, sticky = W+E)
b6_clear = Button(window, text = 'Vyčisti pole', command = vycisti_pole)
b6_clear.grid(row = 15, column = 2, sticky = W+E)
b7_delete_selected = Button(window, text ='Vymazat označené', command = smaz_oznacene)
b7_delete_selected.grid(row = 16, column = 2, sticky = W + E)
b8_exit = Button(window, text = 'Vypnout', command = vypnout)
b8_exit.grid(row = 17, column = 2, sticky = W+E)

window.mainloop()


import requests
import json
import logging
import datetime
from datetime import datetime, timedelta
import schedule
import time
import datetime
import xml.etree.ElementTree as ET
from System_func.send_telegram_message import Alert

# Создание объекта логгера для ошибок и критических событий
check_error_logger = logging.getLogger('Check_Ticket_Error')
check_error_logger.setLevel(logging.ERROR)
check_error_handler = logging.FileHandler('./logs/check_ticket-errors.log')
check_error_handler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M')
check_error_handler.setFormatter(formatter)
check_error_logger.addHandler(check_error_handler)

# Создание объекта логгера для информационных сообщений
check_info_logger = logging.getLogger('Check_Ticket_Info')
check_info_logger.setLevel(logging.INFO)
check_info_handler = logging.FileHandler('./logs/check_ticket-info.log')
check_info_handler.setLevel(logging.INFO)
check_info_handler.setFormatter(formatter)
check_info_logger.addHandler(check_info_handler)

# Создаем объект класса Alert
alert = Alert()

### Авторизация в HappyFox
# Указываем путь к файлу с данными
CONFIG_FILE = "Main.config"
# открываем файл и загружаем данные
with open(CONFIG_FILE, 'r', encoding='utf-8-sig') as f:
    data_config = json.load(f)
# извлекаем значения API_KEY и API_SECRET
API_ENDPOINT = data_config['HAPPYFOX_SETTINGS']['API_ENDPOINT']
API_KEY = data_config['HAPPYFOX_SETTINGS']['API_KEY']
API_SECRET = data_config['HAPPYFOX_SETTINGS']['API_SECRET']
# сохраняем значения в переменную auth
HEADERS = {'Content-Type': 'application/json'}

def get_time_diff(date_str):
    """Функция подсчёта времени с последнего ответа клиента"""
    current_time = datetime.datetime.now()
    date_time = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours=3)

    # Определяем день недели
    day_of_week = date_time.weekday()

    # Смещаем дату назад на количество дней, равное количеству выходных
    days_to_subtract = 0
    if day_of_week == 5:  # суббота
        days_to_subtract = 1
    elif day_of_week == 6:  # воскресенье
        days_to_subtract = 2

    # Вычисляем время между датами, исключая выходные
    business_days = 0
    elapsed_days = (current_time - date_time).days
    for day in range(1, elapsed_days+1):
        d = date_time + datetime.timedelta(days=day)
        if d.weekday() not in (5, 6):
            business_days += 1
    elapsed_days = business_days - days_to_subtract
    diff = datetime.timedelta(days=elapsed_days, seconds=(current_time - date_time).seconds)

    return diff

def get_assigned_name(assigned_to):
    if isinstance(assigned_to, list):
        for assigned in assigned_to:
            assigned_name = assigned.get('name')
            break
    else:
        if assigned_to is not None:
            assigned_name = assigned_to.get('name')
        else:
            assigned_name = "Нет исполнителя"
    return assigned_name

def get_name_info(contact_info):
    name_info = None
    for contact in contact_info:
        name_info = contact.get('name')
        break
    return name_info

def get_contact_name(contact_groups):
    """Функция для получения имени контактной группы"""
    name_info = None
    for contact in contact_groups:
        name_info = contact.get('name')
        break
    return name_info

def get_alert_chat_id(assigned_name):
    """"Функция получения chat_id для отправки сообщения"""
    # Разбор XML-файла и получение корневого элемента
    tree = ET.parse('data.xml')
    root = tree.getroot()
    # Находим все элементы header_footer внутри элемента user
    header_footer_elements = root.findall('.//user/header_footer')
    # Задаем начальное значение alert_chat_id
    alert_chat_id = None
    # Проходим циклом по всем найденным элементам header_footer
    for header_footer in header_footer_elements:
        # Сравниваем значение элемента name с assignee_name
        if header_footer.find('name').text == assigned_name:
            # Если значения совпадают, сохраняем значение элемента chat_id в alert_chat_id
            alert_chat_id = header_footer.find('chat_id').text
            break  # Выходим из цикла, т.к. нужный элемент уже найден

    # Если alert_chat_id не был найден, выводим ошибку
    if alert_chat_id is None:
        check_error_logger.error("Не удалось найти chat_id для пользователя {assigned_name} %s")
    return alert_chat_id

def process_ticket(ticket_data):
    """Функция по формированию данных из тикета"""
    status = ticket_data.get('status').get('behavior')
    # Узнаём в работе ли ещё тикет
    if status == "pending":
        ticket_id = ticket_data.get('id')
        priority_info = ticket_data.get('priority')
        priority_name = priority_info.get('name')
        user_info = ticket_data.get('user')
        contact_info = user_info.get('contact_groups')
        assigned_to = ticket_data.get('assigned_to')
        # Проверяем, что значение для поля last_staff_reply_at существует
        if ticket_data.get('last_staff_reply_at'):
            reple_client_time = ticket_data.get('last_staff_reply_at')
            time_difference = get_time_diff(reple_client_time)
            # Проверяем, что в тикете нет ответа более 3х дней
            if time_difference >= timedelta(days=3):
                name_info = get_contact_name(contact_info)
                assigned_name = get_assigned_name(assigned_to)
                # print(f"Время последнего ответа клиента в тикете {time_difference}")

                ticket_message = (f'Ожидание ответа клиента более 3-х дней.\nНомер тикета:: {ticket_id}\nПриоритет: {priority_name}\nНазвание клиента: {name_info}\nНазначен: {assigned_name}')

                # Получаем chat_id для отправки сообщения
                alert_chat_id = get_alert_chat_id(assigned_name)

                # Отправляем сообщение в телеграм-бот
                alert.send_telegram_message(alert_chat_id, ticket_message)
                check_info_logger.info('Сотруднику: %s в чат отправлена информация о 3х дневном неотвеченном тикете: %s', assigned_name, ticket_id)

def get_tickets():
    """Функция проверки тикетов, у которых нет ответа"""
    check_info_logger.info('Задача запущена')
    params = {
        #'status': '1,2,3',  # выбираем все статусы, кроме "Закрыт"
        #'status_neq': '4',       # Исключить закрытые статусы
        'category': '1',
        'status': '_pending',
        #'q': 'behavior:pending'
        #'q': 'unresponded:"true"'
        #'q': 'duedate:"today"'
    }
    url = API_ENDPOINT + '/tickets/?size=1&page=1'
    # проверка на доступность сервера, если сервер недоступен, выводит ошибку
    try:
        response = requests.get(url, auth=(API_KEY, API_SECRET), headers=HEADERS, params=params, timeout=10)
        response.raise_for_status()
    except requests.exceptions.Timeout as error_message:
        check_error_logger.error("Timeout error: request timed out: %s", error_message)
    except requests.exceptions.RequestException as error_message:
        check_error_logger.error("Error occurred: %s", error_message)

    data_res = response.json()
    page_info = data_res.get('page_info')
    last_index = page_info.get('last_index')
    if last_index == 0:
        print('No tickets')
    else:
        for page in range(last_index):
            url = API_ENDPOINT + f'/tickets/?size=1&page={page + 1}'
            # проверка на доступность сервера, если сервер недоступен, выводит ошибку
            try:
                response = requests.get(url, auth=(API_KEY, API_SECRET), headers=HEADERS, params=params, timeout=10)
                response.raise_for_status()
            except requests.exceptions.Timeout as error_message:
                check_error_logger.error("Timeout error: request timed out: %s", error_message)
            except requests.exceptions.RequestException as error_message:
                check_error_logger.error("Error occurred: %s", error_message)
            data = response.json()
            for ticket_data in data.get('data'):
                process_ticket(ticket_data)

# Создадим задачу на отправку алертов в чат
schedule.every().day.at("10:25").do(get_tickets)
check_info_logger.info('Задача на отправку алертов по 3х дневным простоям создана')

while True:
    # Получаем текущую дату и время
    now = datetime.datetime.now()
    # Проверяем, является ли текущий день недели будним днём (пн-пт)
    if now.weekday() < 5:
        # Запускаем отложенные задачи
        schedule.run_pending()
    # Ждём 1 секунду перед проверкой снова
    time.sleep(1)

if __name__ == '__main__':
    get_tickets()


import time
from typing import Any
from absl import logging
import pandas as pd

ACTION = 'Action'
CAMPAIGN_STATUS = 'Campaign status'
CUSTOMER_ID = 'Customer ID'
CAMPAIGN = 'Campaign'
CAMPAIGN_TYPE = 'Campaign type'
BID_STRATEGY_TYPE = 'Bid strategy type'
LABEL = 'Label'
UPDATES_APPLIED = 'Updates applied'

_COLS = [
    ACTION,
    CAMPAIGN_STATUS,
    CUSTOMER_ID,
    CAMPAIGN,  # Campaign name
    CAMPAIGN_TYPE,
    BID_STRATEGY_TYPE,
    LABEL,
    UPDATES_APPLIED,  # Updates applied to this DataFrame / Row.
]

_DEFAULT_ACTION = 'Add'
_DEFAULT_STATUS = 'Paused'
_DEFAULT_CUSTOMER_ID = 'Enter customer ID'
_DEFAULT_LABEL = 'Keyword Translator'

class Campaigns:
  """A class to represent data for a customer's Google Ads campaigns.

  The columns should match the columns specified in a Google Ads Editor "Create
  New Campaign" template.
  See:
  https://support.google.com/google-ads/answer/10702525?hl=en#zippy=%2Ccampaigns

  Example usage:

  # Initializes campaigns DataFrame from Google Ads API response.
  get_campaign_data_response = google_ads_api_client.get_campaign_data(...)
  campaigns = campaigns_lib.Campaigns(get_campaigns_response)

  # Iterates around the data using df() method.
  for index, row in campaigns.df().iterrows():
    print(row)

  # Passes into GoogleAdsObjects class for transformation.
  google_ads_objects = google_ads_objects_lib.GoogleAdsObjects(campaigns...)

  translator_response = keyword_translator(google_ads_objects)

  campaigns = translator_response.get_campaigns()

  # Records an update to the campaign data. For example, record that translation
  # was applied to 'campaign 1'.
  campaigns.add_update(update='translated', campaigns='campaign 1')

  # Writes campaign data to CSV.
  campaigns.to_csv()
  """

  def __init__(self, response_jsons: list[list[Any]]) -> None:
    """Initializes the Campaigns object.

    Creates a DataFrame containing Campaign data from a Google Ads API
    searchStream response.

    See class docstring for more details.

    Args:
      response_jsons: A list of JSON responses from a Google Ads API
        searchStream request. The requests should contain the following fields:
        campaign.name
        campaign.advertising_channel_type
        cmapaign.bidding_strategy_type
    """
    self._df = self._build_campaign_df(response_jsons=response_jsons)
    logging.info('Initialized Campaigns DataFrame with length %d.', self.size())
    self._campaigns_list = self._build_campaigns_list(
        response_jsons=response_jsons
    )

  def _build_campaign_df(self, response_jsons: list[Any]) -> pd.DataFrame:
    """Builds a DataFrame containing Campaign data from Google Ads API JSON.

    Args:
      response_jsons: A list of JSON responses from a Google Ads API
        searchStream request.

    Returns:
      A DataFrame containing Campaign data.
    """
    campaigns = []
    for response_json in response_jsons:
      for batch in response_json:
        for result in batch['results']:
          campaigns.append(
              {
                  ACTION: _DEFAULT_ACTION,
                  CAMPAIGN_STATUS: _DEFAULT_STATUS,
                  CUSTOMER_ID: _DEFAULT_CUSTOMER_ID,
                  CAMPAIGN: result['campaign']['name'],
                  CAMPAIGN_TYPE: result['campaign']['advertisingChannelType'],
                  BID_STRATEGY_TYPE: result['campaign']['biddingStrategyType'],
                  LABEL: _DEFAULT_LABEL,
                  UPDATES_APPLIED: [],
              })

    return pd.DataFrame(campaigns, columns=_COLS)

  def _build_campaigns_list(
      self, response_jsons: list[Any]
  ) -> list[dict[str, str]]:
    """Builds a list of Campaign data from Google Ads API JSON.

    Args:
      response_jsons: A list of JSON responses from a Google Ads API
        searchStream request.

    Returns:
      A list of dicts with campaign id and name.
    """
    campaigns = []
    for response_json in response_jsons:
      for batch in response_json:
        for result in batch['results']:
          campaigns.append({
              'id': result['campaign']['id'],
              'name': result['campaign']['name'],
          })

    return campaigns

  def df(self) -> pd.DataFrame:
    """Returns the DataFrame containing Campaign data."""
    return self._df

  def campaigns_list(self) -> list[dict[str, str]]:
    return self._campaigns_list

  def csv_data(self) -> str:
    """Returns the DataFrame as CSV."""
    return self._df.to_csv(index=False) or str(_COLS)

  def csv_file_name(self) -> str:
    """Returns the CSV file name."""
    time_str = time.strftime('%Y%m%d-%H%M%S')
    return f'campaigns_{time_str}.csv'

  def add_update(self, update: str) -> None:
    """Marks the row as having been updated by a processor.

    Args:
      update: The name of the update to add. E.g. "translated".
    """
    for index in range(self.size()):
      self._df.loc[index, UPDATES_APPLIED].append(update)
    logging.info('Applied update to Campaign DataFrame: %s.', update)

  def columns(self) -> list[str]:
    """Returns the Campaign columns."""
    return _COLS

  def size(self) -> int:
    """Returns the number of Campaign rows."""
    return len(self._df)

  def campaign_names(self) -> list[str]:
    """Returns a list of unique Campaign names."""
    return list(self._df[CAMPAIGN].unique())


import streamlit as st
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from skimage.metrics import peak_signal_noise_ratio, mean_squared_error, structural_similarity
import warnings
from tensorflow import keras
warnings.filterwarnings("ignore")

gen = keras.models.load_model('generator_100')

def inference_gan(gen):
    st.markdown(f"
DISTILLED LEARNING GAN
", unsafe_allow_html=True)
    x = st.slider("Adjust x", min_value=0, max_value=100, value=5)
    low_res_image_path = f"Data/LR/{x}.png"
    high_res_image_path = f"Data/HR/{x}.png"

    lr_img = Image.open(low_res_image_path)
    lr_img = np.array(lr_img.resize((96, 96))) / 255.0
    lr_img = np.expand_dims(lr_img, axis=0)
    hr_img = Image.open(high_res_image_path)
    hr_img = np.array(hr_img.resize((384, 384))) / 255.0
    gen_img = gen.predict(lr_img)
    gen_img = np.squeeze(gen_img, axis=0)
    gen_img = np.clip(gen_img, 0, 1)
    col1, col2 = st.columns(2)
    with col1:
        st.image(lr_img[0], caption="Low-Resolution (LR)", use_column_width=True)
    with col2:
        st.image(gen_img, caption="Generated High-Resolution (HR)", use_column_width=True)
    psnr = peak_signal_noise_ratio(hr_img, gen_img)
    mse = mean_squared_error(hr_img, gen_img)
    # ssim = structural_similarity(hr_img, gen_img, multichannel=True)
    st.markdown(f"
PSNR: {psnr}
", unsafe_allow_html=True)
    st.markdown(f"
MSE: {mse}
", unsafe_allow_html=True)
    # st.markdown(f"
SSIM: {ssim}
", unsafe_allow_html=True)

inference_gan(gen)