import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np

k_min = 1
k_max = 15

# Hiperparametri
nb_classes = 2
nb_features = 8
class_trans = {'B': 0, 'M': 1, 0: 'B', 1: 'M'}

# Ucitavanje podataka
filename = 'data/Prostate_Cancer.csv'
all_data = np.genfromtxt(filename, dtype='str', delimiter=',', skip_header=1, usecols=range(1, 10))

# Nasumicno mesanje podataka
nb_samples = all_data.shape[0]
indices = np.random.permutation(nb_samples)
all_data = all_data[indices]

# Konvertovanje podataka u brojeve
for row in all_data:
    row[0] = class_trans[row[0]]
all_data = [[0 if x == 'NA' else float(x) for x in row] for row in all_data]

# Deljenje podataka na skup za treniranje i testiranje
train, test = np.split(all_data, [int(len(all_data)*0.8)])

train_y, train_x = zip(*[(x[0], x[1:].tolist()) for x in train])
test_y, test_x = zip(*[(x[0], x[1:].tolist()) for x in test])

train_x = np.array(train_x)
train_y = np.array(train_y)
test_x = np.array(test_x)
test_y = np.array(test_y)

nb_train = len(train)
nb_test = len(test)

ax_k = []
ax_acc = []

for k in range(k_min, k_max+1):
    tf.reset_default_graph()

    # Ulazni podaci
    X = tf.placeholder(shape=(None, nb_features), dtype=tf.float32)
    Y = tf.placeholder(shape=(None), dtype=tf.int32)

    # Upiti
    Q = tf.placeholder(shape=(nb_features), dtype=tf.float32)

    # Racunanje K minimalnih udaljenosti
    dists = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(X, Q)), axis=1))
    _, idxs = tf.nn.top_k(-dists, k)
    classes = tf.gather(Y, idxs)
    dists = tf.gather(dists, idxs)
    w = tf.fill([k], 1/k)

    # Odabir klase na osnovu frekvencije u skupu najblizih objekata
    w_col = tf.reshape(w, (k, 1))
    one_hot = tf.one_hot(classes, nb_classes)
    scores = tf.reduce_sum(w_col * one_hot, axis=0)
    hyp = tf.argmax(scores)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        # Trening
        matches = 0
        for i in range(nb_test):
            feed = {X: train_x, Y: train_y, Q: test_x[i]}
            hyp_val = sess.run(hyp, feed_dict = feed)
            act_val = test_y[i]
            match = hyp_val == act_val
            matches += 1 if match else 0
            print('{}/{} Predicted: {} Actual: {} Match: {}'
            .format(i+1, nb_test, class_trans[hyp_val], class_trans[act_val], match))

        print('Examples: {} Matches: {}'.format(nb_test, matches))

        # Racunanje tacnosti modela
        accuracy = matches / nb_test
        print('Accuracy: ', accuracy)

        ax_k.append(k)
        ax_acc.append(accuracy)

# Grafik koji prikazuje zavisnost funkcije troska od stepena polinoma
plt.plot(ax_k, ax_acc)
plt.show()

# ODGOVOR 3C
# Sudeći po grafiku, može se zaključiti da model koji uzima
# u obzir više obeležja ima veću tačnost zato što su objekti
# više razdvojeni u više dimenzija.
# Tvrđenje iz prethodog dela zadatka ostaje da važi
# zato što bi se povećanjem vrednosti parametra K u jednom trenutku
# primetilo opadanje tačnosti modela zato što bi veličina klase bila manja
# od broja susednih objekata koje uzimamo u obzir pri određivanju
# klase objekta iz skupa podataka za testiranje.


H, W = map(int, input().split())
A = [[c == '#' for c in input()] + [False] for _ in range(H)]
A.append([False] * (W + 1))

s = 0
for r in A:
  s += r.count(True)

h = w = c = 0
while True:
  c += 1
  if A[h + 1][w]:
    h += 1
    continue
  if A[h][w + 1]:
    w += 1
    continue

  break

print('Possible' if s == c else 'Impossible')


nums = list(map(int, input().split())) # 해당 숫자 랜덤한 순서로 입력 받기

nums.sort() # 오름차순 정렬

# 오름차순 정렬 시,
# 가장 작은 숫자는 항상 A,
# 두 번째로 작은 숫자는 항상 B
a = nums[0]
b = nums[1]

# 가장 큰 숫자는 항상 A + B + C 이므로
# C는 끝 숫자 - A - B
c = nums[6] - (a + b)

print(a, b, c)


import cv2
import numpy as np

f="1.jpg"
img=cv2.imread(f)
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

'''
lower_blue=np.array([100,43,46])
upper_blue=np.array([124,255,255])
mask_blue=cv2.inRange(hsv,lower_blue,upper_blue)
res_blue=cv2.bitwise_and(img,img,mask=mask)
'''

def mask(bgr_img,hsv_img,low,up):
    lower_color = np.array(low)
    upper_color = np.array(up)
    mask_color = cv2.inRange(hsv_img,lower_color,upper_color)
    return cv2.bitwise_and(bgr_img,bgr_img,mask=mask_color)

res_blue = mask(img,hsv,[100,43,46],[124,255,255])
res_green = mask(img,hsv,[35,43,46],[77,255,255])
res_orange = mask(img,hsv,[11,43,46],[25,255,255])
res_red = mask(img,hsv,[0,43,46],[6,255,255])
res_white = mask(img,hsv,[0,0,120],[170,60,255])
res_yellow = mask(img,hsv,[17,165,120],[34,255,255])

res = cv2.add(res_blue,res_green)
res = cv2.add(res,res_orange)
res = cv2.add(res,res_red)
res = cv2.add(res,res_white)
res = cv2.add(res,res_yellow)

cv2.imshow('res_blue',res_blue)
cv2.imshow('res_green',res_green)
cv2.imshow('res_orange',res_orange)
cv2.imshow('res_red',res_red)
cv2.imshow('res_white',res_white)
cv2.imshow('res_yellow',res_yellow)
cv2.imshow('res',res)
cv2.waitKey(0)


"""
This script produces GMRF grid
Author: Yaolin Ge
Contact: yaolin.ge@ntnu.no
Date: 2022-04-23
"""
import pandas as pd
import numpy as np
from Config.Config import *

lats = np.load(FILEPATH + "models/lats.npy")
lons = np.load(FILEPATH + "models/lons.npy")
depth = np.load(FILEPATH + "models/depth.npy")
x, y = latlon2xy(lats, lons, LATITUDE_ORIGIN, LONGITUDE_ORIGIN)
z = depth
GMRFGrid = np.vstack((x, y, z)).T

df = pd.DataFrame(GMRFGrid, columns=['x', 'y', 'z'])
df.to_csv(FILEPATH+"Config/GMRFGrid.csv", index=False)
print("GMRF grid is generated successfully!")


import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.select import Select

class a():
    def dropdown(self):
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
        driver.get("https://www.salesforce.com/in/form/signup/freetrial-sales/?d=topnav2-btn-ft")
        d = driver.find_element(By.NAME, "UserTitle")
        dd = Select(d)
        time.sleep(4)

        dd.select_by_visible_text("Marketing / PR Manager")
        time.sleep(4)

        dd.select_by_index(1)
        time.sleep(4)

        dd.select_by_value("CxO_General_Manager_AP")
        time.sleep(4)

b = a()
b.dropdown()


import requests
import time
import numpy as np
import matplotlib.pyplot as plt

HOST = "127.0.0.1"
PORT = "5000"
server_url = "http://" + HOST + ":" + PORT
response_times = []
cache_response_times = []
dist=[]

source = "115 Brittany Manor Drive, Amherst, MA, USA"
path_percentage = 425
destinations = ["Brittany Manor Drive, Amherst, MA, USA",
"United States Postal Service, University Drive, Amherst, MA, USA",
"University of Massachusetts Amherst, Amherst, MA, USA",
"Brandywine Apartments, Brandywine, Amherst, MA, USA",
"Sunderland, MA, USA" ]

minimize_elevation_gain = True
for destination in destinations:
    request_json = {
                                                     "source": source,
                                                     "destination": destination,
                                                     "algorithm_id": 0,
                                                     "path_percentage": path_percentage,
                                                     "minimize_elevation_gain": minimize_elevation_gain,
                                                     "transportation_mode": 1
}

    post_time_start = time.time()
    server_response = requests.post(f'{server_url}/getroute', json= request_json)
    post_time_end = time.time()
    print(server_response)
    server_json = server_response.json()
    if not "message" in server_json:
        response_times.append(post_time_end - post_time_start)
        dist.append(server_json["best_path_distance"])

for destination in destinations:
    request_json = {
                                                     "source": source,
                                                     "destination": destination,
                                                     "algorithm_id": 0,
                                                     "path_percentage": path_percentage,
                                                     "minimize_elevation_gain": minimize_elevation_gain,
                                                     "transportation_mode": 1
                                                     }

    post_time_start = time.time()
    server_response = requests.post(f'{server_url}/getroute', json= request_json)
    post_time_end = time.time()
    print(server_response)
    server_json = server_response.json()
    if not "message" in server_json:
        cache_response_times.append(post_time_end - post_time_start)

print(f"response_times:{response_times}")
print(f"cache_response_times:{cache_response_times}")

plt.plot(dist,response_times, label ='1st Time API Hit (No Cache)')
plt.plot(dist,cache_response_times, '-.', label ='2nd Time API Hit (Cached)')

plt.xlabel("Path Distance (in meters)")
plt.ylabel("API Response Time (in seconds)")
plt.legend()
plt.title('Response Time for 1st time and 2nd time API Hit for same configuration')
plt.show()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=C0103
"""
Discourse relation types model/task.
"""
__author__ = "GW [http://gw.tnode.com/] <gw.2015@tnode.com>"
__license__ = "GPLv3+"

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import numpy as np
from keras.layers.core import Activation, TimeDistributedDense, RepeatVector, Permute
from keras.layers.recurrent import GRU

from common import build_index
from conll16st.relations import tag_to_rtsip, filter_tags

### Model

def rel_types_model(model, ins, max_len, embedding_dim, rel_types2id_size, focus, pre='rtypes'):
    """Discourse relation types model as Keras Graph."""

    # prepare focus dimensionality
    model.add_node(RepeatVector(rel_types2id_size), name=pre + '_focus_rep', input=focus)
    model.add_node(Permute((2, 1)), name=pre + '_focus', input=pre + '_focus_rep')

    # discourse relation types dense neural network (sample, time_pad, rel_types2id)
    model.add_node(TimeDistributedDense(rel_types2id_size, init='he_uniform'), name=pre + '_dense', input=ins[0])
    model.add_node(Activation('softmax'), name=pre + '_softmax', input=pre + '_dense')

    # multiplication to focus the activations (doc, time_pad, rel_types2id)
    model.add_node(Activation('linear'), name=pre + '_out', inputs=[pre + '_focus', pre + '_softmax'], merge_mode='mul')
    return pre + '_out'

def rel_types_one_model(model, ins, max_len, embedding_dim, rel_types2id_size, focus, pre='rtypesone'):
    """Discourse relation types model to return one output as Keras Graph."""

    # forward recurrent layer returning one last output (sample, rel_types2id)
    model.add_node(GRU(rel_types2id_size, return_sequences=False, activation='sigmoid', inner_activation='sigmoid', init='he_uniform', inner_init='orthogonal'), name=pre + '_fwd', input=ins[0])

    # backward recurrent layer returning one last output (sample, rel_types2id)
    model.add_node(GRU(rel_types2id_size, return_sequences=False, activation='sigmoid', inner_activation='sigmoid', init='he_uniform', inner_init='orthogonal', go_backwards=True), name=pre + '_bck', input=ins[0])

    # join activations (sample, rel_types2id)
    model.add_node(Activation('linear'), name=pre + '_out', inputs=[pre + '_fwd', pre + '_bck'], merge_mode='ave')
    return pre + '_out'

### Build index

def build_rel_types2id(rel_types, max_size=None, min_count=1, rel_types2id=None):
    """Build vocabulary index for all discourse relation types (reserved ids: 0 = missing, 1 = out-of-vocabulary)."""

    return build_index(rel_types, max_size=max_size, min_count=min_count, index=rel_types2id)

### Encode data

def encode_x_rel_types(word_metas_slice, rel_types2id, rel_types2id_size, max_len, filter_prefixes=None, oov_key=""):
    """Encode discourse relation types as normalized vectors (sample, time_pad, rel_types2id)."""

    # crop sequence if needed
    word_metas_slice = word_metas_slice[:max_len]

    # encode all relation types in same row
    x = np.zeros((max_len, rel_types2id_size), dtype=np.float32)

    for i in range(max_len):
        if i < len(word_metas_slice):
            # mark all relation types
            tags = filter_tags(word_metas_slice[i]['RelationTags'], filter_prefixes)
            for rel_tag in tags:
                rel_type, _, _, _ = tag_to_rtsip(rel_tag)

                try:
                    x[i, rel_types2id[rel_type]] += 1.
                except KeyError:  # missing in vocabulary
                    x[i, rel_types2id[oov_key]] += 1.

        if i >= len(word_metas_slice) or not filter_tags(word_metas_slice[i]['RelationTags'], filter_prefixes):
            # no relation types present
            x[i, rel_types2id[None]] += 1.

    # normalize by rows to [0,1] interval
    x_sum = np.sum(x, axis=1)
    x2 = (x.T / x_sum).T
    x2[x_sum == 0.] = x[x_sum == 0.]  # prevent NaN
    return x2

def decode_x_rel_types(x_rel_types, token_range, relation, rel_types2id, rel_types2id_size):
    """Decode one discourse relation type for a given relation spans."""

    # sum type predictions for relation tokens
    totals = np.zeros((rel_types2id_size,))
    for i, token_id in enumerate(token_range):
        if token_id in relation['Arg1'] or token_id in relation['Arg2'] or token_id in relation['Connective'] or token_id in relation['Punctuation']:
            totals += x_rel_types[i]
            #XXX: / np.max(x_rel_types[i])

    # return most probable type
    rel_type = None
    max_total = -1.
    for t, j in rel_types2id.items():
        if totals[j] > max_total:
            max_total = totals[j]
            rel_type = t
    return rel_type, totals

def encode_x_rel_types_one(rel_type, rel_types2id, rel_types2id_size, oov_key=""):
    """Encode discourse relation types as one normalized vector (sample, rel_types2id)."""

    # one-hot encode type
    try:
        i = rel_types2id[rel_type]
    except KeyError:  # missing in vocabulary
        i = rel_types2id[oov_key]
    x = np.zeros((rel_types2id_size,), dtype=np.float32)
    x[i] = 1.
    return x

def decode_x_rel_types_one(x_rel_types_one, rel_types2id, rel_types2id_size):
    """Decode one discourse relation type from a normalized vector."""

    # normalize by rows to [0,1] interval
    x_sum = np.sum(x_rel_types_one)
    totals = x_rel_types_one / x_sum
    totals[x_sum == 0.] = x_rel_types_one[x_sum == 0.]  # prevent NaN

    # return most probable type
    rel_type = None
    max_total = -1.
    for t, j in rel_types2id.items():
        if totals[j] > max_total:
            max_total = totals[j]
            rel_type = t
    return rel_type, totals

### Tests

def test_build_rel_types2id():
    rel_types = {14903: 'Implicit', 14904: 'Explicit', 14878: 'Explicit'}
    t_rel_types2id = {None: 0, "": 1, "Explicit": 2, "Implicit": 3}
    t_rel_types2id_size = len(t_rel_types2id)

    rel_types2id, rel_types2id_size = build_rel_types2id(rel_types)
    assert rel_types2id == t_rel_types2id
    assert rel_types2id_size == t_rel_types2id_size

def test_encode_x_rel_types():
    word_metas_slice = [
        {'RelationIDs': [14903], 'SentenceID': 30, 'RelationTags': ['Implicit:Comparison.Contrast:14903:Arg1'], 'DocID': 'wsj_1000', 'TokenID': 854, 'SentenceOffset': 827, 'Text': 'trading', 'ParagraphID': 13, 'RelationSpans': ['Arg1']},
        {'RelationIDs': [], 'SentenceID': 30, 'RelationTags': [], 'DocID': 'wsj_1000', 'TokenID': 855, 'SentenceOffset': 827, 'Text': '.', 'ParagraphID': 13, 'RelationSpans': []},
        {'RelationIDs': [14903, 14904], 'SentenceID': 31, 'RelationTags': ['Implicit:Comparison.Contrast:14903:Arg2', 'Explicit:Comparison.Concession:14904:Arg1'], 'DocID': 'wsj_1000', 'TokenID': 859, 'SentenceOffset': 857, 'Text': 'that', 'ParagraphID': 13, 'RelationParts': ['Arg2', 'Arg1']},
        {'RelationIDs': [14903, 14904], 'SentenceID': 31, 'RelationTags': ['Implicit:Comparison.Contrast:14903:Arg2', 'Explicit:Comparison.Concession:14904:Arg1'], 'DocID': 'wsj_1000', 'TokenID': 860, 'SentenceOffset': 857, 'Text': '``', 'ParagraphID': 13, 'RelationSpans': ['Arg2', 'Arg1']},
    ]
    rel_types2id = {None: 0, '': 1, 'Implicit': 2, 'Explicit': 3, 'EntRel': 4}
    rel_types2id_size = len(rel_types2id)
    max_len_0 = 3
    max_len_1 = 5
    t_x_0 = [
        [0, 0, 1, 0, 0],
        [1, 0, 0, 0, 0],
        [0, 0, 0.5, 0.5, 0],
    ]
    t_x_1 = [
        [0, 0, 1, 0, 0],
        [1, 0, 0, 0, 0],
        [0, 0, 0.5, 0.5, 0],
        [0, 0, 0.5, 0.5, 0],
        [1, 0, 0, 0, 0],
    ]

    x_0 = encode_x_rel_types(word_metas_slice, rel_types2id, rel_types2id_size, max_len_0)
    assert (x_0 == t_x_0).all()

    x_1 = encode_x_rel_types(word_metas_slice, rel_types2id, rel_types2id_size, max_len_1)
    assert (x_1 == t_x_1).all()

def test_decode_x_rel_types():
    rel_types2id = {None: 0, '': 1, 'Implicit': 2, 'Explicit': 3, 'EntRel': 4}
    rel_types2id_size = len(rel_types2id)
    relation = {
        'Arg1': [854],
        'Arg1Len': 1,
        'Arg2': [859, 860],
        'Arg2Len': 2,
        'Connective': [],
        'ConnectiveLen': 0,
        'Punctuation': [],
        'PunctuationLen': 0,
        'PunctuationType': '',
        'DocID': 'wsj_1000',
        'ID': 14903,
        'TokenMin': 854,
        'TokenMax': 861,
        'TokenCount': 3,
    }
    token_start = 854
    token_end = 861
    x_rel_types_0 = [
        [0., 0., 1., 0., 0.],
        [1., 0., 0., 0., 0.],
        [1., 0., 0., 0., 0.],
        [1., 0., 0., 0., 0.],
        [1., 0., 0., 0., 0.],
        [0., 0., 0.5, 0.5, 0.],
        [0., 0., 0.5, 0.5, 0.],
    ]
    t_type_0 = 'Implicit'
    x_rel_types_1 = [
        [0., 0.25, 0., 0.75, 0.],
        [0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 1.],
        [0., 0., 0., 1., 0.],
        [1., 0., 0., 0., 0.],
        [0., 0.5, 0., 0.5, 0.],
        [0., 0., 0., 0.5, 0.5],
    ]
    t_type_1 = 'Explicit'

    type_0, totals_0 = decode_x_rel_types(x_rel_types_0, range(token_start, token_end), relation, rel_types2id, rel_types2id_size)
    assert type_0 == t_type_0

    type_1, totals_1 = decode_x_rel_types(x_rel_types_1, range(token_start, token_end), relation, rel_types2id, rel_types2id_size)
    assert type_1 == t_type_1

def test_encode_x_rel_types_one():
    rel_types2id = {None: 0, '': 1, 'Implicit': 2, 'Explicit': 3, 'EntRel': 4}
    rel_types2id_size = len(rel_types2id)
    rel_type_0 = "Implicit"
    t_x_0 = [0, 0, 1, 0, 0]
    rel_type_1 = "AltLex"
    t_x_1 = [0, 1, 0, 0, 0]

    x_0 = encode_x_rel_types_one(rel_type_0, rel_types2id, rel_types2id_size)
    assert (x_0 == t_x_0).all()

    x_1 = encode_x_rel_types_one(rel_type_1, rel_types2id, rel_types2id_size)
    assert (x_1 == t_x_1).all()

def test_decode_x_rel_types_one():
    rel_types2id = {None: 0, '': 1, 'Implicit': 2, 'Explicit': 3, 'EntRel': 4}
    rel_types2id_size = len(rel_types2id)
    x_rel_types_one_0 = [0, 0, 1, 0, 0]
    t_type_0 = "Implicit"
    x_rel_types_one_1 = [0, 1, 0, 0, 0]
    t_type_1 = ""

    type_0, totals_0 = decode_x_rel_types_one(x_rel_types_one_0, rel_types2id, rel_types2id_size)
    assert type_0 == t_type_0

    type_1, totals_1 = decode_x_rel_types_one(x_rel_types_one_1, rel_types2id, rel_types2id_size)
    assert type_1 == t_type_1

if __name__ == '__main__':
    import pytest
    pytest.main(['-s', __file__])


from django import forms
from requests.models import Request

class RequestForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(RequestForm, self).__init__(*args, **kwargs)
        self.fields['description'].widget.attrs.update({'rows': '4'})
        for field_name in ['applicant', 'assigned_to', 'apartment', 'status', 'priority', 'request_type']:
            self.fields[field_name].widget.attrs.update({'class': 'form-control select2'})

    class Meta:
        model = Request
        fields = (
            'assigned_to', 'status', 'priority', 'request_type', 'applicant', 'apartment', 'closed_on', 'description')


'''
Given an array A of N integers and a number K. The task is to print the numbers with K frequency.
For each testcase, output the array elements in sorted order which are occurring K times in the array if present, else print -1.
'''
from collections import OrderedDict
if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        num = list(map(int,input().split()))
        N , K = num[0],num[1]
        A = sorted(list(map(int,input().split())))
        d = OrderedDict()
        for ch in A :
            if ch not in d:d[ch]=1
            else:d[ch]+=1
        l = []
        for k,v in d.items():
            if v==K:l.append(k)
        if l==[]:print(-1)
        else:print(*l)


"""providers.bzl contains Providers instances that are public.
"""

JarIndexerAspectInfo = provider(
    "a provider for Jar Indexing",
    fields = {
        "info_file": "The index database",
        "jar_index_files": "A list of jar index files",
    },
)


import csv

import openai
import pandas as pd

# Read Key
T = pd.read_csv('../API_key.csv')
key = T['OpenAI API key']
key = key[0]
openai.api_key = key

def opt3_completion(promt, engine='text-davinci-003', temp=0.2, top_p=1.0, tokens=400, freq_pen=0.0, pres_pen=0.0,
                    stop=['<<END>>']):
    prmt = promt.encode('ASCII', errors='ignore').decode()
    r = openai.Completion.create(
        engine=engine,
        prompt=prmt,
        temperature=temp,
        max_tokens=tokens,
        top_p=top_p,
        frequency_penalty=freq_pen,
        presence_penalty=pres_pen,
        stop=stop)
    text = r['choices'][0]['text'].strip()
    return text

if __name__ == '__main__':
    # Enter metadata
    METADATA = pd.read_csv('/Users/marioacuna/Library/Mobile '
                           'Documents/com~apple~CloudDocs/Project_AcuNez/Studies_December2022'
                           '/Studies_December2022_CheckedMA.csv')

    iid = 0
    responses = []
    for itext in METADATA['summary']:
        iid =iid+ 1
        init_text = "From the following text, infer from where the data were taken (leucocytes?, PBMC?, etc), providing  " \
                    "me with the origin of the samples where the " \
                    "and what kind of patients (AD, PD, MS, etc). Nnothing more, be as concise as possible." \
                    " TEXT: \n <<BLOCK>>"

        text = itext
        #print(text)

        prompt = init_text.replace('<<BLOCK>>', text)
        #prompt = prompt + 'JAX: '
        response = opt3_completion(prompt)
        print(str(iid) + " " + response)
        responses.append(response)
        #conversation.append('JAX: %s' % response)

    with open('output.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['iteration', 'output'])
        for i, output in enumerate(responses):
            writer.writerow([i, output])


from assets import Assets
from helpers import *
from market_data import getInflation
from portfolio import Portfolio
from simulation import Simulation

def runSimulation(
        length,
        initialPortfolio,
        failureThreshold,
        initStrategies,
        minSimYear,
        maxSimYear,
        ignoreInfation=False,
        testCallback=None):
    """ Runs a simulation. This is the inner loop to this entire project.

    :param length: The length of the simulation. Inclusive on both sides, so passing in
                two here would cause 36 months of growth and withdrawal cycles.
    :param initialPortfolio: The initial portfolio. This is a Portfolio class instance.
    :param failureThreshold: Assert a failure if a withdrawal ever falls below this amount.
    :param initStrategies: A tuple of the various strategies. Each strategy is also a tuple of the form
                (Strategy object, Assets object, weight)
                The strategy object is an instantiation of a particular strategy.
                The Assets are the initial asset allocation.
                The weights of all the (s, a, w) in the initStrategies tuple must sum to 1.0.
    :param minSimYear: The inclusive beginning year of the simulation.
    :param maxSimYear: The inclusive ending year of the simulation.
    :param ignoreInfation: Self explanatory. Worth noting that the web interface never sets this as true.
                It is basically only used for tests. Defaults to False.
    :param testCallback: Provided by tests as a hook for assertions. Defaults to null.
    :return: A Simulation class instance. This contains various results and stored data.
    """
    simulation = Simulation(minSimYear, maxSimYear, length, ignoreInfation, initialPortfolio, failureThreshold)
    strategies = []
    initPortfolios = []
    totalWeight = 0.0
    for s in initStrategies:
        strategies.append(s[0])
        initPortfolios.append(Portfolio(s[1], s[2] * initialPortfolio))
        totalWeight += s[2]

    if not isclose(totalWeight, 1.0):
        raise RuntimeError("Your weights don't add up to 1.0")

    simulationIteration = 0
    # why 2? python and last year of the simulation are both exclusive
    for startYear in range(minSimYear, maxSimYear - length + 2):
        for i in range(0, len(strategies)):
            portfolio = initPortfolios[i].copy()
            strategies[i].reset(portfolio)

        candidateFailureYear = None
        inflationRate = None
        try:
            underflow = 0.0
            overflow = 0.0
            for simulationYear in range(startYear, startYear + length):
                candidateFailureYear = simulationYear
                inflationRate = getInflation(startYear-1, simulationYear-1)
                if ignoreInfation:
                    inflationRate = 1.0
                failMin = failureThreshold * inflationRate
                monthsPerYear = 12
                for month in range(1, monthsPerYear + 1):
                    monthGrowth = Assets.getMarketReturns(simulationYear, month)
                    if testCallback:
                        pv = sum(s.getPortfolioValue() for s in strategies) / inflationRate
                        testCallback["pre"](pv, monthGrowth, strategies)

                    actualWithdrawal = 0.0
                    for i in range(0, len(strategies)):
                        actualWithdrawal += strategies[i].withdraw(inflationRate, monthsPerYear)
                        strategies[i].grow(monthGrowth)
                    currentPortfolioValue = sum(s.getPortfolioValue() for s in strategies) / inflationRate
                    simulation.recordData(
                        simulationIteration,
                        simulationYear,
                        month,
                        actualWithdrawal / inflationRate,
                        currentPortfolioValue)
                    diff = actualWithdrawal - (
                        sum(s.getInitialWithDrawal() / monthsPerYear for s in strategies) * inflationRate)

                    if testCallback:
                        testCallback["post"](currentPortfolioValue, actualWithdrawal / inflationRate)

                    if actualWithdrawal <= .999999999 * failMin / monthsPerYear:
                        raise StopIteration
                    if diff < 0:
                        underflow += diff / inflationRate
                    else:
                        overflow += diff / inflationRate
            simulation.recordSuccess()
        except StopIteration:
            simulation.recordFailure(startYear, candidateFailureYear)
        finally:
            simulation.endPortfolioValue.append(sum(s.getPortfolioValue() for s in strategies) / inflationRate)
            simulation.underflow.append(underflow)
            simulation.overflow.append(overflow)
            simulation.endRelativeInflation.append(inflationRate)
            simulationIteration += 1
    simulation.finalize()
    return simulation


import numpy as np
import matplotlib.pyplot as plt
import pathlib
from pathlib import Path
import os
import random

import PIL
import PIL.Image
import tensorflow as tf
import sys
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import CSVLogger
import pandas as pd
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
# import tensorflow_datasets as tfds
import seaborn as sns
import glob
print(tf.__version__)

object_category_names = sorted(os.listdir('train'))
object_categories = {}
for i, name in enumerate(object_category_names):
    object_categories[i] = name

#result = object_categories.items()
#data = list(result)

#object_labels_two = np.array(data)
object_labels_two = np.delete(np.array(list(object_categories.items())),0,1)
object_labels = object_labels_two.reshape(len(object_categories),)

print(object_labels)

def display_sample(sample_images, sample_labels, sample_predictions=None, num_rows=8, num_cols=8,
                   plot_title=None, fig_size=None):
    """ display a random selection of images & corresponding labels, optionally with predictions
        The display is laid out in a grid of num_rows x num_col cells
        If sample_predictions are provided, then each cell's title displays the prediction
        (if it matches actual) or actual/prediction if there is a mismatch
    """
    from PIL import Image
    import seaborn as sns
    assert len(sample_images) == num_rows * num_cols

    # a dict to help encode/decode the labels
    global object_categories

    with sns.axes_style("whitegrid"):
        sns.set_context("notebook", font_scale=1.1)
        sns.set_style({"font.sans-serif": ["Verdana", "Arial", "Calibri", "DejaVu Sans"]})

        f, ax = plt.subplots(num_rows, num_cols, figsize=((14, 12) if fig_size is None else fig_size),
            gridspec_kw={"wspace": 0.02, "hspace": 0.25}, squeeze=True)
        #fig = ax[0].get_figure()
        f.tight_layout()
        f.subplots_adjust(top=0.93)

        for r in range(num_rows):
            for c in range(num_cols):
                image_index = r * num_cols + c
                ax[r, c].axis("off")
                # show selected image
                pil_image = sample_images[image_index] #Image.fromarray(sample_images[image_index])
                ax[r, c].imshow(pil_image.astype('uint8'))

                if sample_predictions is None:
                    # show the actual labels in the cell title
                    title = ax[r, c].set_title("%s" % object_categories[sample_labels[image_index]])
                else:
                    # else check if prediction matches actual value
                    true_label = sample_labels[image_index]
                    pred_label = sample_predictions[image_index]
                    prediction_matches_true = (sample_labels[image_index] == sample_predictions[image_index])
                    if prediction_matches_true:
                        # if actual == prediction, cell title is prediction shown in green font
                        title = object_categories[true_label]
                        title_color = 'g'
                    else:
                        # if actual != prediction, cell title is actua/prediction in red font
                        title = '%s/%s' % (object_categories[true_label][:5], object_categories[pred_label][:5])
                        title_color = 'r'
                    # display cell title
                    title = ax[r, c].set_title(title)
                    plt.setp(title, color=title_color)
        # set plot title, if one specified
        if plot_title is not None:
            f.suptitle(plot_title)

        plt.show()
        plt.close()

#args = -1
batch_size = 128
img_height = 200
img_width = 200

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
	Path(os.getcwd() + "\\train"),
	labels="inferred",
	color_mode='rgb',
	validation_split=0.2,
	subset="training",
	seed=123,
	image_size=(img_height, img_width),
	batch_size=batch_size)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
	Path(os.getcwd() + "\\train"),
	labels="inferred",
	color_mode='rgb',
	validation_split=0.2,
	subset="validation",
	seed=123,
	image_size=(img_height, img_width),
	batch_size=batch_size)

test_ds = tf.keras.preprocessing.image_dataset_from_directory(
	Path(os.getcwd() + "\\test"),
	labels="inferred",
	color_mode='rgb',
	seed=123,
	shuffle=True,
	image_size=(img_height, img_width),
	batch_size=batch_size)

train_steps = int(train_ds.cardinality() - 1)
validation_steps = int(val_ds.cardinality() - 1)
test_steps = int(test_ds.cardinality() - 1)

print('Training set size %d, step-size %d' % (train_steps*batch_size, train_steps))
print('Validation set size %d, step-size %d' % (validation_steps*batch_size, validation_steps))
print('Test set size %d, step-size %d' % (test_steps*batch_size , test_steps))
class_names = train_ds.class_names
train_ds = train_ds.repeat()
val_ds = val_ds.repeat()

normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)

normalized_train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))

images, labels = iter(normalized_train_ds).next()
#print(labels)
nd_labels = labels.numpy()
nd_images = images.numpy()
#labels = np.argmax(labels, axis=1)

display_sample(nd_images[:16], nd_labels[:16], num_rows=4, num_cols=4, plot_title='Sample Training Data', fig_size=(16,20))

if Path.exists(Path(os.getcwd() + "\\saved_model")):
	print("./saved_model existiert bereits.")
	model = tf.keras.models.load_model('Q:/rgbd-data/RGBD multimodal/saved_model')

	print("Modell geladen.")

else:

	print(class_names)
	normalization_layer = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)

	#normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
	#image_batch, labels_batch = next(iter(normalized_ds))
	#first_image = image_batch[0]
	# Notice the pixels values are now in `[0,1]`.
	# print(np.min(first_image), np.max(first_image))
	#
	# AUTOTUNE = tf.data.experimental.AUTOTUNE
	#
	# train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
	# val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

	num_classes = len(class_names)
	# num_classes = 5
	#factor = 0.5
	#regularizer = contrib.layers.l2_regularizer(scale=0.1)
	model = tf.keras.Sequential([
		layers.experimental.preprocessing.Rescaling(1./255),
		#layers.Conv2D(16*factor, 3, activation='relu'),
		#layers.MaxPooling2D(),
		#layers.Activation('sigmoid'),
		layers.Conv2D(32, 3, activation='relu'),
		layers.MaxPooling2D(),
		#layers.Activation('sigmoid'),
		layers.Conv2D(64, 3, activation='relu'),
		#layers.Activation('sigmoid'),
		layers.MaxPooling2D(),
		layers.Conv2D(128, 3, activation='relu'),
		layers.MaxPooling2D(),
		layers.Conv2D(256, 3, activation='relu'),
		layers.MaxPooling2D(),
		layers.Dropout(0.10),
		layers.Dense(1024, activation='relu', kernel_regularizer="l2"),
		layers.MaxPooling2D(),
		layers.Dropout(0.10),
		#layers.Dropout(.2),
		#layers.Activation('sigmoid'),
		#layers.Conv2D(128, 3, activation='relu'),
		#layers.Conv2D(512*factor, 3, activation='relu'),
		#layers.MaxPooling2D(),
		#layers.Activation('sigmoid'),
		layers.Flatten(),
		#layers.Flatten(),
		layers.Dense(1024, activation='relu', kernel_regularizer="l2"),
		layers.Dropout(0.10),
		#layers.Softmax(),
		layers.Dense(num_classes)
	])

	model.compile(
		optimizer='adam',
		loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
		metrics=['accuracy'])

	epochs = 15
	faster_steps = train_steps // 4
	csv_logger = CSVLogger('training.log', separator=',', append=False)
	#csv_logger_test = CSVLogger('test.log', separator=',', append=False)

	history = model.fit(
		train_ds,
		validation_data=val_ds,
		callbacks=[csv_logger],
		epochs=epochs,
		steps_per_epoch=faster_steps,
		validation_steps=validation_steps

	)

	model.save(
		'Q:/rgbd-data/RGBD multimodal/saved_model',
		overwrite=True,
		include_optimizer=True,
		save_format=None,
		signatures=None,
		options=None,
		save_traces=True,
	)

if Path.exists(Path(os.getcwd() + "\\saved_model")):
	log_data = pd.read_csv('training.log', sep=',', engine='python')
	history = log_data
	acc = history['accuracy']
	val_acc = history['val_accuracy']
	loss = history['loss']
	val_loss = history['val_loss']

else:
	history = model.history

	acc = history.history['accuracy']
	val_acc = history.history['val_accuracy']

	loss = history.history['loss']
	val_loss = history.history['val_loss']

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
# plt.plot(epochs_range, acc, label='Training Accuracy')
# plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
# plt.plot(epochs_range, loss, label='Training Loss')
# plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

model.summary()
plt.clf()

all_predictions, all_labels = [], []
#all_predictions = np.empty(test_steps * batch_size, dtype=np.int)
#all_labels = np.empty(test_steps * batch_size, dtype=np.int)

print("Total test batches to predict: ",test_steps)

for i in range(test_steps):
	print("Testing batch nr:",i,"out of",test_steps)
	images, labels = iter(test_ds).next()
	predict_labels = model.predict(images)
	predict_labels = np.argmax(predict_labels, axis=1)
	nd_labels = labels.numpy()
	nd_images = images.numpy()

	all_predictions.extend(predict_labels)
	all_labels.extend(nd_labels)

all_labels = np.array(all_labels)
all_predictions = np.array(all_predictions)
#np.savetxt("test_labels.csv", all_labels, delimiter=',')test_steps
#np.savetxt("test_predictions.csv", all_predictions, delimiter=',')

def int_label_to_string(x):
  return object_labels[x]

print("\n Confusion Matrix:")
cm = confusion_matrix(int_label_to_string(all_labels),int_label_to_string(all_predictions), labels=object_labels,normalize='true')
np.savetxt("cm.csv", cm, delimiter=",")
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=object_labels)
#plt.figure(figsize=(10, 10))
disp.plot(xticks_rotation='vertical')
for labels in disp.text_.ravel():
    labels.set_fontsize(6)

plt.savefig('confusion_matrix.png',dpi=300)
#plt.show()

#number_of_samples = np.zeros((len(object_categories),), dtype=np.uint64)
#wrong_labels = np.zeros((len(object_categories),), dtype=np.uint64)
#accuracies = np.zeros((len(object_categories),), dtype=np.uint64)
#for i in range(len(all_labels)):
#	number_of_samples[all_labels[i]] += 1
#	if all_labels[i] != all_predictions[i]:
#		wrong_labels[all_labels[i]] +=1

#print("Per object accuracy:")
for i in range(len(object_categories)):
#	correct_amount = number_of_samples[i] - wrong_labels[i]
#	accuracies[i] = correct_amount / number_of_samples[i]
#	print(object_categories[i]," accuracy: ", correct_amount / number_of_samples[i])
	print(object_categories[i]," accuracy: ", cm[i,i])

correct_pred_count = (all_labels == all_predictions).sum()
test_acc = correct_pred_count / len(all_labels)
print('\n We got %d of %d correct (or %.3f accuracy)' % (correct_pred_count, len(all_labels), test_acc))
#loss, acc = model.evaluate(test_ds, steps=test_steps, verbose=1)
#print('Testing data   -> loss: %.3f, acc: %.3f' % (loss, acc))

images, labels = iter(test_ds).next()

#print(labels)
nd_labels = labels.numpy()
nd_images = images.numpy()
#labels = np.argmax(labels, axis=1)

#display_sample(nd_images[:16], nd_labels[:16], num_rows=4, num_cols=4, plot_title='Sample Training Data', fig_size=(16,20))
predict_labels = model.predict(images)
predict_labels = np.argmax(predict_labels, axis=1)

#print("\n Actual label index:\n", nd_labels)
#print("\n Predicted label index:\n", predict_labels)

# display sample predictions

display_sample(nd_images[:64], nd_labels[:64], sample_predictions=predict_labels[:64], num_rows=8, num_cols=8,
               plot_title='Sample Predictions on Test Dataset', fig_size=(18,20))

#normalized_test_ds = test_ds.map(lambda x, y: (normalization_layer(x), y))
#images, labels = iter(normalized_test_ds).next()
#images, labels = iter(normalized_test_ds).next()

#score = model.evaluate(test_ds,verbose=1)
#print(score)
print()
loss, acc = model.evaluate(train_ds, steps=train_steps, verbose=1)
print('Training data  -> loss: %.3f, acc: %.3f' % (loss, acc))
loss, acc = model.evaluate(val_ds, steps=validation_steps, verbose=1)
print('Cross-val data -> loss: %.3f, acc: %.3f' % (loss, acc))
loss, acc = model.evaluate(test_ds, steps=test_steps, verbose=1)
print('Testing data   -> loss: %.3f, acc: %.3f' % (loss, acc))


import sys

import pygame

from bullet import Bullet

def check_keydown_events(event, ai_settings, screen, ship, bullets):
    """response to the keydown"""
    if event.key == pygame.K_UP:
        ship.moving_up = True
    elif event.key == pygame.K_DOWN:
        ship.moving_down = True
    elif event.key == pygame.K_SPACE:
        # build a bullet and join it to the group of bullets
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

def check_keyup_events(event, ship):
    """response to the keyup"""
    if event.key == pygame.K_UP:
        ship.moving_up = False
    elif event.key == pygame.K_DOWN:
        ship.moving_down = False

def check_events(ai_settings, screen, ship, bullets):
    """response to the keyboard and the mouse"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)

def update_screen(ai_settings, screen, ship, bullets):
    """update the screen's image, and change to the new screen"""
    # redraw all the bullets after the aliens and the ship
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    # Redraw the screen each time starts the loop
    screen.fill(ai_settings.bg_color)
    ship.blitme()

    # show the lastest drawing screen
    pygame.display.flip()


import os
import trimesh
import os.path as path
import configargparse
from trimesh_handler import *
import timeit

# --------------------------------------------------------#
# ------------------------ PARSER ------------------------#
# --------------------------------------------------------#

def get_parser():
    parser = configargparse.get_argument_parser()

    parser.add_argument('--input', '-in', type=str,
                        help='object name to use as input from inputs folder, input file MUST be in inputs folder')
    parser.add_argument('--input-type', '-t', type=str, default='ply',
                        help='input file type, defauld is ply')
    parser.add_argument('--iterations', '-i', type=int, default=1,
                        help='number of iterations to run, default is a single iteration')
    parser.add_argument('--results', '-r', type=int, default=1,
                        help='number of results to provide, must be maximum as number of iterations')
    parser.add_argument('--constraints', '-c', nargs='+', type=int, required=True,
                        help='a list of constraints chosen from 0 to 5: [0-2]:Rotational, [3-5]:XYZ')
    parser.add_argument('--convex-hull', '-cv', action='store_true', required=False,
                        help='a flag to use input`s convex hull')

    parsed = parser.parse_args()
    parsed.constraints = np.array([[1.0 if i in parsed.constraints else 0.0 for i in range(6)]])
    parsed.clustering = parsed.results < parsed.iterations
    assert parsed.iterations >= 0 and parsed.results >= 0, 'iteration must be bigger than 0'
    assert parsed.results <= parsed.iterations, 'iterations must be bigger than results'

    return parsed

# ------------------------------------------------------#
# ------------------------ MAIN ------------------------#
# ------------------------------------------------------#

def input_path(filename: str, file_type='ply'):
    file_full_path = f'{path.pardir}{path.sep}{"inputs"}{path.sep}{filename}.{file_type}'
    if path.exists(file_full_path):
        return file_full_path
    else:
        raise ValueError('Given input file doesnt exist, please rerun with an existing file or add the wanted file into '
                         'inputs directory')

def main():
    vectors = []
    results = []

    args = get_parser()
    mesh_data = trimesh_handler(
        mesh=trimesh.load(input_path(args.input, args.input_type)),
        constraints=args.constraints,
        convex_hull=args.convex_hull
    )

    # Folders preparing
    c = ''.join([str(i) for i in range(6) if args.constraints[0][i]])
    out_folder = f'{path.pardir}{path.sep}outputs{path.sep}{args.input}_{c}{path.sep}'
    all_folder = f'{out_folder}all'
    os.makedirs(all_folder, exist_ok=True)
    start = timeit.default_timer()
    for iteration in range(args.iterations):
        print(f'\n Staring computing for {args.input} ')
        print(f'\n\t - Computing iteration: #{iteration}')
        starting_point = calc_starting_point(mesh_data)
        curr_shell = shell_computation(mesh_data, starting_point)
        results.append(mesh_data.current_holder_T)
        vectors.append(curr_shell)
        out_path = f'{all_folder}{path.sep}{args.input}_{iteration}.obj'
        mesh_data.current_holder_T.export(out_path)
        print(f'\n\t\t ~ Exporting: {out_path}')

    if args.clustering:
        clustered_folder = f'{out_folder}clustered'
        os.makedirs(clustered_folder, exist_ok=True)

        print(f'\n\t Running Clustering {args.iterations}->{args.results}:')
        clustering = clustering_fit(vectors, args.results)
        clustered = [False for i in range(args.results)]

        for i in range(len(vectors)):
            for j in range(i, len(vectors)):
                if clustering.labels_[j] == i:
                    if not clustered[i]:
                        out_path = f'{clustered_folder}{path.sep}{args.input}_{i}.obj'
                        results[i].export(out_path)
                        print(f'\t ~ Export: {out_path}')
                        clustered[i] = True

    print(f'\nFinished running Auto-Connect for {args.input} \n')
    print(f'Execution took: {timeit.default_timer()-start} seconds\n')

if __name__ == "__main__":
    main()


import datetime
import django_filters

from django.shortcuts import render

from django_filters.rest_framework import FilterSet
from django_filters import DateRangeFilter, DateFilter, CharFilter, NumberFilter
from django.shortcuts import render

from django.views.generic import TemplateView
from django.views.generic.edit import CreateView

from django.contrib.messages.views import SuccessMessageMixin
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.urls import reverse_lazy
from django.http import HttpResponseRedirect

from rest_framework import viewsets, filters
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated

from django.db.models import Q

from .models import TimeSheet, PaymentMade, CashOut
from users.models import Clients
from .serializers import TimeSheetSerializer, PaymentMadeSerializer, CashOutSerializer

class AddVaTimeSheetView(SuccessMessageMixin, LoginRequiredMixin, CreateView):
    template_name = 'timesheet/add_timesheet.html'
    model = TimeSheet
    fields = [
        'company_tagging', 'shift_date', 'month_to_date', 'clients_full_name', 'client_full_name',
        'title_job_request', 'channel_job_requested', 'job_request_description',
        'time_in', 'time_out', 'total_items', 'additional_comments', 'hourly_rate_peso',
        'status', 'assigned_approval'
    ]
    success_message = "Successfully added a timesheet!"

    def form_valid(self, form):
        form.instance.assigned_approval = self.request.user.staffs
        return super().form_valid(form)

    def get_success_url(self):
        return reverse_lazy('timesheet:add_timesheet')

    def get_form(self, form_class=None):
        form = super().get_form(form_class)

        # make the 'client_name' field use a datalist to autocomplete
        form.fields['clients_full_name'].widget.attrs.update({'list':'full_names'})

        return form

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # add 'clients_full_name' to the context to populate the datalist
        context['full_names'] = Clients.objects.all()

        return context

class VaTimeSheetView(TemplateView, LoginRequiredMixin):
    template_name = 'timesheet/va_timesheet.html'

class TimeSheetView(TemplateView, LoginRequiredMixin):
    template_name = 'timesheet/view_timesheet.html'

class TimeSheetMasterView(TemplateView, LoginRequiredMixin):
    template_name = 'timesheet/masterboard.html'

class TimeSheetFilter(FilterSet):
    shift_date__month = NumberFilter(
        field_name='shift_date', lookup_expr='month')
    shift_date__gte = DateFilter(field_name='shift_date', lookup_expr='gte')
    shift_date__lte = DateFilter(field_name='shift_date', lookup_expr='lte')
    assigned_approval__full_name = CharFilter(
        field_name='assigned_approval__full_name', lookup_expr='icontains')
    clients_full_name = CharFilter(
        field_name='clients_full_name__full_name', lookup_expr='icontains')

    class Meta:
        model = TimeSheet
        fields = (
            'shift_date__month',
            'assigned_approval__full_name',
            'clients_full_name',
            'shift_date__gte',
            'shift_date__lte',
        )

class PaymentMadeFilter(FilterSet):
    date__month = NumberFilter(field_name='date', lookup_expr='month')
    name = CharFilter(field_name='client_name__full_name',
                      lookup_expr='icontains')

    class Meta:
        model = PaymentMade
        fields = ('date__month', 'name')

class CashOutFilter(FilterSet):
    cash_date_release__month = NumberFilter(
        field_name='cash_date_release', lookup_expr='month')
    cash_date__gte = DateFilter(field_name='cash_date_release', lookup_expr='gte')
    cash_date__lte = DateFilter(field_name='cash_date_release', lookup_expr='lte')
    name = CharFilter(field_name='name__full_name', lookup_expr='icontains')

    class Meta:
        model = CashOut
        fields = ('cash_date_release__month', 'cash_date__gte',
                  'cash_date__lte', 'name')

class TimeSheetViewSet(viewsets.ModelViewSet):
    permission_classes = (IsAuthenticated,)
    serializer_class = TimeSheetSerializer
    filter_class = (TimeSheetFilter)

    def get_queryset(self):
        timesheet = TimeSheet.objects.all()
        current_year = datetime.date.today().year
        year = Q(shift_date__year=current_year)

        if self.request.user.is_client:
            client = Q(
                clients_full_name__full_name__icontains=self.request.user.clients.full_name)
            client_name = Q(client_full_name=self.request.user.clients.full_name)
            queryset = timesheet.filter(client | client_name & Q(
                admin_approval="Approved") & Q(status="Approved"))
            return queryset
        elif self.request.user.is_staffs:
            staff = Q(
                assigned_approval__full_name__icontains=self.request.user.staffs)
            qs = timesheet.filter(staff)
            return qs
        elif self.request.user.is_superuser:
            qs = TimeSheet.objects.all()
            return qs

class PaymentMadeViewSet(viewsets.ModelViewSet):
    permission_classes = (IsAuthenticated,)
    serializer_class = PaymentMadeSerializer
    filter_class = (PaymentMadeFilter)

    def get_queryset(self):
        if self.request.user.is_client:
            queryset = PaymentMade.objects.filter(Q(client_name__full_name=self.request.user.clients.full_name))
            return queryset
        elif self.request.user.is_superuser:
            qs = PaymentMade.objects.all()
            return qs

class CashOutViewSet(viewsets.ModelViewSet):
    permission_classes = (IsAuthenticated,)
    serializer_class = CashOutSerializer
    filter_class = (CashOutFilter)

    def get_queryset(self):
        if self.request.user.is_staffs:
            queryset = CashOut.objects.filter(Q(name__full_name=self.request.user.staffs.full_name))
            return queryset
        elif self.request.user.is_superuser:
            qs = CashOut.objects.all()
            return qs

def client_company_name(request):
    client_name = Clients.objects.all()
    return render(request, 'timesheet/add_timesheet.html',  {"client_names": client_name})


from django.shortcuts import render, get_object_or_404, redirect
from .models import Loans, Clients
# from .forms import LoansForm
from django.contrib.auth.decorators import login_required
# import datetime
from datetime import datetime, timedelta
from .load_json import ConfigData
from kredyty.validators.loan_validation_hour import LoanValidationHour
from kredyty.validators.check_loan_validators import CheckLoanValidators
from kredyty.calculators.repayment_amount_calculator import RepaymentAmountCalculator
from django import forms

# REST_FRAMEWORK:
from rest_framework import viewsets
from django.contrib.auth.models import User
from .serializers import UserSerializer, LoansSerializer, ClientsSerializer, LoansSerializerPUT
from rest_framework.response import Response
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import (
    IsAuthenticated, AllowAny, IsAuthenticatedOrReadOnly, IsAdminUser, DjangoModelPermissions)
from rest_framework import status

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()   # co chcemy wybierac z bd
    serializer_class = UserSerializer
    authentication_classes = (TokenAuthentication, )
    permission_classes = (IsAuthenticatedOrReadOnly, )

class LoansViewSet(viewsets.ModelViewSet):
    queryset = Loans.objects.all()
    serializer_class = LoansSerializer
    authentication_classes = (TokenAuthentication, )
    permission_classes = (IsAuthenticated, )

    def create(self, request, *args, **kwargs):
        if CheckLoanValidators().validate() == True:
            loan = request.data
            loan = loan.copy()
            loan['repayment_amount'] = RepaymentAmountCalculator(int(request.data["amount"]), float(ConfigData().get_data()['interest_rate'])).calculate()
            print(loan)
            serializer = LoansSerializer(data=loan)
            serializer.is_valid(raise_exception=True)    # validuje request
            self.perform_create(serializer)   # zapisuje do bazy
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
        else:
            pass

    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        mod_data = request.data
        mod_data = mod_data.copy()
        mod_data['repayment_amount'] = RepaymentAmountCalculator(int(request.data["amount"]), float(ConfigData().get_data()['interest_rate'])).calculate()
        serializer = LoansSerializerPUT(instance, data=mod_data, partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        return Response(serializer.data)

class ClientsViewSet(viewsets.ModelViewSet):
    queryset = Clients.objects.all()
    serializer_class = ClientsSerializer
    authentication_classes = (TokenAuthentication, )
    permission_classes = (IsAuthenticated, )

# PONIEZEJ FUNKCJE DO PRZEPISANIA

def loan_list(request):
    all_loans = Loans.objects.all()
    return render(request, 'kredyty.html', {'loans': all_loans})

@login_required
def new_loan(request):
    form = LoansForm(request.POST or None, request.FILES or None)
    form.fields['interest_rate'].widget = forms.HiddenInput()
    form.fields['repayment_amount'].widget = forms.HiddenInput()

    if form.is_valid():
        post = form.save(commit=False)
        repayment_amount = int(form['amount'].value()) * int(form['interest_rate'].value()) / 100 + int(form['amount'].value())
        post.repayment_amount = repayment_amount
        post.save
        form.save()
        return redirect(loan_list)

    if CheckLoanValidators().validate() == True:
        # return render(request, 'loan_form.html', {'form': form})
        return render(request, 'loan_form.html', {'form': form, 'interest_rate': ConfigData().get_data()['interest_rate']})
    else:
        return render(request, 'wrong_hour.html')

@login_required
def edit_loan(request, id):
    loan = get_object_or_404(Loans, pk=id)
    form = LoansForm(request.POST or None, request.FILES or None,
                     instance=loan)

    if form.is_valid():
        form.save()
        return redirect(loan_list)
    return render(request, 'loan_form.html', {'form': form})

@login_required
def delete_loan(request, id):
    loan = get_object_or_404(Loans, pk=id)

    if request.method == "POST":
        loan.delete()
        return redirect(loan_list)

    return render(request, 'confirm.html', {'loan': loan})


import function_definition
# first_num = int(input("Please enter First number:"))
# second_num = int(input("Please enter Second number:"))

# print("The Addition of the numbers is ", function_definition.my_addition(first_num,second_num) )

# print("First number squared is  ", int(function_definition.my_square(first_num)))

# print("First number raised to number second number is  ", int(function_definition.my_exponentiation(first_num,second_num)))
# string1 = input("enter string")
# print("upper case of string is ", (function_definition.my_string(string1)))

osalary = int(input("enter exiting salary"))
percentage = int(input("enter percentage "))
print ("raised salary is",function_definition.salary_raised(osalary,percentage))


def inorderTraversal(self, root):
    """
    :type root: TreeNode
    :rtype: List[int]
    """
    res = []
    stack = []
    while root:
    	stack.append(root)
    	root = root.left

    while stack:
    	tmp = stack.pop()
    	res.append(tmp.val)
    	if tmp.right:
    		t = tmp.right
    		#add all left
    		while t:
    			stack.append(t)
    			t = t.left
    return res

tree/EricD/94. Binary Tree Inorder Traversal - EricD.py


import os

from dotenv import load_dotenv

from controller import get_non_empty_matches
from mailer import send_mail
from movies_to_search import get_movies_to_search
from programs_fetcher import fetch_programs

def execute():
    load_dotenv()
    movies_to_search = get_movies_to_search()
    print(f"Trying to search movies: {movies_to_search}")
    programs = fetch_programs()
    matches_dict = get_non_empty_matches(movies_to_search, programs)
    if len(matches_dict) > 0:
        print(f"New movies found! Sending them by email. Detail: {matches_dict}")
        send_email_for_movies(matches_dict)
    else:
        print("No movie found on TV.")

def send_email_for_movies(matches):
    text = get_email_body(matches)
    send_mail(text, "[MoviesOnTvBot] New movies found on TV!", os.getenv("EMAIL_RECIPIENT"))

def get_email_body(matches):
    text = "Here is the list of movies found:"
    for movie, programmes in matches.items():
        text += f"\nMovie '{movie}':\n"
        for prog in programmes:
            text += f"{prog}\n"
    text += "\n Don't forget to update your movie list. Bye bye :)"
    return text

if __name__ == "__main__":
    execute()


import heapq
def solution(jobs):
    t = sorted(jobs, key=lambda x:x[0], reverse=True)
    k = t.pop()
    arr = [(k[1], k[0])]
    answer = 0
    time = arr[0][1]
    while True:
        if t == [] and arr == []:
            return answer//len(jobs)
        while t:
            if t[-1][0] <= time:
                m = t.pop()
                heapq.heappush(arr, (m[-1], m[0]))
            else:
                break
        if arr:
            n = heapq.heappop(arr)
            answer += (time-n[1])+n[0]
            time += n[0]
        else:
            time += 1
    return answer//len(jobs)


from setuptools import setup

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name='db-handler',
    version='0.0.1',
    packages=['db_handler'],
    url='https://realmatch.visualstudio.com/Algo%20-%20Pandologic/_git/db-handler',
    license='MIT License',
    author='Pandologic',
    author_email='ebrill@pandologic.com',
    description='Python integration with multiple databases types',
    long_description=long_description,
    long_description_content_type="text/markdown",
    classifiers=[
            "Programming Language :: Python :: 3",
            "License :: MIT License",
            "Operating System :: OS Independent",
        ],
    python_requires='>=3.6',
    install_requires=[
                        'pandas==1.0.3',
                        'pyodbc==4.0.30'
                    ]
)


import operator

from django.db import models
from django.utils.translation import gettext_lazy as _

class GeneratorQuerySet(models.QuerySet):
    def active(self):
        return self.filter(is_active=True)

    def as_generators(self):
        return [g.as_generator() for g in self.prefetch_related("conditions")]

class Generator(models.Model):
    SOURCES = {}

    is_active = models.BooleanField(_("is active"), default=True)
    group = models.CharField(_("group"), max_length=50)
    source = models.CharField(_("source"), max_length=50)

    objects = GeneratorQuerySet.as_manager()

    class Meta:
        verbose_name = _("generator")
        verbose_name_plural = _("generators")

    def __str__(self):
        return self.group

    def as_generator(self):
        source = self.SOURCES[self.source]
        return {
            "group": self.group,
            "conditions": [
                condition.as_condition() for condition in self.conditions.all()
            ],
            **source,
        }

class Condition(models.Model):
    OPERATORS = [
        (">", operator.gt),
        ("<", operator.lt),
        (">=", operator.ge),
        ("<=", operator.le),
        ("=", operator.eq),
        ("!=", operator.ne),
    ]

    generator = models.ForeignKey(
        Generator,
        on_delete=models.CASCADE,
        related_name="conditions",
        verbose_name=_("generator"),
    )
    variable = models.CharField(_("variable"), max_length=50)
    operator = models.CharField(
        _("operator"), max_length=10, choices=[(d, d) for d, op in OPERATORS]
    )
    value = models.IntegerField(_("value"))

    class Meta:
        ordering = ["pk"]
        verbose_name = _("condition")
        verbose_name_plural = _("conditions")

    def __str__(self):
        return "{o.variable} {o.operator} {o.value}".format(o=self)

    def as_condition(self):
        op = dict(self.OPERATORS)[self.operator]
        return {"variable": self.variable, "test": lambda v: op(v, self.value)}


import os
import sys
import warnings
from argparse import ArgumentParser
from functools import partial
from glob import glob
from multiprocessing import Pool
from typing import List, Callable, Tuple, Union

import cv2
import numpy as np
import pandas as pd
from scipy.stats import kurtosis, skew

warnings.filterwarnings("ignore")

EXTERNAL_UTILS_LIB = "../alaska_utils"
sys.path.append(EXTERNAL_UTILS_LIB)

from alaska_utils import safe_mkdir
from alaska_utils import configure_arguments
from alaska_utils import parse_image_to_dir_basename

def process_image(image_file_path: str, functions: Tuple[str, Callable], ) -> Union[pd.Series, pd.DataFrame]:
    image = cv2.imread(image_file_path, cv2.IMREAD_COLOR)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)
    tmp = {name: [func(image[:, :, i].flatten()) for i in range(image.shape[-1])] for name, func in functions}
    for name, func in functions:
        tmp[name].append(func(image.flatten()))
    tmp = pd.DataFrame(tmp).stack().swaplevel()
    return tmp

def main(args: ArgumentParser):
    for dir_path in [args.output_dir, args.cached_dir, args.model_dir]:
        safe_mkdir(dir_path)

    args = configure_arguments(args)
    file_path: str = args.file_path_images_stats
    if os.path.exists(file_path) and not args.refresh:
        print(f"{file_path} exists, skip generates meta info")
        return False

    # process
    list_of_functions: List[Tuple[str, Callable]] = [
        ("mean", np.mean), ("std", np.std), ("kurt", kurtosis), ("skew", skew), ]
    # file path
    list_all_images: List[str] = list(glob(os.path.join(args.data_dir, "*", "*.jpg")))
    if args.debug:
        list_all_images = list_all_images[:100]

    with Pool(processes=args.n_jobs) as p:
        func = partial(process_image, functions=list_of_functions)
        df = pd.concat(list(p.map(func, list_all_images)), axis=1).astype(np.float32).T
        df.columns = [f"{i}_{m}" for m, i in df.columns]

    df_train = parse_image_to_dir_basename(args, list_all_images, column="file_path")

    # compose return dataframe
    image, kind = args.shared_indices
    df[image] = df_train[image].tolist()
    df[kind] = df_train[kind].tolist()
    df.sort_values(image, inplace=True)
    df.set_index(args.shared_indices, inplace=True)

    df.to_parquet(file_path)
    print(f"Save stats to: {file_path}\n{df.describe().T}")
    return

if "__main__" == __name__:
    #
    default_output_dir: str = "../input/alaska2-image-steganalysis-output/"
    default_cached_dir: str = "../input/alaska2-image-steganalysis-cached-data/"
    default_meta_dir: str = "../input/alaska2-image-steganalysis-image-quality/"
    default_model_dir: str = "../input/alaska2-image-steganalysis-models/"
    default_data_dir: str = "../input/alaska2-image-steganalysis/"
    #
    default_n_jobs: int = 8
    default_init_seed: int = 42

    parser = ArgumentParser()
    parser.add_argument("--output-dir", type=str, default=default_output_dir, help="folder for output")
    parser.add_argument("--cached-dir", type=str, default=default_cached_dir, help="folder for cached data")
    parser.add_argument("--meta-dir", type=str, default=default_meta_dir, help="folder for meta data")
    parser.add_argument("--model-dir", type=str, default=default_model_dir, help="folder for models")
    parser.add_argument("--data-dir", type=str, default=default_data_dir, help="folder for data")
    #
    parser.add_argument("--refresh", action="store_true", default=False, help="refresh cached data")
    parser.add_argument("--n-jobs", type=int, default=default_n_jobs, help="num worker")
    # debug
    parser.add_argument("--debug", action="store_true", default=False, help="debug")
    args = parser.parse_args()

    # start program
    main(args)


S = input()
K = int(input())

nY = S.count('Y')

A = []
streak = None
for c in S:
  if c == 'Y':
    if streak is None:
      streak = 0
    else:
      A.append(streak)
      streak = 0

  elif streak is not None:
    streak += 1

A += [1 << 40] * (len(S) + 10)

B = [0]
for a in A:
  B.append(B[-1] + a)

if nY <= 1:
  print(nY)
  exit()

def check(X):
  v = 0
  for i in range(X - 1):
    v += min(i + 1, X - 1 - i) * A[i]

  retval = v
  l = X // 2
  for i in range(nY):
    v += B[X + i] - B[X + i - l]
    v -= B[i + l] - B[i]
    retval = min(retval, v)

  return retval <= K

ok = 1
ng = nY + 1

while ng - ok > 1:
  tgt = (ok + ng) // 2

  if check(tgt):
    ok = tgt
  else:
    ng = tgt

print(ok)


'''
业务: 开3个线程按照顺序打印ABC 10次
方案: 用semaphore
'''

import logging
import time
from threading import Semaphore, Thread, current_thread
from typing import List

import requests

session = requests.Session()

def req1(list: List, sem: Semaphore):
    while len(list) > 0:
        with sem:
            if len(list) <= 0:
                break
            session.get(f'https://deelay.me/100/http://httpbin.org/get?a=1')
            logging.info(f'{current_thread().name}-{list.pop(0)}')
        time.sleep(0.000000000000000001)

def main():
    sem = Semaphore(1)
    list = ['A', 'B', 'C'] * 10
    tasks = [Thread(target=req1, args=(list, sem)) for i in range(3)]
    for i in tasks:
        i.start()
    for i in tasks:
        i.join()

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    main()


from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
	# Core
	url('', include('core.urls')),

	# Pronostic
	url('^pronostic/', include('pronostic.urls')),

    # Allauth
    url(r'^accounts/', include('allauth.urls')),

    # Admin
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += patterns('',
	(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
	'document_root': settings.MEDIA_ROOT}))


#potegowanie modulo

# x = 59**3*61**4*2*3*5*7*3*5*7
# y = x**2562142
# print(y)

# 1 liczba A mod N = ab
# B mod N = bi
# AB mod N = ?..

# A = A1*N + ab
# B = B1*N + bi
# AB = A1*B1*n2 + a1N*b + b1N*a + a*bina*B mod = (a*b ) mod N
# 123456*78332 mod10 = 6*2 mod 10 = 2

# funkcja of mod simple(a,b,n)

# funkcja mod fast
# a^b mod n

def mod_simple(a, b, n):
    r = 1
    for x in range(b):
        #print(x)
        #print(r)
        r = r * a % n

        # print(result)
        # print("")

    print(r)

mod_imple(3, 9, 10)

# def print_mod(a, b, n):
#     lista = mod_imple(a, b, n)

#     for x in lista:
#         print(x)

# print_mod(256, 40, 100)


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

from bs4 import BeautifulSoup
import numpy as np
import pandas as pd
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline
import string
from nltk.stem import PorterStemmer
import re
import spacy
import nltk
from io import StringIO
from nltk.tokenize import word_tokenize

ps = PorterStemmer()

#Stemming Function
def stemming(review):
    stem_review = [ps.stem(word) for word in review]
    return ' '.join(stem_review)

#Fuction to remove HTML
def removeHtml(review):
    review = re.compile(r'<[^>]+>').sub('', review)
    return review.lower().strip()

#Function to remove punctuation
def remove_punctuation(review):
    punctuationfree="".join([i for i in review if i not in string.punctuation])
    return punctuationfree

#Tokenizer
def tokenization(review):
    #tokens = re.split('W+',text)
    tokens = word_tokenize(review)
    tokens = [word for word in tokens if not word in stop_words]
    return tokens

#Finds similarities between training vectors and test vector
def findSimilarities(train_vect, test_vect):
    cosineSimilarities = np.dot(test_vect, np.transpose(train_vect)) #dot product of the 2 vectors
    similarities = cosineSimilarities.toarray()
    return similarities

def findNeighbors(similarities, k):
    return np.argsort(-similarities)[:k]

def predict(nearestNeighbors, sentiments):
    """Calculates the count of positive and negative reviews from similarity vector.
        If positive reviews are more, then the test review is positive and vice-versa"""
    #print(labels)

    positiveReviews = 0
    negativeReviews = 0
    for neighbor in nearestNeighbors:
        #print(neighbor)
        if int(sentiments[neighbor]) == 1:
            positiveReviews += 1
        else:
            negativeReviews += 1
    if positiveReviews > negativeReviews:
        return 1
    else:
        return -1

nlp = spacy.load('en_core_web_sm')

#Set the stopwords
stop_words = set(nltk.corpus.stopwords.words("english"))

def remove_stopwords(review):
    output= [i for i in review if i not in stop_words]
    return output

#Read in training file
train_data = pd.read_csv('./train_file.txt', sep='\t', header = None, skiprows=[0])
t = open("./test_file.txt", "r")
test_data = t.readlines()
test_data = pd.DataFrame(test_data)
print("Reading in data...\n")

#Set the column headers
column_names = ['sentiment', 'review']
train_data.columns = column_names
test_data.columns = ['review']
print("Preprocessing training and testing files...\n")

#Remove HTML
train_data['review'] = train_data['review'].apply(lambda x:removeHtml(x))
test_data['review'] = test_data['review'].apply(lambda x:removeHtml(x))

#Remove puntuation
train_data['review'] = train_data['review'].apply(lambda x:remove_punctuation(x))
test_data['review'] = test_data['review'].apply(lambda x:remove_punctuation(x))

#Tokenization
train_data['review'] = train_data['review'].apply(lambda x:tokenization(x))
test_data['review'] = test_data['review'].apply(lambda x:tokenization(x))

#Stemming
train_data['review'] = train_data['review'].apply(lambda x:stemming(x))
test_data['review'] = test_data['review'].apply(lambda x:stemming(x))

#set
y = train_data['sentiment']
x = train_data['review']
test_reviews = test_data['review']

#split data into training and testing sets
#from sklearn.model_selection import train_test_split

#x_train, x_test, y_train, y_test = train_test_split(x, y,  test_size = 0.25, random_state = 0)
#print("Train: ", x_train.shape, y_train.shape, "Test: ", (x_test.shape, y_test.shape))

print("Creating TF-IDF vectors...\n")
#Vectorization
vectorizer = TfidfVectorizer()
tf_x_train = vectorizer.fit_transform(x)
tf_x_test = vectorizer.transform(test_reviews)

#print(tf_x_train, tf_x_test)
#print(vectorizer.idf_)
print("Creating similarity model...\n")

#Find Similarities
similarities = findSimilarities(tf_x_train, tf_x_test)

k = 250
test_sentiments = list()

print("Predicting Sentiments...\n")
for similarity in similarities:
    knn = findNeighbors(similarity, k)
    prediction = predict(knn, y)

    #To write to the list as +1 instead of just a 1 for positive reviews
    if prediction == 1:
        test_sentiments.append('1')
    else:
        test_sentiments.append('-1')

#Write the result to a txt file
output = open('format.txt', 'w')

output.writelines( "%s\n" % item for item in test_sentiments )

output.close()
print("Predictions complete")


#!/usr/bin/env python
'''
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

Solution:
This question is similar as [Largest Rectangle in Histogram]:

You can maintain a row length of Integer array H recorded its height of '1's, and scan and update row by row to find out the largest rectangle of each row.

For each row, if matrix[row][i] == '1'. H[i] +=1, or reset the H[i] to zero. and according the algorithm of [Largest Rectangle in Histogram], to update the maximum area.
'''
def maximalRectangle(matrix):
    if not matrix:
        return 0

    nrows = len(matrix)
    ncols = len(matrix[0])

    max_area = 0

    height = [0] * (ncols + 1) # array length is ncols + 1 since we have to add a '0' in the end, similar to Largest Rectangle in Histogram

    for i in range(nrows):
        stack = []
        for j in range(ncols + 1):
            if j < ncols:
                if matrix[i][j] == '1':
                    height[j] += 1

                else:
                    height[j] = 0

            # same logic as Largest Rectangle in Histogram
            if not stack or height[j] >= height[stack[-1]]:
                stack.append(j)

            else:
                while stack and height[stack[-1]] > height[j]:
                    top = stack.pop()
                    width = j if not stack else j -1 - stack[-1]
                    max_area = max(max_area, height[top] * width)

                stack.append(j)

    return max_area


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        ('idc', '0001_initial'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Backup',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True)),
                ('name', models.CharField(max_length=64, verbose_name='Name')),
                ('backup_type', models.IntegerField(default=1, verbose_name='Back Type', choices=[(1, 'BACKUP_TYPE_FULL'), (2, 'BACKUP_TYPE_INCREMENT')])),
                ('volumes', models.CharField(default=None, max_length=255, null=True, verbose_name='Volumes', blank=True)),
                ('instance', models.IntegerField(default=None, null=True, verbose_name='Instance', blank=True)),
                ('create_date', models.DateTimeField(auto_now_add=True, verbose_name='Create Date')),
                ('delete_date', models.DateTimeField(auto_now=True, verbose_name='Delete Date')),
                ('deleted', models.BooleanField(default=False, verbose_name='Deleted')),
                ('status', models.IntegerField(default=0, verbose_name='Status', choices=[(0, 'Backup Waiting'), (1, 'Backup Available'), (2, 'Backup Error'), (9, 'Backup Deleted'), (10, 'Backup Backuping'), (11, 'Backup Restoring'), (20, 'Backup Pending Delete'), (21, 'Backup Pending Restore'), (19, 'Backup Deleting')])),
                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
                ('user_data_center', models.ForeignKey(to='idc.UserDataCenter')),
            ],
            options={
                'ordering': ['-create_date'],
                'db_table': 'backup',
                'verbose_name': 'Backup',
                'verbose_name_plural': 'Backup',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='BackupItem',
            fields=[
                ('id', models.AutoField(serialize=False, primary_key=True)),
                ('resource_id', models.IntegerField(default=0, null=True, verbose_name='Resource ID', blank=True)),
                ('resource_uuid', models.CharField(max_length=64, null=True, verbose_name='Backend UUID', blank=True)),
                ('resource_type', models.CharField(max_length=64, null=True, verbose_name='Resource', blank=True)),
                ('resource_name', models.CharField(max_length=64, null=True, verbose_name='Resource Name', blank=True)),
                ('resource_size', models.IntegerField(default=0, null=True, verbose_name='Size(GB)', blank=True)),
                ('rbd_image', models.CharField(default=None, max_length=256, null=True, verbose_name='RBD Image', blank=True)),
                ('create_date', models.DateTimeField(auto_now_add=True, verbose_name='Create Date')),
                ('delete_date', models.DateTimeField(auto_now=True, verbose_name='Delete Date')),
                ('deleted', models.BooleanField(default=False, verbose_name='Deleted')),
                ('status', models.IntegerField(default=0, verbose_name='Status', choices=[(0, 'Backup Waiting'), (1, 'Backup Available'), (2, 'Backup Error'), (9, 'Backup Deleted'), (10, 'Backup Backuping'), (11, 'Backup Restoring'), (20, 'Backup Pending Delete'), (21, 'Backup Pending Restore'), (19, 'Backup Deleting')])),
                ('backup', models.ForeignKey(related_name='items', to='backup.Backup')),
                ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
                ('user_data_center', models.ForeignKey(to='idc.UserDataCenter')),
            ],
            options={
                'ordering': ['-create_date'],
                'db_table': 'backup_item',
                'verbose_name': 'Backup Item',
                'verbose_name_plural': 'Backup Item',
            },
            bases=(models.Model,),
        ),
    ]