import pytest
import asyncio
import time

from mock import Mock

from yarp import NoValue, Value, delay

class TestDelayPersistent(object):

    @pytest.fixture
    def v(self):
        """Value, pre-delay."""
        return Value(123)

    @pytest.fixture
    def dt(self):
        """Delay duration, seconds."""
        return Value(0.1)

    @pytest.fixture
    def log(self):
        """An array of (time, value) tuples for each callback from 'delay'."""
        return []

    @pytest.fixture
    def sem(self, event_loop):
        """A semaphore released whenever the callback is called."""
        return asyncio.Semaphore(0, loop=event_loop)

    @pytest.fixture
    def dv(self, v, dt, log, sem, event_loop):
        """Delayed value"""
        dv = delay(v, dt, loop=event_loop)

        def on_change(value):
            log.append((event_loop.time(), value))
            sem.release()
        dv.on_value_changed(on_change)

        return dv

    @pytest.mark.asyncio
    async def test_initial_value(self, dv):
        # Initial value should pass through immediately
        assert dv.value == 123

    @pytest.mark.asyncio
    async def test_single_change(self, v, dv, sem, log, event_loop):
        # Trigger a change for later...
        before = event_loop.time()
        v.value = 321
        assert dv.value == 123
        assert len(log) == 0
        await sem.acquire()
        assert len(log) == 1
        assert log[-1][0] - before >= 0.1
        assert log[-1][1] == 321
        assert dv.value == 321

    @pytest.mark.asyncio
    async def test_rapid_changes(self, v, dv, sem, log, event_loop):
        # Trigger a sequence of rapid changes
        before = event_loop.time()
        v.value = 1234
        v.value = 12345
        v.value = 123456
        assert dv.value == 123
        assert len(log) == 0
        await sem.acquire()
        await sem.acquire()
        await sem.acquire()
        assert len(log) == 3
        assert log[-3][0] - before >= 0.1
        assert log[-2][0] - before >= 0.1
        assert log[-1][0] - before >= 0.1
        assert log[-3][1] == 1234
        assert log[-2][1] == 12345
        assert log[-1][1] == 123456
        assert dv.value == 123456

    @pytest.mark.asyncio
    async def test_delay_increase(self, v, dv, dt, sem, log, event_loop):
        before = event_loop.time()
        v.value = 321

        # Changing the delay after a value change has occurred should push that
        # change further into the past, but only relative to its original start
        # time
        await asyncio.sleep(0.05, loop=event_loop)
        dt.value = 0.2

        await sem.acquire()
        assert len(log) == 1
        assert log[-1][0] - before >= 0.2
        assert dv.value == 321

    @pytest.mark.asyncio
    async def test_delay_decrease(self, v, dv, dt, sem, log, event_loop):
        before = event_loop.time()
        v.value = 321

        # Changing the delay after a value change has occurred should push that
        # delay closer
        await asyncio.sleep(0.025, loop=event_loop)
        dt.value = 0.05

        await sem.acquire()
        assert len(log) == 1
        assert 0.05 <= log[-1][0] - before < 0.10
        assert dv.value == 321

    @pytest.mark.asyncio
    async def test_delay_decrease_lots(self, v, dv, dt, sem, log, event_loop):
        before = event_loop.time()
        v.value = 321

        # Changing the delay such that a still-delayed value should have been
        # output already should cause that value to be output immediately
        await asyncio.sleep(0.05, loop=event_loop)
        dt.value = 0.01

        assert len(log) == 1
        assert log[-1][0] - before >= 0.05
        assert log[-1][1] == 321
        assert dv.value == 321

class TestDelayInstantaneous(object):

    @pytest.mark.asyncio
    async def test_instantaneous(self, event_loop):
        value = Value()

        delayed_value = delay(value, 0.1, loop=event_loop)
        assert delayed_value.value is NoValue

        # Monitor changes
        evt = asyncio.Event(loop=event_loop)
        m = Mock(side_effect=lambda *_: evt.set())
        delayed_value.on_value_changed(m)

        # Trigger a change for later...
        before = time.time()
        value.set_instantaneous_value(123)
        assert delayed_value.value is NoValue
        assert not m.mock_calls
        await evt.wait()
        assert time.time() - before >= 0.1
        m.assert_called_once_with(123)
        assert delayed_value.value is NoValue


import requests
import bs4

url  = 'https://www.aporeto.com/job-listings/'
html = requests.get(url)

bs = bs4.BeautifulSoup(html.text,"html.parser")

jobs = bs.find_all('div',{'class':'job-listing__text'})
# print(jobs)
open('aporeto-jobs.txt','w').close()
file = open('aporeto-jobs.txt','a+')
for job in jobs:
    title = job.find('h2').get_text()
    location = job.find('h4').get_text()
    description = job.find('p').getText()

    file.write("Title:"+ title +"\n")
    file.write("Location:"+ location +"\n")
    file.write("Description:"+ description +"\n")
    file.write("\n\n")
file.close()


# https://www.acmicpc.net/problem/11724
import sys
from collections import deque, defaultdict

input = sys.stdin.readline

def bfs(x):
    q = deque()
    q.append(x)
    while q:
        vx = q.popleft()
        for nx in graph[vx]:
            if nx not in visited:
                visited.add(nx)
                q.append(nx)

if __name__ == "__main__":
    global graph
    graph = defaultdict(set)
    N, M = map(int, input().split())
    for _ in range(M):
        u, v = map(int, input().split())
        graph[u].add(v)
        graph[v].add(u)
    global visited
    visited = set()
    ans = 0
    for i in range(1,N+1):
        if i not in visited:
            visited.add(i)
            bfs(i)
            ans += 1
    print(ans)


import json
import nbformat
import pathlib
import tempfile

from nbconvert import RSTExporter

def convert_rst(nb_path, tags_to_ignore=["hide"]):
    """
    Convert a notebook to rst
    """
    contents = nb_path.read_text()
    nb = json.loads(contents)

    error_cols = ['\x1b[0;31m', '\x1b[0m', '\x1b[0;32m', '\x1b[0;34m', '\x1b[0;36m']

    body = ''
    for cell in nb["cells"]:
        if cell['cell_type'] != 'code':
            for line in cell['source']:
                body += line.replace('$', '$$')
        if cell['cell_type'] == 'code':
            if "tags" not in cell["metadata"] or 'hide' not in cell["metadata"]["tags"]:
                body += "{% highlight python %}\n"
                for line in cell['source']:
                    if line[:4] == '    ':
                        body += '... '
                        body += line
                    elif line[0] in ['}', ')', ']']:
                        body += '... '
                        body += line
                    elif line == '\n':
                        body += '\n'
                    else:
                        body += '>>> '
                        body += line
                body += '\n'
                if len(cell['outputs']) == 1:
                    if 'data' in cell['outputs'][0]:
                        for l in cell['outputs'][0]['data']['text/plain']:
                            body += l
                    elif 'text' in cell['outputs'][0]:
                        for l in cell['outputs'][0]['text']:
                            body += l
                    else:
                        for l in cell['outputs'][0]['traceback']:
                            for col in error_cols:
                                l = l.replace(col, '')
                            body += l
                            body += '\n'
                    body += '\n'
                body += "{% endhighlight %}"
        body += '\n\n'

    with open('chapters/' + nb_path.name.split('.')[0] + '.md', 'w') as f:
        f.write(body)

    cells = []
    for cell in nb["cells"]:
        if "tags" not in cell["metadata"] or all(
            tag not in cell["metadata"]["tags"] for tag in tags_to_ignore
        ):
            if cell["cell_type"] == "code":
                cell['outputs'] = []
            cells.append(cell)
    nb["cells"] = cells

    with (nb_path.parent / 'student' / nb_path.name).open(mode='w') as f:
        f.write(json.dumps(nb))

if __name__ == "__main__":
    nb_dir = pathlib.Path("chapters/nbs")
    worksheet_paths = sorted(nb_dir.glob("*ipynb"))
    for worksheet in worksheet_paths:
        print(worksheet)
        convert_rst(worksheet)


# -*- coding: utf-8 -*-
"""
Created on 2020/11/16 9:48 下午
@Author  : Donghang He
@FileName: method.py
@Software: PyCharm
"""
import os
import pandas as pd
import numpy as np
import traceback

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn import svm, tree
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis as QDA

sc = StandardScaler()
le = LabelEncoder()

knn_classifier = KNeighborsClassifier(n_neighbors=7)
logistic_classifier = LogisticRegression()
nb_classifier = GaussianNB()
clf = tree.DecisionTreeClassifier(criterion='entropy')
model = RandomForestClassifier(n_estimators=9, max_depth=5, random_state=1, criterion='entropy')
lda_classifier = LDA()
qda_classifier = QDA()
svm_classifier = svm.SVC(kernel='linear')

ticker = 'nba'
input_dir = r'datasets'
input_file = os.path.join(input_dir, ticker + '.csv')
output_dir = r'plots'

score_col = ['FG', 'FGA', 'FG%', '3P', '3PA', '3P%', '2P', '2PA', '2P%', 'eFG%', 'FT', 'FTA', 'FT%', 'PTS']
other_col = ['G', 'MP', 'ORB', 'DRB', 'TRB', 'AST', 'STL', 'BLK', 'TOV', 'PF']
pred_score = ['FG%', '3P%', '2P%', 'eFG%', 'FT%']
pred_other = ['G', 'ORB', 'TRB', 'AST', 'BLK']

x_train = [0, 0]
x_train_sc = [0, 0]
y_train = [0, 0]

x_test = [0, 0]
x_test_sc = [0, 0]
y_test = [0, 0]

title_list = ['score', 'other']
method = ['kNN', 'Logistic', 'Naive', 'Decision Tree', 'Random Forest',
          'LDA', 'QDA', 'SVM']

try:
    df_new = pd.read_csv('datasets/NBA_label.csv')
    df_score = df_new[score_col]
    df_other = df_new[other_col]
    df_corr = [df_score, df_other]

    x1 = df_new[pred_score].values
    y = df_new['Label'].values
    x_train[0], x_test[0], y_train[0], y_test[0] = train_test_split(x1, y, test_size=0.5, random_state=0)

    x2 = df_new[pred_other].values
    x_train[1], x_test[1], y_train[1], y_test[1] = train_test_split(x2, y, test_size=0.5, random_state=0)

    x_train_sc[0] = sc.fit_transform(x_train[0])
    x_train_sc[1] = sc.fit_transform(x_train[1])
    x_test_sc[0] = sc.fit_transform(x_test[0])
    x_test_sc[1] = sc.fit_transform(x_test[1])

except Exception as e:
    print(e)
    print(traceback.format_exc())

def acc(predict, name, i):
    accuracy = np.mean(predict == y_test[i])
    print("Accuracy of", name, "for", title_list[i], "is", round(float(accuracy), 3))
    return round(float(accuracy), 3)

# use to get the label of age, bigger than 25 is 1 other is 0
# def generateLabel():
#     df = pd.read_csv(input_file, encoding='gbk')
#     age = df['Age'].tolist()
#     label = []
#     for i in range(len(age)):
#         if age[i] > 25:
#             label.append(1)
#         else:
#             label.append(0)
#     df['Label'] = label
#     df.to_csv('datasets/NBA_label.csv', index=False)
#
#
# def main():
#     # generateLabel()

# main()


import operator
import os
import re
import string
from collections import defaultdict
from sys import argv

sentence_index = 1

class Token:
    def __init__(self, t, c, p, h, nam, o, v):
        self.type = t  #word or char
        self.c5 = c    #c5 tag
        self.pos = p   #pos = part of speech
        self.hw = h    #head word
        self.name = nam   #usually empy in our corpus
        self.occurs = o   #numbers of occurs to each word
        self.value = v  #value of token
        return

    def __hash__(self):
        return hash(set(self.type, self.c5, self.pos, self.hw, self.name, self.occurs, self.value, self.level))

class Sentence:
    def __init__(self, tokens, t, n):
        self.tokens = tokens  #arr of tokens
        self.title = t
        self.n = 1
        return

def text_clean(text):
    """
    Cleans the text from some characters.
    Remove [#] notations on words if exsits.
    Reduce double or more '=' signs to one.
    Remove newlines.
    Remove tabs.
    Remove double spaces.
    Remove special space char.
    :param text: Text.
    :return: Clean text.
    """
    text = re.sub(b'=+', b'=', text)
    text = re.sub(b"\[\d+\]", b"", text)
    text = re.sub(b"\n", b" ", text)
    text = re.sub(b"\t+", b" ", text)
    text = re.sub(b" +", b" ", text)
    text = re.sub("\u200b".encode(), b"", text)
    return text.decode()

def split_to_sentences(text):
    """
    Split text to arrays of sentences.
    Sentences difne by '.' '?' '!'
    :param text: Text.
    :return: list of sentences.
    """
    sentences_list = []
    sentence_list = []
    parentheses = defaultdict(int)
    parentheses["count"] = 0
    flag = False
    for i, c in enumerate(text):
        if c == '(':
            parentheses[c] += 1
        elif c == ')':
            parentheses['('] -= 1
        elif c == '"':
            if parentheses[c] > 0:
                parentheses[c] -= 1
            else:
                parentheses[c] += 1
        if is_end_of_sentence(i, c, text, parentheses):
            sentence_list.append(c)
            sentences_list.append("".join(sentence_list).strip())
            sentence_list.clear()
            parentheses.clear()
            flag = True
        elif flag:
            flag = False
            if c == " ":
                continue
            sentence_list.append(c)
        else:
            sentence_list.append(c)
    if sentence_list:
        sentences_list.append("".join(sentence_list).strip())
    return sentences_list

def arranging_titles(sentences):
    """
    arranging title to "= title =" as a sentence
    :param sentences: list of sentences.
    :return: list of sentences.
    """
    pattern = r'\=(.*?)\='
    new_sentences = []
    for sentence in sentences:
        while sentence.startswith('='):
            str = re.search(pattern, sentence)
            sentence = re.sub(pattern, '', sentence, 1).lstrip()
            try:
                new_sentences.append(str.group(0))
            except:
                break
        new_sentences.append(sentence.lstrip())
    return new_sentences

def is_numbers(before, after):
    # Check if chars are numbers
    return str.isdigit(before) and str.isdigit(after)

def is_letters(before, after):
    # Check if chars are letters.
    return str.isalpha(before) and str.isalpha(after)

def is_numbers_or_letters(before, after):
    # Check if we have mix between number and letter.
    if before.isalnum() and after.isalnum():
        return True
    else:
        return False

def is_shortcut(index, char, text):
    """
    Check if given char is shortcut word.
    :param char: char.
    :param text: text.
    :param index: index of given char in text.
    :return: True or False.
    """
    if (len(text)) <= (index + 2):
        return False
    after_char = text[index + 1]
    before_char = text[index - 1]
    two_index_after_char = text[index + 2]
    if (char == "."):
        # regular sentence
        if before_char.isalpha() and (after_char == " ") and two_index_after_char.isalpha():
            if two_index_after_char.islower():
                return True
        # case "something somewhere 1.6.2 and even more..
        if before_char.isdigit() and (after_char == " ") and two_index_after_char.isalpha():
            if two_index_after_char.islower():
                return True
        # e.g. shortcut.
        if before_char == 'g' and text[index - 2] == '.' and text[index - 3] == 'e':
            return True
        # shortcut to vs.
        if before_char == 'v' and text[index - 2] == ' ':
            return True
        # shortcut to for Mr.
        if before_char == 'r' and text[index - 2] == 'M':
            return True
        # shortcut to Dr.
        if before_char == 'r' and text[index - 2] == 'D':
            return True
        # i.e. shortcut.
        if before_char == 'e' and text[index - 2] == '.' and text[index - 3] == 'i':
            return True
        # shortcut to _p.
        if before_char == 'p' and text[index - 2] == ' ':
            return True
        # name shortcut
        if (char == ".") and before_char.isupper() and (after_char == " ") and two_index_after_char.isalpha():
            if two_index_after_char.isupper():
                return True
    return False

def clean_whitespace(sentences):
    # lstrip() = Remove spaces to the left of the string.
    temp_sentences = []
    for sentence in sentences:
        if not (
                sentence == '.' or sentence == '"' or sentence == ')' or sentence == '".' or sentence == "..." or sentence == ').'):
            if sentence.startswith(",") or sentence.startswith(')') or sentence.startswith('),'):
                sentence = sentence[1:].lstrip()
            temp_sentences.append(sentence.lstrip())
    return temp_sentences

def is_end_of_sentence(index, char, text, helper):
    """
    Check if given char is an end of a sentence.
    End of sentence == ?/./!/..+
    :param index: the index of the char in the text string.
    :param char: char in sentence.
    :param text: text.
    :return: True or False
    """
    if (len(text)) <= (index + 2):
        return False
    before_char = text[index - 1]
    after_char = text[index + 1]
    if char == '.':
        if is_numbers(before_char, after_char) or is_letters(before_char, after_char) or is_numbers_or_letters(
                before_char, after_char):
            return False
        elif is_shortcut(index, char, text):
            return False
        elif after_char == '.':  # handel case "etc..."
            return False
        elif after_char == '-':
            return False
        else:
            return True
    if char == '"' or char == ')':
        if before_char == '.' or before_char == '?' or before_char == '!':  # or before_char == ';':
            return True
    return (char == '?') or (char == '.') or (char == '!')  # or(char == ';')

def tokenize_words(sentences):
    """
    tokenize the words in the sentences.
    :param sentences:
    :return: list of sentences that includes tokens.
    """
    many_tokens = []
    for sentence in sentences:
        sentence = tokenize_special_characters(sentence)
        tokens = []
        if sentence.startswith(" "):
            sentence = sentence[1:]
        words = sentence.split(" ")
        # create tokens
        for word in words:
            if (word in string.punctuation):
                word_or_char = False
            else:
                word_or_char = True
            new_token = Token('word' if word_or_char else 'char', "", "", "", "", "", word + " ")
            tokens.append(new_token)
        many_tokens.append(tokens)
    return many_tokens

def tokenize_special_characters(sentence):
    """
    Insert whitespace before and after: '(' , ')' , ';' , '<' '>'
     '!' , '?' , '+' , '=' , '"' , '%' , '#' , '^' , '{' , '}' , '[' ']' ,
      ',' , '.' , ':' , ';' , '/' , '\'
    :param sentence: sentence.
    :return: sentence
    """
    regex = re.compile(r'(/|<|>|\(|\)|;|=|\{|\[|\]|\+|\^|\%|\}|\#|"|\|)')
    sentence = regex.sub(r' \1 ', sentence)
    regex = re.compile(r'(?<!\d)' + r'(\.|\\|/|:)' + r'(?!\d|\s)')
    sentence = regex.sub(r' \1 ', sentence)
    regex = re.compile(r'(?<!\d|\s)' + r'(\.|:|\\|/)' + r'(?!\d)')
    sentence = regex.sub(r' \1 ', sentence)
    sentence = re.sub(r'[,]+(?![0-9])', r' , ', sentence)
    sentence = re.sub("  +", " ", sentence)
    if ("i . e ." in sentence):
        sentence = re.sub("i . e .", lambda params: params[0].replace(' ', ""), sentence)
    if ("e . g ." in sentence):
        sentence = re.sub("e . g .", lambda params: params[0].replace(' ', ""), sentence)
    return sentence

class Corpus:
    def __init__(self):
        self.sentences = []   #arr of sentences

    def add_xml_file_to_corpus(self, file_name: str):
        """
        This method will receive a file name, such that the file is an XML file (from the BNC), read the content from
        it and add it to the corpus in the manner explained in the exercise instructions.
        :param file_name: The name of the XML file that will be read
        :return: None
        """
        import xml.etree.ElementTree as ET

        tree = ET.parse(file_name)
        root = tree.getroot()
        dict_value = dict()
        # dictionary of word to count ranks and frequencies
        for w in root.iter('w'):
            if (dict_value.get(w.text) == None):
                dict_value[w.text] = 1
            else:
                dict_value[w.text] += 1
        # sort dict by frequencies to aprove zipf's law
        dict_value = sorted(dict_value.items(), key=operator.itemgetter(1))
        #print(dict_value)
        # iterate over sentence of the xml files
        for sentence in tree.iter('s'):
            tokens_arr = []
            for w in sentence:  # iterate over words in sentence.
                if w.tag in ('w', 'c'):  # inesrt word or char as a token
                    token = Token(w.tag, w.attrib.get('c5', ""), w.attrib.get('pos', ""), w.attrib.get('hw', ""), "",
                                  "", w.text)
                    tokens_arr.append(token)
            new_sentence = Sentence(tokens_arr, "", int(sentence.attrib['n']))
            self.sentences.append(new_sentence)
        return

    def add_text_file_to_corpus(self, file_name: str):
        """
        This method will receive a file name, such that the file is an text file (from Wikipedia), read the content
        from it and add it to the corpus in the manner explained in the exercise instructions.
        :param file_name: The name of the text file that will be read
        :return: None
        """
        global sentence_index
        with open(file_name, "rb") as text:
            wiki_text = text_clean(text.read())
            sentences = split_to_sentences(wiki_text)
            sentences = arranging_titles(sentences)
            sentences = clean_whitespace(sentences)
            tokens_arr = tokenize_words(sentences)
            for t in tokens_arr:
                new_sentence = Sentence(t, "", sentence_index)
                sentence_index += 1
                self.sentences.append(new_sentence)
        return

    def create_text_file(self, file_name: str):
        """
        This method will write the content of the corpus in the manner explained in the exercise instructions.
        :param file_name: The name of the file that the text will be written on
        :return: None
        """
        flag = 0
        file_str = ''
        for sentence in self.sentences:
            for token in sentence.tokens:
                if ("=" in token.value):
                    flag += 1
                file_str += token.value
                if (flag == 2):
                    sentence.title = "true"
                    file_str += '\n'
                    flag = 0
            if (flag != 2):  # get '\n' each sentence and each title
                file_str += '\n'
            flag = 0
        output_file = open(file_name, 'w', encoding='utf8')
        output_file.write(file_str)
        return

if __name__ == '__main__':

    xml_dir = argv[1]  # directory containing xml files from the BNC corpus (not a zip file)
    wiki_dir = argv[2]  # directory containing text files from Wikipedia (not a zip file)
    output_file = argv[3]

    corpus = Corpus()
    xml_files = os.listdir(xml_dir)
    for file in xml_files:
        corpus.add_xml_file_to_corpus(os.path.join(xml_dir, file))

    wiki_files_names = os.listdir(wiki_dir)
    for file in wiki_files_names:
        corpus.add_text_file_to_corpus(os.path.join(wiki_dir, file))

    corpus.create_text_file(output_file)
    print("end of the road")


import numpy as np
import cv2
import scipy
from scipy import ndimage

cap = cv2.VideoCapture('/home/sasidhy1/Videos/samples/take1.mp4')
fgbg = cv2.BackgroundSubtractorMOG2()
blur_radius = 5.0
threshold = 50

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    frame = cv2.resize(frame,(640,480))
    fgmask = cv2.resize(fgmask,(640,480))

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    smooth = ndimage.gaussian_filter(gray, blur_radius)

    labeled, nr_objects = ndimage.label(smooth > threshold)
    flip = cv2.flip(smooth,1)

    cv2.putText(img = flip,
            text = 'objects: {}'.format(nr_objects),
            org = (20,20),
            fontFace = cv2.FONT_HERSHEY_DUPLEX,
            fontScale = 0.5,
            color = (0, 255, 0))

    # Display the resulting frame
    cv2.imshow("stacked", frame)
    cv2.imshow("macked", fgmask)
    # cv2.imshow("stacked", np.hstack([frame, fgmask]))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
"""
Created on Tue Jan 17 14:54:24 2017

@author: RJovelin
"""

# use this script to test for enrichement of overlapping genes among disease genes
# save data as table and figure

# import modules
# use Agg backend on server without X server
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib import rc
import matplotlib.gridspec as gridspec
rc('mathtext', default='regular')
import json
import random
import copy
import sys
import os
import math
import numpy as np
from scipy import stats
from HsaNestedGenes import *

# load dictionaries of overlapping genes
JsonFiles = ['HumanOverlappingGenes.json', 'HumanNestedGenes.json',
             'HumanPiggyBackGenes.json', 'HumanConvergentGenes.json',
             'HumanDivergentGenes.json']
# make a list of dictionaries
Overlap = []
# loop over files
for i in range(len(JsonFiles)):
    # load dictionary of overlapping gene pairs
    json_data = open(JsonFiles[i])
    overlapping = json.load(json_data)
    json_data.close()
    Overlap.append(overlapping)

# get GFF file
GFF = 'Homo_sapiens.GRCh38.88.gff3'

# get the coordinates of genes on each chromo
# {chromo: {gene:[chromosome, start, end, sense]}}
GeneChromoCoord = ChromoGenesCoord(GFF)
# map each gene to its mRNA transcripts
MapGeneTranscript = GeneToTranscripts(GFF)
# remove genes that do not have a mRNA transcripts (may have abberant transcripts, NMD processed transcripts, etc)
GeneChromoCoord = FilterOutGenesWithoutValidTranscript(GeneChromoCoord, MapGeneTranscript)
# get the coordinates of each gene {gene:[chromosome, start, end, sense]}
GeneCoord = FromChromoCoordToGeneCoord(GeneChromoCoord)

# generate gene sets
GeneSets = []
for i in range(len(Overlap)):
    GeneSets.append(MakeFullPartialOverlapGeneSet(Overlap[i]))
# make a set of non-overlapping genes
NonOverlappingGenes = MakeNonOverlappingGeneSet(Overlap[0], GeneCoord)

# map ensembl gene IDs to gene names {gene_ID: Name}
GeneIDToNames = MapNametoID('Homo_sapiens.GRCh38.88.gff3')
# reverse dictionary {name: ID}
GeneNamesToID = {}
for ID in GeneIDToNames:
    GeneNamesToID[GeneIDToNames[ID]] = ID

# make a set of complex disease genes
GAD = ParseComplexDisease('GADCDC_data.tsv', GeneNamesToID)
# make a set of GWAS genes
GWAS = ParseGWASDisease('gwas_catalog_v1.0-associations_e87_r2017-01-09.tsv', 'TraitsToRemove.txt', GeneNamesToID)
# make a set of cancer driver genes
Drivers = ParseCosmicFile('Census_allMon_Mar27_2017.tsv', GeneNamesToID)
# mnake a set of mendelean disease genes
OMIM = ParseOMIMDisease('mimTitles.txt', 'morbidmap.txt', GeneNamesToID)

# create a set with all disease genes
DiseaseGenes = GAD.union(GWAS).union(Drivers).union(OMIM)

# create a list of gene sets
AllGenes = [NonOverlappingGenes]
for i in range(1, len(GeneSets)):
    AllGenes.append(GeneSets[i])
# create a parallel list of gene categories
GeneCats = ['Not', 'Nst', 'Pbk', 'Con', 'Div']

# make a list of counts for each disease and gene category[[[disease, non_disease],... ], ....]
GeneCounts = CountDiseaseGenes(AllGenes, [GAD, GWAS, Drivers, OMIM, DiseaseGenes])

# test significance by comparing each overlapping gene class to non-overlapping genes for each disease
Significance = []
for i in range(len(GeneCounts)):
    # access disease type
    PVals = []
    for j in range(1, len(GeneCounts[i])):
        # compare each gene class to non-overlapping genes for that disease class
        P = stats.fisher_exact([GeneCounts[i][0], GeneCounts[i][j]])[1]
        PVals.append(P)
    # convert p values to star significance
    Significance.append(ConvertPToStars(PVals))

# compute proportions of disease and non-disease for each class in each disease class
DisProp, NonDisProp = [], []
for i in range(len(GeneCounts)):
    # access disease type
    disease, nondisease = [], []
    for j in range(len(GeneCounts[i])):
        # access disease and non-disease counts for given gene class in this disease class
        disease.append(GeneCounts[i][j][0] / sum(GeneCounts[i][j]))
        nondisease.append(GeneCounts[i][j][1] / sum(GeneCounts[i][j]))
    DisProp.append(disease)
    NonDisProp.append(nondisease)

# create a function to format the subplots
def CreateAx(Columns, Rows, Position, figure, Data, XLabel, isYLabel):
    '''
    Returns a ax instance in figure
    '''

    # add a plot to figure (N row, N column, plot N)
    ax = figure.add_subplot(Rows, Columns, Position)
    # Create a horizontal bar plot for proportions of disease genes
    ax.bar([0, 0.2, 0.4, 0.6, 0.8], Data[0], width = 0.2, label = 'disease', color= ['#2b8cbe'] * len(Data[0]), edgecolor = 'white', linewidth = 0.7)
    # Create a horizontal bar plot for proportions of non-disease genes
    ax.bar([0, 0.2, 0.4, 0.6, 0.8], Data[1], width = 0.2, bottom = Data[0], label = 'non-disease', color= ['#88419d'] * len(Data[1]), edgecolor = 'white', linewidth = 0.7)

    # set font for all text in figure
    FigFont = {'fontname':'Arial'}
    # write y axis label
    if isYLabel == True:
        YLabel = 'Proportions'
        ax.set_ylabel(YLabel, color = 'black',  size = 6, ha = 'center', **FigFont)
        # edit y axis ticks
        plt.yticks(np.arange(0, 1.25, 0.25))
    # add ticks and lebels
    plt.xticks([0.1, 0.3, 0.5, 0.7, 0.9], ['Not', 'Nst', 'Pbk', 'Con', 'Div'], rotation = 0, size = 6, color = 'black', ha = 'center', **FigFont)
    # add x label
    ax.set_xlabel(XLabel, color = 'black',  size = 7, ha = 'center', **FigFont)
    # add a range for the Y axis
    plt.ylim([0, 1])
    # do not show lines around figure
    ax.spines["top"].set_visible(False)
    ax.spines["bottom"].set_visible(True)
    ax.spines["right"].set_visible(False)
    if isYLabel == True:
        ax.spines["left"].set_visible(True)
    else:
        ax.spines['left'].set_visible(False)
    # offset the spines
    for spine in ax.spines.values():
        spine.set_position(('outward', 3))
    # edit tick parameters
    if isYLabel == True:
        plt.tick_params(axis='both', which='both', bottom='on', top='off',
                        right = 'off', left = 'on', labelbottom='on',
                        colors = 'black', labelsize = 6, direction = 'out')
        # Set the tick labels font name
        for label in ax.get_yticklabels():
            label.set_fontname('Arial')
    else:
        plt.tick_params(axis='both', which='both', bottom='on', top='off',
                        right = 'off', left = 'off', labelbottom='on', labelleft = 'off',
                        colors = 'black', labelsize = 6, direction = 'out')
    # add margins
    plt.margins(0.1)
    return ax

# make a figure with proportion of disease and non-disease genes

# create figure
fig = plt.figure(1, figsize = (6.2, 1.8))
# plot data
ax1 = CreateAx(5, 1, 1, fig, [DisProp[0], NonDisProp[0]], 'complex', True)
ax2 = CreateAx(5, 1, 2, fig, [DisProp[1], NonDisProp[1]], 'GWAS', False)
ax3 = CreateAx(5, 1, 3, fig, [DisProp[2], NonDisProp[2]], 'tumors', False)
ax4 = CreateAx(5, 1, 4, fig, [DisProp[3], NonDisProp[3]], 'mendelian', False)
ax5 = CreateAx(5, 1, 5, fig, [DisProp[4], NonDisProp[4]], 'all', False)

# annotate figure to add significance
# significant comparisons were already determined, add letters to show significance
xpos = [0.3, 0.5, 0.7, 0.9]
for i in range(len(Significance[0])):
    ax1.text(xpos[i], 1.05, Significance[0][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[1])):
    ax2.text(xpos[i], 1.05, Significance[1][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[2])):
    ax3.text(xpos[i], 1.05, Significance[2][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[3])):
    ax4.text(xpos[i], 1.05, Significance[3][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)
for i in range(len(Significance[4])):
    ax5.text(xpos[i], 1.05, Significance[4][i], ha='center', va='center', color = 'grey', fontname = 'Arial', size = 6)

# add legend
D = mpatches.Patch(facecolor = '#2b8cbe', edgecolor = 'white', linewidth = 0.7, label= 'disease')
N = mpatches.Patch(facecolor = '#88419d', edgecolor = 'white', linewidth = 0.7, label= 'non-disease')
ax1.legend(handles = [D, N], loc = (0.05, 1.2), fontsize = 6, frameon = False, ncol = 2)

## add subplot labels
#ax1.text(-0.45, 1.1, 'A', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax2.text(0, 1.1, 'B', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax3.text(0, 1.1, 'C', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax4.text(0, 1.1, 'D', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)
#ax5.text(0, 1.1, 'E', ha='center', va='center', color = 'black', fontname = 'Arial', size = 7)

# make sure subplots do not overlap
plt.tight_layout()

# save figure to file
outputfile = 'ProportionDiseaseGenes'
for extension in ['.pdf', '.eps', '.png']:
    fig.savefig(outputfile + extension, bbox_inches = 'tight')


from matplotlib.image import NonUniformImage
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

data =  np.loadtxt("/home/user/Desktop/lorenz_transient_terminal__test_full5.csv", delimiter = ",")

rho   =  20.00
sigma = 10.00
beta  = 8.0/3.0

dimension = 0

drho = data[dimension:,4]
dsigma = data[dimension:,5]
dbeta = data[dimension:,6]

drho_edges = np.linspace(-rho/10, rho/10, 10)
dsigma_edges = np.linspace(-sigma/10, sigma/10, 10)
dbeta_edges = np.linspace(-beta/10, beta/10, 10)

H, xedges, yedges = np.histogram2d(dsigma, drho, bins=(dsigma_edges, drho_edges))
H = H.T

fig = plt.figure(figsize=(7, 3))

ax = fig.add_subplot(111, title='P(sigma-rho)', aspect='equal', xlim=xedges[[0, -1]], ylim=yedges[[0, -1]])
im = NonUniformImage(ax, interpolation='bilinear', cmap = cm.Greys_r)
xcenters = (xedges[:-1] + xedges[1:]) / 2
ycenters = (yedges[:-1] + yedges[1:]) / 2
im.set_data(xcenters, ycenters, H)
ax.images.append(im)
plt.show()


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

Controls PID loops to control robot wheel motors

Subscribes To:
    /status: the status of the PS4 controller
    /trajectory: the destination to head towards
    /odom/ekf/enk_imu: the filtered odometry info of the robot
Publishes:
    /cmd_vel: motor commands to be interpreted by the motor driver
        coordinate system:
            x: +forward -backward
            y: +right - left
            z: +up -down

"""
import rospy
from object_tracking.msg import trajectory
from geometry_msgs.msg import Twist
from ds4_driver.msg import Status
from nav_msgs.msg import Odometry
from modules.PID import PID
import time

running = False

def process_error(angular_error, linear_error,
        angularPID, angular_deadband, max_angular,
        linearPID, linear_deadband, max_linear,
        dt, d_vel_err):
    '''
    Feeds the errors into PID loops

    Parameters:
    angular_error (float): how far angularly robot is from desired
    linear_error (float): how far linear robot is from desired
    angularPID (PID): pid controller for angular velocity
    angular_deadband (float): max allowable linear before correcting
    max_angular (float): cap on angular command
    linearPID (PID): pid controller for linear velocity
    linear_deadband (float): max allowable linear error before correcting
    max_linear (float): cap on linear command
    dt (float): time since last called
    d_vel_err (float): optional other estimate for velocity (potentially make use of better velocity estimation and feed it here for feedback control)

    Returns:
    vel (geometry_msgs.msg Twist)

    '''
    vel = Twist()
    #checks if angular error outside deadband
    if(abs(-angular_error) > angular_deadband):
        #if so compute the error using the pid and clamping it below max
        vel.angular.z = min(max_angular, max(-max_angular, angularPID.compute(-angular_error,dt)))
    else:
        angularPID.feed_sample(-angular_error, dt)
    #checks if linear error outside deadband
    if(abs(linear_error) > linear_deadband):
        #if so compute the error using the pid and clamping it below max
        vel.linear.x = min(max_linear, max(-max_linear, linearPID.compute(linear_error,dt, d_vel_err)))
    else:
        linearPID.feed_sample(linear_error, dt)
    return vel

def handle_trajectory(trajectory, container):
    '''
    Callback for reading trajectory info

    Parameters:
    trajectory (object_tracking.msg trajectory): current desired relative location of robot
    container (Dictionary): container to store info in

    '''
    container["angular_error"] = trajectory.angular_error
    container["linear_error"] = trajectory.linear_error
#checks controller state
def handle_controller(msg):
    '''
    Callback for reading the PS4 controller
    Changes global of whether or not the program is "running"

    Parameters:
    msg (ds4_driver.msg 'Status'): msg from the controlller
    '''
    global running
    if (msg.button_triangle == 1):
        running = True
    if (msg.button_circle == 1):
        running = False
def handle_odometry(msg, container):
    '''
    Callback for handlng odometry info

    Parameters:
    msg (sensor_msgs.msg Odometry): odemetry msg from the robot
    container (dictionary): container to store data in

    '''
    container["robot_linear_vel"] = msg.twist.twist.linear.x

def run():
    '''
    Main function controlling node
    '''
    global running

    #grabs parameters
    process_rate = rospy.get_param("/process_rate")
    running = rospy.get_param("/start_running")
    angular_p_gain  = rospy.get_param("/angular_p_gain")
    angular_d_gain  = rospy.get_param("/angular_d_gain")
    angular_deadband= rospy.get_param("/angular_deadband")
    max_angular = rospy.get_param("/max_angular")

    linear_p_gain   = rospy.get_param("/linear_p_gain")
    linear_d_gain   = rospy.get_param("/linear_d_gain")
    linear_deadband = rospy.get_param("/linear_deadband")
    const_vel = rospy.get_param("/const_vel")
    feed_forward_k = rospy.get_param("/feed_forward_k")
    max_linear = rospy.get_param("/max_linear")

    #creates pid controllers
    linearPID = PID(linear_deadband, linear_p_gain, 0, linear_d_gain, 0)
    angularPID = PID(angular_deadband, angular_p_gain, 0, angular_d_gain, 0)

    #initializes ros stuff
    rospy.init_node("navigator", anonymous=False)
    rate = rospy.Rate(process_rate)
    pub = rospy.Publisher("/cmd_vel", Twist, queue_size =process_rate)

    prev_time = time.time()
    #container for storing callback info
    container = {
        "angular_error": 0,
        "linear_error": 0,
        "robot_linear_vel":0,

    }
    #initialize subscriber
    rospy.Subscriber("/trajectory", trajectory, handle_trajectory,
                (container)
            )

    rospy.Subscriber("/status", Status, handle_controller)

    rospy.Subscriber("/odom/ekf/enc_imu", Odometry, handle_odometry, container)

    prev_time = time.time()

    while not rospy.is_shutdown():
        curr_time = time.time()
        #initilize d_vel_error to be none
        d_vel_error = None
        #checks if a const vel (exterior vel element is specified)
        if (const_vel != ""):
            #if so d_vel_error is configured to be the combination of the robots velocity and the velocity of the person (converted to mm because that is what the pid expects
            d_vel_error = 1000 * (container['robot_linear_vel'] + const_vel)
        #run all the info through the process error command
        vel = process_error(container['angular_error'], container['linear_error'], angularPID, angular_deadband, max_angular, linearPID, linear_deadband, max_linear, curr_time-prev_time, d_vel_error)
        #optionally apply the feed forward part if const vel exists (you can set feed_forward_k to zero to stop this from having an effect)
        if (const_vel != ""):
            vel.linear.x += feed_forward_k * const_vel

        #if running publish
        if (running):
            pub.publish(vel)
        prev_time = curr_time
        rate.sleep()

if __name__ == "__main__":
    try:
        run()
    except rospy.ROSInterruptException:
        pass


import requests

def test_calculate_bmr():
    url = 'http://127.0.0.1:8000/calculate_bmr'

    # Example input data, replace with appropriate values for testing
    input_data = {
        'weight': 70,
        'height': 180,
        'age': 30,
        'sex': 'male'
    }

    # Send a POST request with JSON data
    response = requests.post(url, json=input_data)

    # Check the response status code
    if response.status_code == 200:
        # Parse the JSON response
        data = response.json()
        print("BMR:", data.get('bmr'))
    else:
        print("Error:", response.json())

if __name__ == "__main__":
    test_calculate_bmr()


from setuptools import setup, find_packages

with open("requirements.txt") as f:
	install_requires = f.read().strip().split("\n")

# get version from __version__ variable in demo/__init__.py
from demo import __version__ as version

setup(
	name="demo",
	version=version,
	description="it is my first demo frappe app",
	author="Sritarun",
	author_email="lakkarajusri17@gmail.com",
	packages=find_packages(),
	zip_safe=False,
	include_package_data=True,
	install_requires=install_requires
)


from django import forms
from supplier.models import Supplier, SupplierStatement

class AddSupplierForm(forms.ModelForm):
    class Meta:
        model = Supplier
        fields = ['name', 'phone', 'CNIC']
        labels = {'name': "Enter Name",
                  'phone': "Enter Mobile", 'CNIC': 'Enter Cnic'}
        # widgets = {'name': forms.TextInput(attrs={'type': "text", 'placeholder': "Supplier Name", 'class': 'form-control name', 'name': "name"}), 'phone': forms.TextInput(
        #     attrs={'type': "text", 'placeholder': "Supplier Phone", 'class': 'form-control phone', 'name': "phone", }), 'CNIC': forms.TextInput(
        #     attrs={'type': "text", 'placeholder': "CNIC", 'class': 'form-control cnic', 'name': "cnic", }), }

class SupplierStatementForm(forms.ModelForm):
    class Meta:
        model = SupplierStatement
        fields = '__all__'


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

"""
自适应阈值
前面看到简单阈值是一种全局性的阈值，只需要规定一个阈值值，整个图像都和这个阈值比较。
而自适应阈值可以看成一种局部性的阈值，通过规定一个区域大小，
比较这个点与区域大小里面像素点的平均值（或者其他特征）的大小关系确定这个像素点是属于黑或者白（如果是二值情况）。
使用的函数为：cv2.adaptiveThreshold（）
该函数需要填6个参数：
    第一个原始图像
    第二个像素值上限
    第三个自适应方法Adaptive Method:
        — cv2.ADAPTIVE_THRESH_MEAN_C ：领域内均值
        — cv2.ADAPTIVE_THRESH_GAUSSIAN_C ：领域内像素点加权和，权重为一个高斯窗口
    第四个值的赋值方法：只有cv2.THRESH_BINARY 和cv2.THRESH_BINARY_INV
    第五个Block size:规定领域大小（一个正方形的领域）
    第六个常数C，阈值等于均值或者加权值减去这个常数（为0相当于阈值 就是求得领域内均值或者加权值）
这种方法理论上得到的效果更好，相当于在动态自适应的调整属于自己像素点的阈值，而不是整幅图像都用一个阈值。
"""

import cv2
import matplotlib.pyplot as plt

# 读取图像
img = cv2.imread('./images/cv_flower2.jpg')

# 灰度转换
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 阈值列表
size_list = range(5, 126, 4)
# 参数列表
param_list = [
    [cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY],
    [cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV],
    [cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY],
    [cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV]
]
# 标题列表
title_list = ['MEAN-BINARY', 'MEAN-INV', 'GAUSSIAN-BINARY', 'GAUSSIAN-INV']

# 多子图绘制
fig, axes = plt.subplots(len(size_list), len(param_list) + 2, sharey=False, sharex=False)
# 设置图片尺寸
fig.set_size_inches(12, len(size_list) * 12 // 7)
# 总标题
fig.suptitle('')
# 设置标题(中文字体)
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

for i in range(len(size_list)):
    for j in range(len(param_list) + 2):
        if j == 0:
            show = img
            title = '原图'
        elif j == 1:
            show = gray
            title = '灰度图'
        else:
            param = param_list[j - 2]
            # 简单阈值
            show = cv2.adaptiveThreshold(gray, 255, param[0], param[1], size_list[i], 0)
            title = "%s-Block:%d" % (title_list[j - 2], size_list[i])

        ax = axes[i][j]
        # 显示图片
        ax.imshow(show, 'gray')
        # 显示标题
        ax.set_title(title)
        # 隐藏坐标轴
        ax.set_xticks([])
        ax.set_yticks([])

# 调整每隔子图之间的距离
plt.tight_layout()
# 保存图片
plt.savefig('./images/cv_img_threshold_adpt_result.jpg')
# 显示绘制后的图片
plt.show()


# compare two topics by first creating a superset of all words in the two topics
# then create a vector for each topic with the word counts, filling in 0 for words that are not in the topic
# then compute the cosine similarity between the two vectors

from typing import List, Tuple, Dict, NamedTuple, Callable, Optional, Set
from collections import defaultdict

import numpy as np
from scipy.spatial.distance import cosine

Representation = List[Tuple[str, float]]

def make_comparable(representations: List[Representation]) -> List[Representation]:
    # clone the representations so we can modify them
    representations = [r[:] for r in representations]

    # create a superset of all words in all the representations
    words = set()
    for representation in representations:
        words.update([w for w, _ in representation])

    for representation in representations:
        for word in words:
            if word not in [word for word, score in representation]:
                representation.append((word, 0))
    for representation in representations:
        representation.sort(key=lambda x: x[0])

    assert all(len(representation) == len(words) for representation in representations)
    return representations

def compare_topics(topic1: Representation, topic2: Representation) -> float:
    topic1, topic2 = make_comparable([topic1, topic2])
    count_vector1 = np.array([c for _, c in topic1])
    count_vector2 = np.array([c for _, c in topic2])
    return float(1 - cosine(count_vector1, count_vector2))

class ScoredMatch(NamedTuple):
    topic_id: int
    similarity: float
Strategy = Callable[[List[Representation]], Representation]

latest_strategy: Strategy = lambda representations: representations[-1]
first_strategy: Strategy = lambda representations: representations[0]
def average_representations(representations: List[Representation]) -> Representation:
    assert len(representations) > 0

    if len(representations) == 1:
        return representations[0]

    representations = make_comparable(representations)

    # average the scores for each word
    words = [word for word, _ in representations[0]]
    scores = np.array([0.0 for _ in words])
    for representation in representations:
        scores += np.array([score for _, score in representation])
    scores = scores / len(representations)

    return list(zip(words, scores))
average_strategy: Strategy = average_representations

class KnownTopics:
    def __init__(self,
                 strategy: Strategy = latest_strategy,
                 threshold: float = 0.5):
        self.topic_dict: Dict[int, List[Representation]] = {}
        self.strategy = strategy
        self.threshold = threshold

    def topics(self) -> List[List[Representation]]:
        return list(self.topic_dict.values())

    # TODO: Currently this can match multiple new topics to the same old topic
    #       Think about whether this is a problem
    def add_topics(self, topics: List[Representation]) -> Tuple[Dict[int, Representation], List[int]]:
        new_overall_topic_dict = self.topic_dict.copy()
        these_topics = {}

        # this can match multiple new topics to the same old topic
        # record when that happens

        new_matches = defaultdict(list)

        for topic in topics:
            best_match = self.find_best_match(topic)
            if best_match is None or best_match.similarity < self.threshold:
                # new global topic
                topic_id = len(new_overall_topic_dict)
                new_overall_topic_dict[topic_id] = [topic]
            else:
                # It's a match!
                topic_id = best_match.topic_id
                new_overall_topic_dict[topic_id].append(topic)
                new_matches[topic_id].append((best_match.similarity, topic))
            these_topics[topic_id] = topic

        # find the topics that were matched to multiple new topics
        group_matches = {id:matches for id, matches in new_matches.items() if len(matches) > 1}
        if len(group_matches) > 0:
            print("!!!!!!MULTI-MATCH WARNING!!!!!!!")
        for id, matches in group_matches.items():
            print(f"Multiple new topics matched to old topic {id}:")
            for match in matches:
                print(f"  {match[0]}: {match[1]}")

        matched_topic_ids = [id for id in these_topics.keys() if id in self.topic_dict.keys()]

        self.topic_dict = new_overall_topic_dict

        return these_topics, matched_topic_ids

    def find_best_match(self,
                        new_topic: Representation,
                        strategy: Optional[Strategy] = None) -> Optional[ScoredMatch]:
        if strategy is None:
            strategy = self.strategy
        best_match: Optional[ScoredMatch] = None
        for topic_id, representations in self.topic_dict.items():
            similarity = compare_topics(strategy(representations), new_topic)
            if best_match is None or similarity > best_match.similarity:
                best_match = ScoredMatch(topic_id, similarity)
        return best_match


import socket, random
import threading
import pickle
import sys
import time
import hashlib
import os
from collections import OrderedDict
# Default values if command line arguments not given
IP = "127.0.0.1"
PORT = 2000
buffer = 4096

MAX_BITS = 10        # 10-bit
MAX_NODES = 2 ** MAX_BITS
# Takes key string, uses SHA-1 hashing and returns a 10-bit (1024) compressed integer
def getHash(key):
    result = hashlib.sha1(key.encode())
    return int(result.hexdigest(), 16) % MAX_NODES

class Node:
    def __init__(self, ip, port):
        self.filenameList = []
        self.ip = ip
        self.port = port
        self.address = (ip, port)
        self.id = getHash(ip + ":" + str(port))
        self.pred = (ip, port)            # Predecessor of this node
        self.predID = self.id
        self.succ = (ip, port)            # Successor to this node
        self.succID = self.id
        self.fingerTable = OrderedDict()        # Dictionary: key = IDs and value = (IP, port) tuple
        # Making sockets
            # Server socket used as listening socket for incoming connections hence threaded
        try:
            self.ServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.ServerSocket.bind((IP, PORT))
            self.ServerSocket.listen()
        except socket.error:
            print("Socket not opened")

    def listenThread(self):
        # Storing the IP and port in address and saving the connection and threading
        while True:
            try:
                connection, address = self.ServerSocket.accept()
                connection.settimeout(120)
                threading.Thread(target=self.connectionThread, args=(connection, address)).start()
            except socket.error:
                pass#print("Error: Connection not accepted. Try again.")

    # Thread for each peer connection
    def connectionThread(self, connection, address):
        rDataList = pickle.loads(connection.recv(buffer))
        # 5 Types of connections
        # type 0: peer connect, type 1: client, type 2: ping, type 3: lookupID, type 4: updateSucc/Pred
        connectionType = rDataList[0]
        if connectionType == 0:
            print("Connection with:", address[0], ":", address[1])
            print("Join network request recevied")
            self.joinNode(connection, address, rDataList)
            self.printMenu()
        elif connectionType == 1:
            print("Connection with:", address[0], ":", address[1])
            print("Upload/Download request recevied")
            self.transferFile(connection, address, rDataList)
            self.printMenu()
        elif connectionType == 2:
            #print("Ping recevied")
            connection.sendall(pickle.dumps(self.pred))
        elif connectionType == 3:
            #print("Lookup request recevied")
            self.lookupID(connection, address, rDataList)
        elif connectionType == 4:
            #print("Predecessor/Successor update request recevied")
            if rDataList[1] == 1:
                self.updateSucc(rDataList)
            else:
                self.updatePred(rDataList)
        elif connectionType == 5:
            # print("Update Finger Table request recevied")
            self.updateFTable()
            connection.sendall(pickle.dumps(self.succ))
        else:
            print("Problem with connection type")
        #connection.close()

    # Deals with join network request by other node
    def joinNode(self, connection, address, rDataList):
        if rDataList:
            peerIPport = rDataList[1]
            peerID = getHash(peerIPport[0] + ":" + str(peerIPport[1]))
            oldPred = self.pred
            # Updating pred
            self.pred = peerIPport
            self.predID = peerID
            # Sending new peer's pred back to it
            sDataList = [oldPred]
            connection.sendall(pickle.dumps(sDataList))
            #Updating F table
            time.sleep(0.1)
            self.updateFTable()
            # Then asking other peers to update their f table as well
            self.updateOtherFTables()

    def transferFile(self, connection, address, rDataList):
        # Choice: 0 = download, 1 = upload
        choice = rDataList[1]
        filename = rDataList[2]
        fileID = getHash(filename)
        # IF client wants to download file
        if choice == 0:
            print("Download request for file:", filename)
            try:
                # First it searches its own directory (fileIDList). If not found, send does not exist
                if filename not in self.filenameList:
                    connection.send("NotFound".encode('utf-8'))
                    print("File not found")
                else:   # If file exists in its directory   # Sending DATA LIST Structure (sDataList):
                    connection.send("Found".encode('utf-8'))
                    self.sendFile(connection, filename)
            except ConnectionResetError as error:
                print(error, "\nClient disconnected\n\n")
        # ELSE IF client wants to upload something to network
        elif choice == 1 or choice == -1:
            print("Receiving file:", filename)
            fileID = getHash(filename)
            print("Uploading file ID:", fileID)
            self.filenameList.append(filename)
            self.receiveFile(connection, filename)
            print("Upload complete")
            # Replicating file to successor as well
            if choice == 1:
                if self.address != self.succ:
                    self.uploadFile(filename, self.succ, False)

    def lookupID(self, connection, address, rDataList):
        keyID = rDataList[1]
        sDataList = []
        # print(self.id, keyID)
        if self.id == keyID:        # Case 0: If keyId at self
            sDataList = [0, self.address]
        elif self.succID == self.id:  # Case 1: If only one node
            sDataList = [0, self.address]
        elif self.id > keyID:       # Case 2: Node id greater than keyId, ask pred
            if self.predID < keyID:   # If pred is higher than key, then self is the node
                sDataList = [0, self.address]
            elif self.predID > self.id:
                sDataList = [0, self.address]
            else:       # Else send the pred back
                sDataList = [1, self.pred]
        else:           # Case 3: node id less than keyId USE fingertable to search
            # IF last node before chord circle completes
            if self.id > self.succID:
                sDataList = [0, self.succ]
            else:
                value = ()
                for key, value in self.fingerTable.items():
                    if key >= keyID:
                        break
                value = self.succ
                sDataList = [1, value]
        connection.sendall(pickle.dumps(sDataList))
        # print(sDataList)

    def updateSucc(self, rDataList):
        newSucc = rDataList[2]
        self.succ = newSucc
        self.succID = getHash(newSucc[0] + ":" + str(newSucc[1]))
        # print("Updated succ to", self.succID)

    def updatePred(self, rDataList):
        newPred = rDataList[2]
        self.pred = newPred
        self.predID = getHash(newPred[0] + ":" + str(newPred[1]))
        # print("Updated pred to", self.predID)

    def start(self):
        # Accepting connections from other threads
        threading.Thread(target=self.listenThread, args=()).start()
        threading.Thread(target=self.pingSucc, args=()).start()
        # In case of connecting to other clients
        while True:
            print("Listening to other clients")
            self.asAClientThread()

    def pingSucc(self):
        while True:
            # Ping every 5 seconds
            time.sleep(2)
            # If only one node, no need to ping
            if self.address == self.succ:
                continue
            try:
                #print("Pinging succ", self.succ)
                pSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                pSocket.connect(self.succ)
                pSocket.sendall(pickle.dumps([2]))  # Send ping request
                recvPred = pickle.loads(pSocket.recv(buffer))
            except:
                print("\nOffline node dedected!\nStabilizing...")
                # Search for the next succ from the F table
                newSuccFound = False
                value = ()
                for key, value in self.fingerTable.items():
                    if value[0] != self.succID:
                        newSuccFound = True
                        break
                if newSuccFound:
                    # print("new succ", value[1])
                    self.succ = value[1]   # Update your succ to new Succ
                    self.succID = getHash(self.succ[0] + ":" + str(self.succ[1]))
                    # Inform new succ to update its pred to me now
                    pSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    pSocket.connect(self.succ)
                    pSocket.sendall(pickle.dumps([4, 0, self.address]))
                    pSocket.close()
                else:       # In case Im only node left
                    self.pred = self.address            # Predecessor of this node
                    self.predID = self.id
                    self.succ = self.address            # Successor to this node
                    self.succID = self.id
                self.updateFTable()
                self.updateOtherFTables()
                self.printMenu()

    # Handles all outgoing connections
    def asAClientThread(self):
        # Printing options
        self.printMenu()
        userChoice = input()
        if userChoice == "1":
            ip = input("Enter IP to connect: ")
            port = input("Enter port: ")
            self.sendJoinRequest(ip, int(port))
        elif userChoice == "2":
            self.leaveNetwork()
        elif userChoice == "3":
            filename = input("Enter filename: ")
            fileID = getHash(filename)
            recvIPport = self.getSuccessor(self.succ, fileID)
            self.uploadFile(filename, recvIPport, True)
        elif userChoice == "4":
            filename = input("Enter filename: ")
            self.downloadFile(filename)
        elif userChoice == "5":
            self.printFTable()
        elif userChoice == "6":
            print("My ID:", self.id, "Predecessor:", self.predID, "Successor:", self.succID)
        # Reprinting Menu
        # self.printMenu()

    def sendJoinRequest(self, ip, port):
        try:
            recvIPPort = self.getSuccessor((ip, port), self.id)
            peerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            peerSocket.connect(recvIPPort)
            sDataList = [0, self.address]

            peerSocket.sendall(pickle.dumps(sDataList))     # Sending self peer address to add to network
            rDataList = pickle.loads(peerSocket.recv(buffer))   # Receiving new pred
            # Updating pred and succ
            # print('before', self.predID, self.succID)
            self.pred = rDataList[0]
            self.predID = getHash(self.pred[0] + ":" + str(self.pred[1]))
            self.succ = recvIPPort
            self.succID = getHash(recvIPPort[0] + ":" + str(recvIPPort[1]))
            # print('after', self.predID, self.succID)
            # Tell pred to update its successor which is now me
            sDataList = [4, 1, self.address]
            pSocket2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            pSocket2.connect(self.pred)
            pSocket2.sendall(pickle.dumps(sDataList))
            pSocket2.close()
            peerSocket.close()
        except socket.error:
            print("Socket error. Recheck IP/Port.")

    def leaveNetwork(self):
        # First inform my succ to update its pred
        pSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        pSocket.connect(self.succ)
        pSocket.sendall(pickle.dumps([4, 0, self.pred]))
        pSocket.close()
        # Then inform my pred to update its succ
        pSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        pSocket.connect(self.pred)
        pSocket.sendall(pickle.dumps([4, 1, self.succ]))
        pSocket.close()
        print("I had files:", self.filenameList)
        # And also replicating its files to succ as a client
        print("Replicating files to other nodes before leaving")
        for filename in self.filenameList:
            pSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            pSocket.connect(self.succ)
            sDataList = [1, 1, filename]
            pSocket.sendall(pickle.dumps(sDataList))
            with open(filename, 'rb') as file:
                # Getting back confirmation
                pSocket.recv(buffer)
                self.sendFile(pSocket, filename)
                pSocket.close()
                print("File replicated")
            pSocket.close()

        self.updateOtherFTables()   # Telling others to update their f tables

        self.pred = (self.ip, self.port)    # Chaning the pointers to default
        self.predID = self.id
        self.succ = (self.ip, self.port)
        self.succID = self.id
        self.fingerTable.clear()
        print(self.address, "has left the network")

    def uploadFile(self, filename, recvIPport, replicate):
        print("Uploading file", filename)
        # If not found send lookup request to get peer to upload file
        sDataList = [1]
        if replicate:
            sDataList.append(1)
        else:
            sDataList.append(-1)
        try:
            # Before doing anything check if you have the file or not
            file = open(filename, 'rb')
            file.close()
            sDataList = sDataList + [filename]
            cSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            cSocket.connect(recvIPport)
            cSocket.sendall(pickle.dumps(sDataList))
            self.sendFile(cSocket, filename)
            cSocket.close()
            print("File uploaded")
        except IOError:
            print("File not in directory")
        except socket.error:
            print("Error in uploading file")

    def downloadFile(self, filename):
        print("Downloading file", filename)
        fileID = getHash(filename)
        # First finding node with the file
        recvIPport = self.getSuccessor(self.succ, fileID)
        sDataList = [1, 0, filename]
        cSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        cSocket.connect(recvIPport)
        cSocket.sendall(pickle.dumps(sDataList))
        # Receiving confirmation if file found or not
        fileData = cSocket.recv(buffer)
        if fileData == b"NotFound":
            print("File not found:", filename)
        else:
            print("Receiving file:", filename)
            self.receiveFile(cSocket, filename)

    def getSuccessor(self, address, keyID):
        rDataList = [1, address]      # Deafult values to run while loop
        recvIPPort = rDataList[1]
        while rDataList[0] == 1:
            peerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                peerSocket.connect(recvIPPort)
                # Send continous lookup requests until required peer ID
                sDataList = [3, keyID]
                peerSocket.sendall(pickle.dumps(sDataList))
                # Do continous lookup until you get your postion (0)
                rDataList = pickle.loads(peerSocket.recv(buffer))
                recvIPPort = rDataList[1]
                peerSocket.close()
            except socket.error:
                print("Connection denied while getting Successor")
        # print(rDataList)
        return recvIPPort

    def updateFTable(self):
        for i in range(MAX_BITS):
            entryId = (self.id + (2 ** i)) % MAX_NODES
            # If only one node in network
            if self.succ == self.address:
                self.fingerTable[entryId] = (self.id, self.address)
                continue
            # If multiple nodes in network, we find succ for each entryID
            recvIPPort = self.getSuccessor(self.succ, entryId)
            recvId = getHash(recvIPPort[0] + ":" + str(recvIPPort[1]))
            self.fingerTable[entryId] = (recvId, recvIPPort)
        # self.printFTable()

    def updateOtherFTables(self):
        here = self.succ
        while True:
            if here == self.address:
                break
            pSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                pSocket.connect(here)  # Connecting to server
                pSocket.sendall(pickle.dumps([5]))
                here = pickle.loads(pSocket.recv(buffer))
                pSocket.close()
                if here == self.succ:
                    break
            except socket.error:
                print("Connection denied")

    def sendFile(self, connection, filename):
        print("Sending file:", filename)
        try:
            # Reading file data size
            with open(filename, 'rb') as file:
                data = file.read()
                print("File size:", len(data))
                fileSize = len(data)
        except:
            print("File not found")
        try:
            with open(filename, 'rb') as file:
                #connection.send(pickle.dumps(fileSize))
                while True:
                    fileData = file.read(buffer)
                    time.sleep(0.001)
                    #print(fileData)
                    if not fileData:
                        break
                    connection.sendall(fileData)
        except:
            pass#print("File not found in directory")
        print("File sent")

    def receiveFile(self, connection, filename):
        # Receiving file in parts
        # If file already in directory
        fileAlready = False
        try:
            with open(filename, 'rb') as file:
                data = file.read()
                size = len(data)
                if size == 0:
                    print("Retransmission request sent")
                    fileAlready = False
                else:
                    print("File already present")
                    fileAlready = True
                return
        except FileNotFoundError:
            pass
        # receiving file size
        #fileSize = pickle.loads(connection.recv(buffer))
        #print("File Size", fileSize)
        if not fileAlready:
            totalData = b''
            recvSize = 0
            try:
                with open(filename, 'wb') as file:
                    while True:
                        fileData = connection.recv(buffer)
                        #print(fileData)
                        recvSize += len(fileData)
                        #print(recvSize)
                        if not fileData:
                            break
                        totalData += fileData
                    file.write(totalData)
            except ConnectionResetError:
                print("Data transfer interupted\nWaiting for system to stabilize")
                print("Trying again in 10 seconds")
                time.sleep(5)
                os.remove(filename)
                time.sleep(5)
                self.downloadFile(filename)
                    # connection.send(pickle.dumps(True))

    def printMenu(self):
        print("\n1. Join Network\n2. Leave Network\n3. Upload File\n4. Download File")
        print("5. Print Finger Table\n6. Print my predecessor and successor")

    def printFTable(self):
        print("Printing F Table")
        for key, value in self.fingerTable.items():
            print("KeyID:", key, "Value", value)

if len(sys.argv) < 3:
    print("Arguments not supplied (Defaults used)")
else:
    IP = sys.argv[1]
    PORT = int(sys.argv[2])

myNode = Node(IP, PORT)
print("My ID is:", myNode.id)
myNode.start()
myNode.ServerSocket.close()


import json
import gevent
from kube_monitor import KubeMonitor
from kube_manager.common.kube_config_db import PodKM

class PodMonitor(KubeMonitor):

    def __init__(self, args=None, logger=None, q=None):
        super(PodMonitor, self).__init__(args, logger, q, PodKM,
            resource_name='pods')
        self.init_monitor()
        self.logger.info("PodMonitor init done.");

    def process_event(self, event):
        pod_data = event['object']
        event_type = event['type']

        if event_type != 'DELETED':
            if pod_data['spec'].get('hostNetwork'):
                return
            if not pod_data['spec'].get('nodeName'):
                return

        if self.db:
            pod_uuid = self.db.get_uuid(event['object'])
            if event_type != 'DELETED':
                # Update Pod DB.
                pod = self.db.locate(pod_uuid)
                pod.update(pod_data)
            else:
                # Remove the entry from Pod DB.
                self.db.delete(pod_uuid)

        pod_name = pod_data['metadata'].get('name')
        namespace = pod_data['metadata'].get('namespace')
        if not pod_name or not namespace:
            return

        print("Put %s %s %s:%s" % (event['type'],
            event['object'].get('kind'),
            event['object']['metadata'].get('namespace'),
            event['object']['metadata'].get('name')))
        self.q.put(event)

    def event_callback(self):
        while True:
            self.process()
            gevent.sleep(0)


from django.urls import path
from . import views

app_name = 'post'

urlpatterns = [
  path('',views.Home,name='home'),
  path('serach',views.Search,name='search'),
  path('detail/<int:id>/',views.PostDetail,name='detail'),
  path('likes/<int:pk>',views.likes,name="likes")

]


from functools import reduce
from typing import Callable, Tuple, Union, Sequence

from attr import attrs, attrib

import torch
from torch import Tensor

@torch.no_grad()
def transform_dataset(f: Callable[[Tensor], Tensor], x: Tensor, batch_size: int = 64):
    """`x` must be of shape channel×sample×height×width×quantum.
  `f` must be an `eps`-like function which takes one argument - input - and produces the output.
  Doesn't propagate gradient."""
    return torch.cat(tuple(f(slice) for slice in torch.split(x, batch_size, dim=1))).unsqueeze(
        0
    )

def implies(x: bool, y: bool) -> bool:
    return not x or y

def xor(*args: Tuple[bool]) -> bool:
    return reduce(lambda x, y: (x and not y) or (not x and y), args, False)

def exactly_one_true(*args: Tuple[bool]) -> bool:
    assert all(isinstance(arg, bool) for arg in args)
    return sum(args) == 1

@attrs(auto_attribs=True, frozen=True)
class ZeroCenteredNormalInitialization:
    std: float

@attrs(auto_attribs=True, frozen=True)
class ZeroCenteredUniformInitialization:
    maximum: float

@attrs(auto_attribs=True, frozen=True)
class FromFileInitialization:
    path: str

OneTensorInitialization = Union[
    ZeroCenteredNormalInitialization, ZeroCenteredUniformInitialization, FromFileInitialization
]

def raise_exception(exception):
    raise exception

def id_assert_shape_matches(tensor: torch.Tensor, shape: Sequence[int]) -> torch.Tensor:
    assert tensor.shape == tuple(shape)
    return tensor


import argparse
import logging
import os
import random
import sys
import time
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from tensorboardX import SummaryWriter
from torch.nn.modules.loss import CrossEntropyLoss
from torch.utils.data import DataLoader
from tqdm import tqdm
from utils import DiceLoss
from torchvision import transforms

def trainer_synapse(args, model, snapshot_path):
    from datasets.dataset_synapse import Synapse_dataset, RandomGenerator
    logging.basicConfig(filename=snapshot_path + "/log.txt", level=logging.INFO,
                        format='[%(asctime)s.%(msecs)03d] %(message)s', datefmt='%H:%M:%S')
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
    logging.info(str(args))
    base_lr = args.base_lr
    num_classes = args.num_classes
    batch_size = args.batch_size * args.n_gpu
    # max_iterations = args.max_iterations
    db_train = Synapse_dataset(base_dir=args.root_path, list_dir=args.list_dir, split="train",
                               transform=transforms.Compose(
                                   [RandomGenerator(output_size=[args.img_size, args.img_size])]))
    print("The length of train set is: {}".format(len(db_train)))

    def worker_init_fn(worker_id):
        random.seed(args.seed + worker_id)

    trainloader = DataLoader(db_train, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True,
                             worker_init_fn=worker_init_fn)
    if args.n_gpu > 1:
        model = nn.DataParallel(model)
    model.train()
    ce_loss = CrossEntropyLoss()
    dice_loss = DiceLoss(num_classes)
    optimizer = optim.SGD(model.parameters(), lr=base_lr, momentum=0.9, weight_decay=0.0001)
    writer = SummaryWriter(snapshot_path + '/log')
    iter_num = 0
    max_epoch = args.max_epochs
    max_iterations = args.max_epochs * len(trainloader)  # max_epoch = max_iterations // len(trainloader) + 1
    logging.info("{} iterations per epoch. {} max iterations ".format(len(trainloader), max_iterations))
    best_performance = 0.0
    iterator = tqdm(range(max_epoch), ncols=70)
    for epoch_num in iterator:
        for i_batch, sampled_batch in enumerate(trainloader):
            image_batch, label_batch = sampled_batch['image'], sampled_batch['label']
            image_batch, label_batch = image_batch.cuda(), label_batch.cuda()
            outputs = model(image_batch)
            loss_ce = ce_loss(outputs, label_batch[:].long())
            loss_dice = dice_loss(outputs, label_batch, softmax=True)
            loss = 0.5 * loss_ce + 0.5 * loss_dice
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            lr_ = base_lr * (1.0 - iter_num / max_iterations) ** 0.9
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr_

            iter_num = iter_num + 1
            writer.add_scalar('info/lr', lr_, iter_num)
            writer.add_scalar('info/total_loss', loss, iter_num)
            writer.add_scalar('info/loss_ce', loss_ce, iter_num)

            logging.info('iteration %d : loss : %f, loss_ce: %f' % (iter_num, loss.item(), loss_ce.item()))

            if iter_num % 20 == 0:
                image = image_batch[1, 0:1, :, :]
                image = (image - image.min()) / (image.max() - image.min())
                writer.add_image('train/Image', image, iter_num)
                outputs = torch.argmax(torch.softmax(outputs, dim=1), dim=1, keepdim=True)
                writer.add_image('train/Prediction', outputs[1, ...] * 50, iter_num)
                labs = label_batch[1, ...].unsqueeze(0) * 50
                writer.add_image('train/GroundTruth', labs, iter_num)

        save_interval = 50  # int(max_epoch/6)
        if epoch_num > int(max_epoch / 2) and (epoch_num + 1) % save_interval == 0:
            save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')
            torch.save(model.state_dict(), save_mode_path)
            logging.info("save model to {}".format(save_mode_path))

        if epoch_num >= max_epoch - 1:
            save_mode_path = os.path.join(snapshot_path, 'epoch_' + str(epoch_num) + '.pth')
            torch.save(model.state_dict(), save_mode_path)
            logging.info("save model to {}".format(save_mode_path))
            iterator.close()
            break

    writer.close()
    return "Training Finished!"


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
    path('accounts/signup/', views.signup, name='signup'),
    path('bars/', views.bars_index, name='bars'),
    path('bars/bars_create', views.BarCreate.as_view(), name='bars_create'),
    path('bars/<int:pk>/update', views.BarUpdate.as_view(), name='bars_update'),
    path('bars/<int:bar_id>/', views.bar_details, name='bar_details'),
    path('bars/<int:bar_id>/games_create', views.GameCreate.as_view(), name='games_create'),
    path('bars/<int:pk>/delete', views.BarDelete.as_view(), name='bars_delete'),
    path('games/', views.games_index, name='games'),
    path('games/<int:game_id>/add_photo/', views.add_photo, name='add_photo'),
    path('games/<int:game_id>', views.game_details, name='game_details'),
    path('games/<int:pk>/delete', views.GameDelete.as_view(), name='games_delete'),
    path('games/<int:pk>/update', views.GameUpdate.as_view(), name='games_update'),
]


import pandas as pd
import tracking_f as tr
import config
from datetime import datetime
import numpy as np

def days_between(d1, d2):
    #d1 = datetime.strptime(d1, "%Y-%m-%d")
    #d2 = datetime.strptime(d2, "%Y-%m-%d")
    return abs((d2 - d1).days)

"""
    initialise dataframe with norm-days
    Stored in Excel file for easy editing!
"""
def initPrio():
            Prio_Data = config.prio_excel_path()
            PrioDf = pd.read_excel(Prio_Data, 'Priorities',
                        userows=range(0,20),
                        usecols=range(0,10),
                        )
            return PrioDf

"""
    get status of UHD
    To do: Check if None needs to be replaced by NaT
"""
def status_calc(df):
            #print pd.isnull(df.DateShipped)

            if not pd.isnull(df.DateShipped):
                        Status = 8
            elif not pd.isnull(df.QS2):
                        Status = 7
            elif not pd.isnull(df.QS1):
                        Status = 6
            elif not pd.isnull(df.DatumRepAuftrag):
                        Status = 5
            elif not pd.isnull(df.DatumRepAngebot):
                        Status = 4
            elif not pd.isnull(df.DatumAngekommen):
                        Status = 3
            elif not pd.isnull(df.DatumRetAngebot):
                        Status = 2
            else:
                        Status = 1

            return Status

""" get date of last completed step """
def calc_LastStep(df, Status, auth):

            ShippingStatus = ''

            if Status == 1:
                        LastStep = df.DatumBeginn
            elif Status == 2:
                        LastStep = df.DatumRetAngebot
            elif Status == 3:
                        LastStep = df.DatumAngekommen
            elif Status == 4:
                        LastStep = df.DatumRepAngebot
            elif Status == 5:
                        LastStep = df.DatumRepAuftrag
            elif Status == 6:
                        LastStep = df.QS1
            elif Status == 7:
                        LastStep = df.QS2
            elif Status == 8:
                        """ Status = 8 -> shipped.
                            find tracking status
                        """
                        #print "TrackingID: ", pd.isnull(df.TrackingID)
                        if not pd.isnull(df.TrackingID):
                                    if tr.get_carrier(df.TrackingID) == 'UPS':
                                                ShippingStatus, LastStep = tr.ups_track(df.TrackingID, auth)
                                    if tr.get_carrier(df.TrackingID) == 'FedEx':
                                                ShippingStatus, LastStep = tr.fedex_track(df.TrackingID, auth)

                        else:
                                    LastStep = df.DateShipped
                                    ShippingStatus = "Not available - No Tracking ID!"
                        #LastStep, ShippingStatus = ""

            return LastStep, ShippingStatus

"""
    This function only calculates a day difference between time passed in each step and the norm-time for each step
    -> Priority function needs to be discussed!
"""
def calc_waiting(PrioDf, Priority, Status, LastStep):

            #print "Status: ", Status
            now = datetime.now()

            #print now

            """ find the row in the Priority dataframe that matches our priority-type """
            Row = PrioDf.loc[PrioDf['Type - Prio'] == Priority]
            print("Priority: ", Priority)

            """ match  norm-days per step with the current step """
            NormDays = Row.iloc[0, Status]

            #print "NormDays: ", NormDays

            dt = NormDays - days_between(LastStep, now)
            #print "dt: ", dt

            """
                I added an overdue-flagm because I thought it might help with sorting.
                What we did before is calculate a percentage of "time-passed", so overdue orders had some not very intuitive high %-number.
                Now we can directly see how many days are left in the step, or how many days we are overdue.
            """
            if dt < 0:
                        overdue = True
            else:
                        overdue = False

            return abs(dt), overdue


from twilio.rest import Client
import dotenv
import os

dotenv_file = dotenv.find_dotenv()
dotenv.load_dotenv(dotenv_file)

class NotificationManager:
    # This class is responsible for sending notifications with the deal flight details.
    def __init__(self):
        self.account_sid = os.getenv("ACCOUNT_SID")
        self.auth_token = os.environ.get("AUTH_TOKEN")
        self.to_phone = os.getenv("TO_PHONE")
        self.from_phone = os.environ.get("FROM_PHONE")

    def send_notification(self, deal_price, dept_city, iata_dep, dest_city, iata_dest, date_7, date_28):
        """This method structures the SMS message to be sent to the user"""
        client = Client(self.account_sid, self.auth_token)
        message = client.messages.create(
            body=f"Low price alert! Only ₦{deal_price} to fly from {dept_city}({iata_dep}) to "
                 f"{dest_city}({iata_dest}), from {date_7} to {date_28}.",
            from_=self.from_phone,
            to=self.to_phone
        )
        print(message.status)


from copy import deepcopy
from functools import cache

import hypothesis
import hypothesis.strategies as st
import pycardano
import pytest
from opshin import build

from src.utils import network
from src.utils.mock import MockChainContext, MockUser
from src.week06 import lecture_dir
from src.week06.lecture import negative_r_timed

@pytest.mark.parametrize(
    ["d", "r", "validates"],
    [
        [50, -42, True],
        [50, 0, True],
        [50, 42, False],
        [5000, -42, False],
        [5000, 0, False],
        [5000, 42, False],
    ],
)
def test_unit(d: int, r: int, validates: bool):
    if validates:
        run(d, r)
    else:
        run_fails(d, r)

@hypothesis.settings(deadline=None)
@hypothesis.given(
    d=st.integers(min_value=1002),
    r=st.integers(),
)
def test_property_before_fails(d: int, r: int):
    """All redeemers fail before deadline."""
    run_fails(d, r)

@hypothesis.settings(deadline=None)
@hypothesis.given(
    d=st.integers(min_value=0, max_value=998),
    r=st.integers(min_value=1),
)
def test_property_positive_after_fails(d: int, r: int):
    """Positive redeemer always fail after deadline."""
    run_fails(d, r)

@hypothesis.settings(deadline=None)
@hypothesis.given(
    d=st.integers(min_value=0, max_value=998),
    r=st.integers(max_value=0),
)
def test_property_negative_after_succeeds(d: int, r: int):
    """Negative redeemers always succeed after deadline."""
    run(d, r)

def run_fails(d, r):
    try:
        run(d, r)
        errored = False
    except (ValueError, AssertionError):
        errored = True
    assert errored

def setup_user(context: MockChainContext):
    user = MockUser(context)
    user.fund(100000000)  # 100 ADA
    return user

@cache
def setup_context():
    context = MockChainContext()
    # enable opshin validator debugging
    context.opshin_scripts[plutus_script] = negative_r_timed.validator
    return context, setup_user(context), setup_user(context)

# build script once outside our test functions
plutus_script = build(lecture_dir.joinpath("negative_r_timed.py"))
script_hash = pycardano.plutus_script_hash(plutus_script)
script_address = pycardano.Address(script_hash, network=network)

def lock(context: MockChainContext, u1: MockUser, deadline_slot: int):
    deadline_posix = context.posix_from_slot(deadline_slot) * 1000
    datum = negative_r_timed.CustomDatum(deadline_posix)

    val = pycardano.Value(coin=5000000)  # 5 ADA
    tx_builder = pycardano.TransactionBuilder(context)
    tx_builder.add_input_address(u1.address)
    tx_builder.add_output(
        pycardano.TransactionOutput(script_address, amount=val, datum=datum)
    )
    tx = tx_builder.build_and_sign([u1.signing_key], change_address=u1.address)
    context.submit_tx(tx)
    return context

def unlock(context: MockChainContext, u2: MockUser, redeemer_data: int):
    utxo = context.utxos(script_address)[0]
    tx_builder = pycardano.TransactionBuilder(context)
    tx_builder.add_input_address(u2.address)
    tx_builder.add_script_input(
        utxo,
        redeemer=pycardano.Redeemer(
            redeemer_data,
        ),
        script=plutus_script,
    )
    tx_builder.validity_start = context.last_block_slot
    tx_builder.ttl = tx_builder.validity_start + 1

    tx = tx_builder.build_and_sign([u2.signing_key], change_address=u2.address)
    context.submit_tx(tx)
    return context

def run(deadline_slot: int, redeemer_data: int):
    # setup context and users
    # needs deepcopy so state starts fresh each run
    context, u1, u2 = deepcopy(setup_context())

    # user 1 locks 2 ADA ("val") in validator
    context = lock(context, u1, deadline_slot)

    # wait for a bit
    context.wait(1000)

    # user 2 takes "val" from validator - fees
    unlock(context, u2, redeemer_data)

if __name__ == "__main__":
    run(1000, -1)


import json
import uuid

from pgadmin.browser.server_groups.servers.databases.schemas.tests import \
    utils as schema_utils
from pgadmin.browser.server_groups.servers.databases.tests import utils as \
    database_utils
from pgadmin.utils.route import BaseTestGenerator
from regression import parent_node_dict
from regression.python_test_utils import test_utils as utils
from . import utils as collation_utils
from unittest.mock import patch

class CollationPutTestCase(BaseTestGenerator):
    """ This class will update added collation under schema node. """
    scenarios = utils.generate_scenarios('update_collation',
                                         collation_utils.test_cases)

    def setUp(self):
        super().setUp()
        self.schema_info = parent_node_dict["schema"][-1]
        self.schema_name = self.schema_info["schema_name"]
        self.db_name = parent_node_dict["database"][-1]["db_name"]
        self.server_id = self.schema_info["server_id"]
        self.db_id = self.schema_info["db_id"]
        self.schema_id = self.schema_info["schema_id"]
        coll_name = "collation_get_%s" % str(uuid.uuid4())[1:8]
        self.collation = collation_utils.create_collation(self.server,
                                                          self.schema_name,
                                                          coll_name,
                                                          self.db_name)

    def update_collation(self):
        """
        This function returns the collation delete response
        :return: collation delete response
        """
        return self.tester.put(self.url + str(utils.SERVER_GROUP) +
                               '/' + str(self.server_id) + '/' +
                               str(self.db_id) + '/' + str(self.schema_id) +
                               '/' +
                               str(self.collation_id),
                               data=json.dumps(self.test_data),
                               follow_redirects=True)

    def runTest(self):
        """ This function will update collation under schema node. """
        # Verify database
        db_con = database_utils.connect_database(self,
                                                 utils.SERVER_GROUP,
                                                 self.server_id,
                                                 self.db_id)
        if not db_con['data']["connected"]:
            raise Exception("Could not connect to database.")
        # Verify schema
        schema_response = schema_utils.verify_schemas(self.server,
                                                      self.db_name,
                                                      self.schema_name)
        if not schema_response:
            raise Exception("Could not find the schema.")
        # Verify collation
        coll_name = self.collation[1]
        collation_response = collation_utils.verify_collation(self.server,
                                                              self.db_name,
                                                              coll_name)
        if not collation_response:
            raise Exception("Could not find the collation.")
        self.collation_id = self.collation[0]
        self.test_data['id'] = self.collation_id

        if self.is_positive_test:
            put_response = self.update_collation()
        else:
            if hasattr(self, "error_updating_collation"):
                with patch(self.mock_data["function_name"],
                           return_value=eval(self.mock_data["return_value"])):
                    put_response = self.update_collation()

            if hasattr(self, "internal_server_error"):
                with patch(self.mock_data["function_name"],
                           return_value=eval(self.mock_data["return_value"])):
                    put_response = self.update_collation()

        actual_response_code = put_response.status_code
        expected_response_code = self.expected_data['status_code']
        self.assertEqual(actual_response_code, expected_response_code)

    def tearDown(self):
        # Disconnect database to delete it
        database_utils.disconnect_database(self, self.server_id, self.db_id)


# simple video recorder, for making a test video of the globe
# used both for the paper and for people without my specific globe.
import cv2
import numpy as np
import random
from video import create_capture
from common import clock, draw_str

cam = create_capture(0)
cv2.namedWindow('camera')

recorder = cv2.VideoWriter(filename="vid.mpg", fps=30, frameSize=(640, 480),
                           fourcc=cv2.cv.FOURCC('F', 'M', 'P', '4'))
while True:
  ret, img = cam.read()
  recorder.write(img)
  cv2.imshow('camera', img)
  if 0xFF & cv2.waitKey(5) == 27:
    break

cam.release()
cv2.destroyAllWindows()