#!/usr/bin/env python

import sys
import importlib

class C: pass

class_type = type(C)

TO_STRIP = "gigamonkeys.montecarlo"

def load_classes(modules):
    classes = []

    for module in modules:
        module = importlib.import_module(module)

        for attr in dir(module):
            what = getattr(module, attr)
            if type(what) == class_type:
                classes.append(what)

    return classes

def name(c):
    module = c.__module__
    name = c.__name__

    if module == TO_STRIP:
        return f'"{name}"'
    else:
        if module.startswith(TO_STRIP + "."):
            module = module[len(TO_STRIP + ".") :]
        return f'"{module}.{name}"'

def graph(classes, pathfn):

    print("digraph G {")
    print('  rankdir="BT";')
    print('  concentrate="true";')
    for c in classes:
        path = pathfn(c)
        print("  " + " -> ".join(name(p) for p in path) + ";")
    print("}")

if __name__ == "__main__":

    classes = load_classes([
        "gigamonkeys.montecarlo",
        "gigamonkeys.montecarlo.calendar",
        "gigamonkeys.montecarlo.deadline",
    ])

    if sys.argv[1] == "super":
        graph(classes, lambda c: (c,) + c.__bases__)
    elif sys.argv[1] == "mro":
        graph(classes, lambda c: c.__mro__)
    else:
        exit(f"Unrecognized output: {sys.argv[1]}")


from typing import List, Dict

class Disk:
    def __init__(self,
                 displacement_time: float,
                 start_cylinder: int
                 ) -> None:
        self._displacement_time: float = displacement_time
        self._actual_cylinder: int = start_cylinder

    def fcfs(self, required_cylinders: List[int]) -> float:
        total_displacement: int = 0
        for cylinder in required_cylinders:
            total_displacement += abs(self._actual_cylinder - cylinder)
            self._actual_cylinder = cylinder

        return (total_displacement * self._displacement_time)

    def elevator(self, required_cylinders: List[int], ascending: bool = True) -> float:
        total_displacement: int = 0
        biggest_cylinder: int = max(required_cylinders)
        smallest_cylinder: int = min(required_cylinders)
        if ascending:
            total_displacement += biggest_cylinder - self._actual_cylinder
            self._actual_cylinder = smallest_cylinder
        else:
            total_displacement += self._actual_cylinder - smallest_cylinder
            self._actual_cylinder = biggest_cylinder

        total_displacement += biggest_cylinder - smallest_cylinder

        # required_cylinders.sort()
        # for i in range(len(required_cylinders)):
        #     if (required_cylinders[i] >= self._actual_cylinder):
        #         greater_cylinders: List[int] = required_cylinders[i:]
        #         less_cylinders: List[int] = sorted(required_cylinders[:i], reverse=True)
        #         if ascending:
        #             required_cylinders = greater_cylinders + less_cylinders
        #         else:
        #             required_cylinders = less_cylinders + greater_cylinders
        #         break
        #
        # for cylinder in required_cylinders:
        #     total_displacement += abs(self._actual_cylinder - cylinder)
        #     self._actual_cylinder = cylinder

        return (total_displacement * self._displacement_time)

    def ssf(self, required_cylinders: List[int]) -> float:
        total_displacement: int = 0
        while required_cylinders:
            # key = distance, value = cylinder
            distance_between_cylinders: Dict[int, int] = {abs(self._actual_cylinder - cylinder): cylinder
                                                          for cylinder in required_cylinders}
            shortest: int = min(distance_between_cylinders.keys())
            cylinder = distance_between_cylinders[shortest]
            required_cylinders.remove(cylinder)
            total_displacement += shortest
            self._actual_cylinder = cylinder

        return (total_displacement * self._displacement_time)

a = Disk(displacement_time=40, start_cylinder=20)
print(a.fcfs([10, 22, 20, 2, 40, 6, 38]))

b = Disk(displacement_time=31, start_cylinder=20)
print(b.elevator([10, 22, 20, 2, 40, 6, 38]))

c = Disk(displacement_time=25, start_cylinder=20)
print(c.ssf([10, 22, 20, 2, 40, 6, 38]))


import boto3
import json
import os
import random
import base64
import requests

s3_client = boto3.client('s3')
db_client = boto3.client('dynamodb')

idx2tid_data = (s3_client.get_object(Bucket='model-data-proj', Key='idx2tid.json')['Body']).read().decode('utf-8')
idx2tid = json.loads(idx2tid_data)

TOTAL_TRACK = int(os.environ['total_track']) # 22480
client_creds = os.environ['client_creds'] # <clientid:clientsecret> from Spotify Application

def get_token():
    client_creds_64 = base64.b64encode(client_creds.encode())
    token_data = {
        'grant_type': 'client_credentials'
    }
    headers = {
        'Authorization': f'Basic {client_creds_64.decode()}',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    res = requests.post('https://accounts.spotify.com/api/token', data=token_data, headers=headers).json()
    return res['access_token']

def get_info(trackids):
    api_key = get_token()
    print(api_key)
    url = "https://api.spotify.com/v1/tracks"
    url_params = {
        "ids": ",".join(trackids)
    }
    headers = {
        'Authorization': 'Bearer %s' % api_key,
        'Accept': 'applicatioin/json',
        'Content-Type': 'application/json'
    }
    response = requests.request('GET', url, headers=headers, params=url_params)
    tracks = response.json()["tracks"]
    res = []
    for track in tracks:
        musicUrl = track["preview_url"]
        if musicUrl is not None:
            tid = track["id"]
            if len(track["album"]["images"]) >= 0:
                imageUrl = track["album"]["images"][0]["url"]
            else:
                imageUrl = "https://scontent.fewr1-5.fna.fbcdn.net/v/t1.18169-1/17884567_10154570340077496_8996447567747887405_n.png?stp=dst-png_p148x148&_nc_cat=1&ccb=1-6&_nc_sid=1eb0c7&_nc_ohc=sb6jRnk6Ep4AX9IemqY&_nc_ht=scontent.fewr1-5.fna&oh=00_AT91ys0sCigXG90rAF2_y0PYp8CPc7wRuuokXyl71zEVDQ&oe=6298BB11"
            item = {
                "musicId": tid,
                "musicName": track["name"],
                "artistName" : track["artists"][0]["name"],
                "imageUrl" : imageUrl,
                "musicUrl": musicUrl,
            }
            res.append(item)
    return res

def gen_candidate():
    candidates = random.sample(range(0, TOTAL_TRACK), 40)
    candidates_tids = [idx2tid[str(idx)] for idx in candidates]
    info = get_info(candidates_tids)
    return info

def get_current_likelist(userId):
    response = db_client.get_item(
        Key={
            "userId": {
                "S": userId
            }
        },
        TableName='user-like'
    )
    if "Item" in response:
        like_list = (response["Item"]["likelist"]["S"]).replace(" ", "").split(",")
    else:
        like_list = []
    return like_list

def update_user_like(userId, body):
    original_like_list = get_current_likelist(userId)

    new_like_list = []
    musics = body["music"]
    for music in musics:
        if music["like"] == 1:
            new_like_list.append(music["musicId"])
        elif music["musicId"] in original_like_list:
            original_like_list.remove(music["musicId"])
    if original_like_list == [""]:
        like_list = new_like_list
    else:
        like_list = original_like_list + new_like_list

    mylist = list(dict.fromkeys(like_list))
    like_tids = ",".join(mylist)
    item_dict = {
            "userId": {
                "S": userId
            },
            "likelist": {
                "S": like_tids
            }
        }
    db_response = db_client.put_item(Item=item_dict, TableName='user-like')
    return item_dict["likelist"]["S"]

def lambda_handler(event, context):
    headers = {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
        "Access-Control-Allow-Methods": "GET,OPTIONS,POST,PUT"
    }

    statusCode = 200
    if event["httpMethod"] == "GET":

        userId = event["pathParameters"]["userId"]

        musics = gen_candidate()

        results = {
            "count": len(musics),
            "music": musics,
            "message": "fetch test interest successfully"
        }

    elif event["httpMethod"] == "POST":
        userId = event["pathParameters"]["userId"]
        body = json.loads(event["body"])
        # body = event["body"]

        new_like = update_user_like(userId, body)

        # body example, 1 means like, 0 normal, -1 dislike
        # {
        #     "count":5,
        #     "music":[
        #         {
        #             "musicId":"xxxx",
        #             "like":1
        #         },
        #         {
        #             "musicId":"xxxx",
        #             "like":-1
        #         },
        #         {
        #             "musicId":"xxxx",
        #             "like":0
        #         }
        #     ]
        # }

        results = {
            "message": "send interest data successfully"
        }

    return {
        'statusCode': statusCode,
        'headers': headers,
        'body': json.dumps(results)
    }


import requests
from bs4 import BeautifulSoup
from datetime import date
from multiprocessing import Pool
import smtplib
from email.message import EmailMessage
import os

URL = 'https://egov.uscis.gov/casestatus/mycasestatus.do'
# Important Notice: please limit your range size to 1000 receipts,
# larger number may cause uscis block your ip.
RECEIPT_NUM_RANGE = range(1990227366, 1990527366, 300)

def MakeRequest(case_id):
    return requests.post(URL, data = {'appReceiptNum': case_id})

# return [case_status, case_detail]
def GetStatusContentFromHtml(raw_html):
    soup = BeautifulSoup(raw_html, 'html.parser')
    return [str(soup.findAll('h1')[0]).lstrip('<h1>').rstrip('</h1>'), str(soup.findAll('p')[0])]

def GetChangeDateFromCaseDetail(case_detail):
    year = int(case_detail.split(',')[1].strip())

    month_day = case_detail.split(',')[0][6:].split(' ')
    month = ParseMonth(month_day[0])
    day = int(month_day[1])

    return date(year, month, day)

def GetCaseNumberFromCaseDetail(case_detail):
    start = case_detail.find('YSC')
    return case_detail[start:(start+13)]

def sendMail(to, fro, subject, text, files=[],server="localhost"):
    msg = EmailMessage()
    msg['From'] = fro
    msg['To'] = ', '.join(to)
    msg['Subject'] = subject
    smtp = smtplib.SMTP(server)
    smtp.send_message(msg)
    smtp.quit()

def ParseMonth(word):
    word = word.lower()
    if word == 'january' or word == 'jan':
        return 1
    elif word == 'february' or word == 'feb':
        return 2
    elif word == 'march' or word == 'mar':
        return 3
    elif word == 'april' or word == 'apr':
        return 4
    elif word == 'may':
        return 5
    elif word == 'june' or word == 'jun':
        return 6
    elif word == 'july' or word == 'jul':
        return 7
    elif word == 'august' or word == 'aug':
        return 8
    elif word == 'september' or word == 'sep':
        return 9
    elif word == 'october' or word == 'oct':
        return 10
    elif word == 'november' or word == 'nov':
        return 11
    elif word == 'december' or word == 'dec':
        return 12
    else:
        return 0

def CompareStatus(case):
    file_name = 'case_status_' + case
    [case_number, case_status, change_date] = GetCaseStatus(case)
    with open(file_name, 'r+') as myfile:
        data = myfile.read()
        if case_status == data:
            print("no change")
            return
        else:
            myfile.seek(0)
            myfile.write(case_status)
            myfile.truncate()
            sendMail(['ulimartinez@webserver'], ['ulimartinez96@gmail.com'], "uscis status", "case has changed to: " + case_status)

def GetCaseStatus(receipt):
    raw_html = MakeRequest(receipt).text
    [case_status, case_detail] = GetStatusContentFromHtml(raw_html)
    change_date = GetChangeDateFromCaseDetail(case_detail)
    case_number = GetCaseNumberFromCaseDetail(case_detail)
    return [case_number, case_status, change_date]

def main():
    CompareStatus('YSC1990377366')

if __name__ == "__main__":
    main()


# coding: utf-8
import sys, os
import numpy as np
from collections import OrderedDict
from ReluLayer import Relu
from SigmoidLayer import Sigmoid
from AffineLayer import Affine
from SoftmaxLayer import SoftmaxWithLoss

def numerical_gradient(f, x):
    h = 1e-4 # 0.0001
    grad = np.zeros_like(x)

    it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
    while not it.finished:
        idx = it.multi_index
        tmp_val = x[idx]
        x[idx] = float(tmp_val) + h
        fxh1 = f(x) # f(x+h)

        x[idx] = tmp_val - h
        fxh2 = f(x) # f(x-h)
        grad[idx] = (fxh1 - fxh2) / (2*h)

        x[idx] = tmp_val
        it.iternext()

    return grad

class TwoLayerNet:

    def __init__(self, input_size, hidden_size, output_size, weight_init_std = 0.01):
        self.params = {}
        self.params['W1'] = weight_init_std * np.random.randn(input_size, hidden_size)
        self.params['b1'] = np.zeros(hidden_size)
        self.params['W2'] = weight_init_std * np.random.randn(hidden_size, output_size)
        self.params['b2'] = np.zeros(output_size)

        self.layers = OrderedDict()
        #hidden layer1
        self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1'])
        #hidden layer1 activation
        self.layers['Relu1'] = Relu()
        #hidden layer2
        self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2'])
        #hidden layer2 activation
        self.lastLayer = SoftmaxWithLoss()

    def predict(self, x):
        for layer in self.layers.values():
            x = layer.forward(x)
        return x

    def loss(self, x, t):
        y = self.predict(x)
        return self.lastLayer.forward(y, t)

    def accuracy(self, x, t):
        y = self.predict(x)
        y = np.argmax(y, axis=1)
        if t.ndim != 1 : t = np.argmax(t, axis=1)
        accuracy = np.sum(y == t) / float(x.shape[0])
        return accuracy

    def numerical_gradient(self, x, t):
        # un-named function
        loss_W = lambda W: self.loss(x, t)
        grads = {}
        grads['W1'] = numerical_gradient(loss_W, self.params['W1'])
        grads['b1'] = numerical_gradient(loss_W, self.params['b1'])
        grads['W2'] = numerical_gradient(loss_W, self.params['W2'])
        grads['b2'] = numerical_gradient(loss_W, self.params['b2'])
        return grads

    def gradient(self, x, t):
        # forward
        self.loss(x, t)
        # backward
        dout = 1
        dout = self.lastLayer.backward(dout)
        layers = list(self.layers.values())
        layers.reverse()
        for layer in layers:
            dout = layer.backward(dout)
        grads = {}
        grads['W1'], grads['b1'] = self.layers['Affine1'].dW, self.layers['Affine1'].db
        grads['W2'], grads['b2'] = self.layers['Affine2'].dW, self.layers['Affine2'].db
        return grads


"""Sequence-to-sequence model for human motion prediction."""
import random
import torch
from torch import nn

class Seq2SeqModel(nn.Module):
    """Sequence-to-sequence model for human motion prediction"""

    def __init__(self,
                 architecture,
                 rnn_size,
                 num_layers=1,
                 num_joints=21,
                 residual_velocities=False,
                 dropout=0.0,
                 teacher_ratio=0.0):
        """Create the model.
        Args:
          architecture: [basic, tied] whether to tie the encoder and decoder.
          rnn_size: number of units in the rnn.
          num_layers: number of rnns to stack.
          residual_velocities: whether to use a residual connection that models velocities.
        """
        super(Seq2SeqModel, self).__init__()

        self.input_size = num_joints*3
        self.num_joints = num_joints
        # Summary writers for train and test runs
        self.rnn_size = rnn_size
        self.dropout = nn.Dropout(dropout)
        self.teacher_forcing = teacher_ratio
        self.residual_velocities = residual_velocities
        # === Create the RNN that will keep the state ===
        self.encoder = torch.nn.GRU(
            self.input_size, self.rnn_size, batch_first=True, num_layers=num_layers)
        self.decoder = torch.nn.GRU(
            self.input_size, self.rnn_size, num_layers=num_layers, dropout=dropout)
        self.projector = nn.Linear(self.rnn_size*num_layers, self.input_size)

    def forward(self, encoder_inputs, decoder_inputs):
        """
        Args:
            encoder_inputs: batch of dance pose sequences , shape=(batch_size,source_seq_length,num_joints*3)
            decoder_inputs: batch of dance pose sequences , shape=(batch_size,target_seq_length-1,num_joints*3)
        Returns:
            outputs: batch of predicted dance pose sequences, shape=(batch_size,target_seq_length-1,num_joints*3)
        """
        # First calculate the encoder hidden state
        _, encoder_hidden_state = self.encoder(encoder_inputs)
        batch_size = decoder_inputs.size(0)
        outputs = []
        first_decode = True
        next_state = encoder_hidden_state
        # Iterate over decoder inputs
        for inp in decoder_inputs.transpose(0, 1):
            # Perform teacher forcing
            if random.random() < self.teacher_forcing and not first_decode:
                inp = prev_output
            _, next_state = self.decoder(inp.unsqueeze(0), next_state)
            # Apply residual network to help in smooth transition between subsequent poses
            if self.residual_velocities:
                output = inp + self.projector(self.dropout(next_state.view(batch_size, -1)))
            else:
                output = self.projector(self.dropout(next_state))
            # Store the output for Teacher Forcing: use the prediction as
            # the next input instead of feeding the ground truth
            prev_output = output
            outputs.append(output)
            first_decode = False

        outputs = torch.stack(outputs)
        return outputs.transpose(0, 1)

    def generate(self, warmup_input, n_frames):
        with torch.no_grad():
            outputs = []
            batch_size = warmup_input.size(0)
            hidden = torch.zeros(batch_size, self.rnn_size)
            for inp in warmup_input.transpose(0, 1):
                hidden = self.decoder(inp, hidden)

            last_warmup_input = warmup_input.transpose(0, 1)[-1]
            if self.residual_velocities:
                output = last_warmup_input + self.projector(hidden)
            else:
                output = self.projector(hidden)

            outputs.append(output)

            for _ in range(n_frames-1):
                hidden = self.decoder(output, hidden)
                if self.residual_velocities:
                    output = output + self.projector(hidden)
                else:
                    output = self.projector(hidden)
                outputs.append(output)

            outputs = torch.stack(outputs)
            return outputs.transpose(0, 1)


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.impute import SimpleImputer
from sklearn.metrics import mean_squared_error, r2_score

df = pd.read_csv('preprocess/cleaned_dataset.csv')

df['lat'] = df['lat'].str.replace('٫', '.').astype(float)
df['lng'] = df['lng'].str.replace('٫', '.').astype(float)

X = df[['rooms', 'foundation', 'estate_age', 'approve_time', 'lat', 'lng']]
y = df['price']

reference_date = pd.to_datetime('2022-01-01')  # Choose an appropriate reference date
X['approve_time'] = (pd.to_datetime(X['approve_time']) - reference_date).dt.days
print(X)

imputer = SimpleImputer(strategy='mean')
X_imputed = imputer.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_imputed, y, test_size=0.2, random_state=42)

model = LinearRegression()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)

mse = mean_squared_error(y_test, y_pred)
rmse = mse ** 0.5
r2 = r2_score(y_test, y_pred)

print("Mean Squared Error (MSE):", mse)
print("Root Mean Squared Error (RMSE):", rmse)
print("R-squared (R2) Score:", r2)


from django.contrib.auth.models import User
from django.db import models
from datetime import datetime

class BudgetControl(models.Model):
    today_month = datetime.today().month
    months = [
        ('JANEIRO', 'JANEIRO'),
        ('FEVEREIRO', 'FEVEREIRO'),
        ('MARÇO', 'MARÇO'),
        ('ABRIL', 'ABRIL'),
        ('MAIO', 'MAIO'),
        ('JUNHO', 'JUNHO'),
        ('JULHO', 'JULHO'),
        ('AGOSTO', 'AGOSTO'),
        ('SETEMBRO', 'SETEMBRO'),
        ('OUTUBRO', 'OUTUBRO'),
        ('NOVEMBRO', 'NOVEMBRO'),
        ('DEZEMBRO', 'DEZEMBRO')
    ]
    categories = [
        ('RECEITA', 'RECEITA'),
        ('GASTO', 'GASTO'),
        ('INVESTIMENTO', 'INVESTIMENTO')
    ]

    user = models.ForeignKey(
        User,
        null=True,
        on_delete=models.CASCADE
    )
    name = models.CharField(
        max_length=255,
        null=True
    )
    category = models.CharField(
        max_length=12,
        choices=categories,
        null=True
    )
    value = models.FloatField(
        null=True
    )
    month = models.CharField(
        max_length=10,
        choices=months,
        null=True,
        default=months[today_month-1][0]
    )
    date_created = models.DateTimeField(
        auto_now_add=True,
        null=True
    )

    def __str__(self):
        return self.name

class FixedValues(models.Model):
    categories = [
        ('RECEITA', 'RECEITA'),
        ('GASTO', 'GASTO'),
        ('INVESTIMENTO', 'INVESTIMENTO')
    ]
    fixed_choices = [
        ('VARIÁVEL', 'VARIÁVEL'),
        ('FIXO', 'FIXO')
    ]

    user = models.ForeignKey(
        User,
        null=True,
        on_delete=models.CASCADE
    )
    name = models.CharField(
        max_length=255,
        null=True
    )
    category = models.CharField(
        max_length=12,
        choices=categories,
        null=True
    )
    value = models.FloatField(
        null=True
    )
    fixed = models.CharField(
        max_length=12,
        choices=fixed_choices,
        null=True
    )
    date_created = models.DateTimeField(
        auto_now_add=True,
        null=True
    )

    def __str__(self):
        return self.name


#Authors: Prince Rios and Alex Espinoza
#Last modified: 8 March 2020
#Description: This program uses a class to create a GUI that allows a user to enter text into a box and search for an image
#using QLineEdit. There is also a QPushButton in the gui to allow the user to search for the image. Once the user clicks the
#search button, the image they chose is displayed in a new window, which has a combo box under the iamge to allow the user to
#choose between a sepia, negative, grayscale, or thumbnail version of the image.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QComboBox, QVBoxLayout, QHBoxLayout, QLabel, QMainWindow
# from task2 import Window
from PyQt5.QtGui import QPixmap
import PIL.Image
# from PIL import Image
# self.im = PIL.Image.open('34694102243_3370955cf9_z.jpg')
# self.im.show()
class Image(QWidget):
	def __init__(self):
		super().__init__()
		self.UI()
		self.show()
	def UI(self):
		self.setGeometry(500, 300, 200, 100)
		self.setWindowTitle('Image Search')
		self.InitUI()
		self.show()
	def InitUI(self):
		# Label, Line Edit, and Push button created in this function.
		self.label = QLabel(self)
		self.label.setText('Search by id, title, flickr user, or tag')
		self.search_button = QPushButton(self)
		self.search_button.setText('Search')
		self.line_edit = QLineEdit(self)
		vbox = QVBoxLayout()
		vbox.addWidget(self.label)
		vbox.addWidget(self.line_edit)
		vbox.addWidget(self.search_button)
		self.setLayout(vbox)
		self.show()
		self.search_button.clicked.connect(self.btn_clicked)
	# Function created to show image once the button is clicked
	def btn_clicked(self):
		ls = ['Sepia', 'Negative', 'Grayscale', 'Thumbnail', 'None']
		image_info = [
	    {
	        	"id" : "34694102243_3370955cf9_z",
	           	"title" : "Eastern",
	           	"flickr_user" : "Sean Davis",
	           	"tags" : ["Los Angeles", "California", "building"]
	     },
	     {
	        	"id" : "37198655640_b64940bd52_z",
	           	"title" : "Spreetunnel",
	           	"flickr_user" : "Jens-Olaf Walter",
	           	"tags" : ["Berlin", "Germany", "tunnel", "ceiling"]
	      },
	      {
	           	"id" : "36909037971_884bd535b1_z",
	           	"title" : "East Side Gallery",
	           	"flickr_user" : "Pieter van der Velden",
	           	"tags" : ["Berlin", "wall", "mosaic", "sky", "clouds"]
	      },
	      {
	           	"id" : "36604481574_c9f5817172_z",
	           	"title" : "Lombardia, september 2017",
	           	"flickr_user" : "Mónica Pinheiro",
	           	"tags" : ["Italy", "Lombardia", "alley", "building", "wall"]
	      },
	      {
	           	"id" : "36885467710_124f3d1e5d_z",
	           	"title" : "Palazzo Madama",
	           	"flickr_user" : "Kevin Kimtis",
	           	"tags" : [ "Rome", "Italy", "window", "road", "building"]
	      },
	      {
	           	"id" : "37246779151_f26641d17f_z",
	           	"title" : "Rijksmuseum library",
	           	"flickr_user" : "John Keogh",
	           	"tags" : ["Amsterdam", "Netherlands", "book", "library", "museum"]
	      },
	      {
	           	"id" : "36523127054_763afc5ed0_z",
	           	"title" : "Canoeing in Amsterdam",
	           	"flickr_user" : "bdodane",
	           	"tags" : ["Amsterdam", "Netherlands", "canal", "boat"]
	      },
	      {
	           	"id" : "35889114281_85553fed76_z",
	           	"title" : "Quiet at dawn, Cabo San Lucas",
	           	"flickr_user" : "Erin Johnson",
	           	"tags" : ["Mexico", "Cabo", "beach", "cactus", "sunrise"]
	      },
	      {
	           	"id" : "34944112220_de5c2684e7_z",
	           	"title" : "View from our rental",
	           	"flickr_user" : "Doug Finney",
	           	"tags" : ["Mexico", "ocean", "beach", "palm"]
	      },
	      {
	           	"id" : "36140096743_df8ef41874_z",
	           	"title" : "Someday",
	           	"flickr_user" : "Thomas Hawk",
	           	"tags" : ["Los Angeles", "Hollywood", "California", "Volkswagen", "Beatle", "car"]
	      }
		]
		# this just sets all the layouts for the guis and get the pictures from the folder
		self.new_window = QWidget()
		self.new_window.setGeometry(0, 0, 600, 600)
		self.label2 = QLabel(self.new_window)
		new_list = self.line_edit.text().split()
		for i in range(len(new_list)):
			new_list[i] = new_list[i].lower()
		for i in range(len(image_info)):
			for j in range(len(image_info[i]['tags'])):
				image_info[i]['tags'][j] = image_info[i]['tags'][j].lower()
		counter = 0
		tag_list = []
		for i in range(len(image_info)):
			for key, value in image_info[i].items():
				if key != 'tags' and self.line_edit.text().lower() == value.lower():
					self.img = image_info[i]["id"]
					self.image = QPixmap(f'{image_info[i]["id"]}.jpg')
				for j in range(len(new_list)):
					if new_list[j] in image_info[i]["tags"]:
						counter += 1
						if counter == 2:
							self.img = image_info[i]["id"]
							self.image = QPixmap(f'{image_info[i]["id"]}.jpg')
						else:
							self.img = image_info[i]["id"]
							self.image = QPixmap(f'{self.img}.jpg')
		self.label2.setPixmap(self.image)
		self.combo = QComboBox(self.new_window)
		for i, value in enumerate(ls):
			self.combo.addItem(value)
		self.combo.move(0, 477)
		self.label3 = QLabel(self.new_window)
		self.combo.currentTextChanged.connect(self.combo_change)
		self.new_window.show()
	#Function created to activate the designated image manipulation functions once the combo text is changed.
	def combo_change(self):
		text = self.combo.currentText()
		if text == 'Negative':
			self.negative()
		elif text == 'Grayscale':
			self.grayscale()
		elif text == 'Thumbnail':
			self.thumbnail()
		elif text == 'Sepia':
			self.sepia()
	# functions are just to convert image to whatever colors
	def negative(self):
		self.im = PIL.Image.open(f'{self.img}.jpg')
		new_list = []
		for p in self.im.getdata():
 			temp = (int(p[0]*.5), p[1], p[2])
 			new_list.append(temp)
		self.im.putdata(new_list)
		# self.im.save(f'negative_{self.img}.jpg')
		self.im.show()
	def grayscale(self):
		self.im = PIL.Image.open(f'{self.img}.jpg')
		new_list = map(lambda a : (int((a[0]+a[1]+a[2])/3),) * 3, self.im.getdata())
		self.im.putdata(list(new_list))
		# self.im.save(f'grayscale_{self.img}.jpg')
		self.im.show()
	def thumbnail(self):
		self.im = PIL.Image.open(f'{self.img}.jpg')
		self.xcenter = self.im.width/2
		self.ycenter = self.im.height/2
		self.x1 = self.xcenter - 100
		self.y1 = self.ycenter - 100
		self.x2 = self.xcenter + 100
		self.y2 = self.ycenter + 100
		self.cropped = self.im.crop((self.x1, self.y1, self.x2, self.y2))
		# self.cropped.save(f'thumbnail_{self.img}.jpg')
		self.cropped.show()
	#Attribution for sepia filter: Tessaracter from stack overflow
	def sepia(self):
		self.im = PIL.Image.open(f'{self.img}.jpg')
		self.width, self.height = self.im.size
		self.pixels = self.im.load() # create the pixel map
		for y in range(self.height):
			for x in range(self.width):
				r, g, b = self.im.getpixel((x, y))
				tr = int(0.393 * r + 0.769 * g + 0.189 * b)
				tg = int(0.349 * r + 0.686 * g + 0.189 * b)
				tb = int(0.272 * r + 0.534 * g + 0.131 * b)
				if tr > 255:
					tr = 255
				if tg > 255:
					tg = 255
				if tb > 255:
					tb = 255
				self.pixels[x, y] = (tr, tg, tb)
		self.im.show()
app = QApplication(sys.argv)

image = Image()

sys.exit(app.exec_())


import pickle

import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OrdinalEncoder

from ml_project.entities.feature_params import FeatureParams

class Features_transformer(object):

    def __init__(self, params: FeatureParams):
        self.transformer_was_fitted = False
        self.params = params
        if (params.categorical_features is not None) and (params.numerical_features is not None):
            transformer = ColumnTransformer(
                [
                    (
                        "categorical_pipeline",
                        self.build_categorical_pipeline(),
                        params.categorical_features,
                    ),
                    (
                        "numerical_pipeline",
                        self.build_numerical_pipeline(),
                        params.numerical_features,
                    ),
                ]
            )

        elif params.categorical_features is None:
            self.params.categorical_features = []
            transformer = ColumnTransformer(
                [
                    (
                        "numerical_pipeline",
                        self.build_numerical_pipeline(),
                        params.numerical_features,
                    )
                ]
            )

        elif params.numerical_features is None:
            self.params.numerical_features = []
            transformer = ColumnTransformer(
                [
                    (
                        "categorical_pipeline",
                        self.build_categorical_pipeline(),
                        params.categorical_features,
                    )
                ]
            )
        else:
            raise NotImplementedError("Numerical and categorical columns is None")

        self.transformer = transformer

    @staticmethod
    def build_categorical_pipeline() -> Pipeline:
        categorical_pipeline = Pipeline(
            [
                ("ordinal", OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=np.nan)),
                ("impute", SimpleImputer(missing_values=np.nan, strategy="most_frequent"))
            ]
        )
        return categorical_pipeline

    @staticmethod
    def build_numerical_pipeline() -> Pipeline:
        num_pipeline = Pipeline(
            [("impute", SimpleImputer(missing_values=np.nan, strategy="mean"))]
        )
        return num_pipeline

    def check_input_df(self, df: pd.DataFrame) -> pd.DataFrame:

        # check type of input
        if type(df) != pd.DataFrame:
            raise NotImplementedError("DataFrame object type expected as input")

        # check matching of columns with params
        params_categorical = self.params.categorical_features
        params_numerical = self.params.numerical_features
        param_cols = params_categorical + params_numerical
        param_cols.sort()
        df_cols = df.columns.to_list()
        df_cols.sort()
        if param_cols != df_cols:
            raise NotImplementedError(f"DataFrame columns don't match with params",
                                      f"Expected {param_cols}",
                                      f"Got {df_cols}")

        # check matching of columns types with params
        df_cols_numeric = df.select_dtypes(
            include=['int16', 'int32', 'int64', 'float16', 'float32', 'float64']).columns.to_list()
        df_cols_numeric.sort()
        params_numerical.sort()
        if df_cols_numeric != params_numerical:
            raise NotImplementedError("Numeric columns don't match with params")
        df_cols_categorical = [col for col in df.columns if col not in df_cols_numeric]
        df_cols_categorical.sort()
        params_categorical.sort()
        if df_cols_categorical != params_categorical:
            raise NotImplementedError("Categorical columns don't match with params")

        # fix columns order
        df = df[
            [n_col for n_col in self.params.numerical_features] + [c_col for c_col in self.params.categorical_features]]

        return df

    def fit(self, df: pd.DataFrame):
        df = self.check_input_df(df)
        self.transformer.fit(df)
        self.transformer_was_fitted = True

    def transform(self, df: pd.DataFrame) -> pd.DataFrame:
        if not self.transformer_was_fitted:
            raise NotImplementedError("First you need fit transformer")
        df = self.check_input_df(df)
        return pd.DataFrame(self.transformer.transform(df))

    def fit_transform(self, df: pd.DataFrame) -> pd.DataFrame:
        df = self.check_input_df(df)
        self.transformer.fit(df)
        self.transformer_was_fitted = True
        return pd.DataFrame(self.transformer.transform(df))

def make_features(transformer: Features_transformer, df: pd.DataFrame) -> pd.DataFrame:
    return pd.DataFrame(transformer.transform(df))

def extract_target(df: pd.DataFrame, params: FeatureParams) -> pd.Series:
    target = df[params.target_col]
    return target

def serialize_features_transformer(transformer: Features_transformer, output: str) -> str:
    with open(output, "wb") as f:
        pickle.dump(transformer, f)
    return output


from django.urls import path

from . import views

urlpatterns = [
    path('', views.Security, name='Security'),
    path('recipientadd/', views.RecipientAdd, name='RecipientAdd'),
    path('recipientremove/<str:pk>/', views.RecipientRemove, name='RecipientRemove'),
    path('recipientedit/<str:pk>/', views.RecipientEdit, name='RecipientEdit'),
    # path('alert/', views.alert, name = 'alert'),

]


import beaker.container as container
from beaker.exceptions import InvalidCacheBackendError
import beaker.util as util

clsmap = {
          'memory':container.MemoryNamespaceManager,
          'dbm':container.DBMNamespaceManager,
          'file':container.FileNamespaceManager,
          }

try:
    import beaker.ext.memcached as memcached
    clsmap['ext:memcached'] = memcached.MemcachedNamespaceManager
except InvalidCacheBackendError:
    pass

try:
    import beaker.ext.database as database
    clsmap['ext:database'] = database.DatabaseNamespaceManager
except InvalidCacheBackendError:
    pass

try:
    import beaker.ext.sqla as sqla
    clsmap['ext:sqla'] = sqla.SqlaNamespaceManager
except InvalidCacheBackendError:
    pass

try:
    import beaker.ext.google as google
    clsmap['ext:google'] = google.GoogleNamespaceManager
except (InvalidCacheBackendError, SyntaxError):
    pass

class Cache(object):
    """Front-end to the containment API implementing a data cache."""
    def __init__(self, namespace, **kwargs):
        self.namespace = namespace
        self.context = container.ContainerContext()
        self._values = {}
        self.kwargs = kwargs
        self.kwargs.setdefault('type', 'memory')

    def put(self, key, value, **kwargs):
        self._values.pop(key, None)
        self._get_value(key, **kwargs).set_value(value)
    set_value = put

    def get(self, key, **kwargs):
        return self._get_value(key, **kwargs).get_value()
    get_value = get

    def remove_value(self, key, **kwargs):
        mycontainer = self._get_value(key, **kwargs)
        if mycontainer.has_current_value():
            mycontainer.clear_value()

    def _get_value(self, key, **kwargs):
        if isinstance(key, unicode):
            key = key.encode('ascii', 'backslashreplace')

        if not kwargs:
            value = self._values.get(key)
        else:
            value = None

        if not value:
            kw = self.kwargs.copy()
            kw.update(kwargs)
            type = kw.pop('type')
            self._values[key] = value = container.Value(key, self.context, self.namespace, clsmap[type], **kw)
        return value

    def _get_namespace(self, **kwargs):
        type = kwargs['type']
        return self.context.get_namespace(self.namespace, clsmap[type], **kwargs)

    def _get_namespaces(self):
        """return a collection of all distinct ``Namespace`` instances
        referenced by this ``Cache``."""

        return util.Set(
            [v.namespacemanager for v in self._values.values()]).\
            union([self._get_namespace(**self.kwargs)])

    def clear(self):
        """clear this Cache's default namespace, as well as any other
        Namespaces that have been referenced by this Cache."""
        for namespace in self._get_namespaces():
            namespace.remove()
        self._values = {}

    # public dict interface
    def __getitem__(self, key):
        return self.get(key)

    def __contains__(self, key):
        return self.has_key(key)

    def has_key(self, key):
        mycontainer = self._get_value(key)
        return mycontainer.has_current_value()

    def __delitem__(self, key):
        self.remove_value(key)

    def __setitem__(self, key, value):
        self.put(key, value)

class CacheManager(object):
    def __init__(self, **kwargs):
        self.kwargs = kwargs
        self.caches = {}

    def get_cache(self, name, **kwargs):
        kw = self.kwargs.copy()
        kw.update(kwargs)
        return self.caches.setdefault(name + str(kw), Cache(name, **kw))


from flask import Flask , render_template, jsonify, request, redirect, url_for, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import ForeignKey

app = Flask(__name__)

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'

db = SQLAlchemy(app)

# class for an item
class Item(db.Model):
	# Defines the Table Name item
	__tablename__ = "item"

	# Makes three columns into the table id, description, price
	_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
	name = db.Column(db.Text(), nullable=False)
	description = db.Column(db.Text(), nullable=False)
	price = db.Column(db.Numeric(6,2), nullable=False)

	# A constructor function where we will pass the id, description and price and add as a new entry in the table.
	def __init__(self, name, description, price):
		self.name = name
		self.description = description
		self.price = price

	def __repr__(self):
		return "Item<id={} name={} description={} price={}>".format(self._id, self.name, self.description, self.price)

# class for image
class Image(db.Model):
	# Defines the Table Name item
	__tablename__ = "image"

	# Makes two columns into the table
	_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
	name = db.Column(db.Text(), nullable=False)

	# A constructor function where we will pass the id and name of image and add as a new entry in the table.
	def __init__(self, name):
		self.name = name

	def __repr__(self):
		return "Image<id={} name={}>".format(self._id, self.name)

# Connect images to items
class Item_Image(db.Model):
	# Defines the Table Name item
	__tablename__ = "item_image"

	# Makes two columns into the table
	image_id = db.Column(db.Integer, ForeignKey("image._id"), primary_key=True)
	item_id = db.Column(db.Integer, ForeignKey("item._id"))

	def __init__(self, image_id, item_id):
		self.image_id = image_id
		self.item_id = item_id

	def __repr__(self):
		return "Item_Image<image_id={} item_id={}>".format(self.image_id, self.item_id)

# Control will come here and then gets redirect to the index function
@app.route("/")
def home():
	return redirect(url_for('index'))

@app.route("/index", methods = ["GET", "POST"])
def index():
	if request.method == 'POST': # When a user clicks submit button it will come here.
		data = request.form # request the data from the form in index.html fileee
		if "name" in data and "price" in data and "description" in data:
			name = data["name"]
			price = data["price"]
			description = data["description"]

			new_data = Item(name, description, price)
			db.session.add(new_data)
			db.session.commit()
		elif "image_name" in data:
			image_name = data["image_name"]

			new_image = Image(image_name)
			db.session.add(new_image)
			db.session.commit()
		elif "item_id" in data and "image_id" in data:
			image_id = data["image_id"]
			item_id = data["item_id"]

			new_data = Item_Image(image_id, item_id)
			db.session.add(new_data)
			db.session.commit()

	item_data = Item.query.all()
	image_data = Image.query.all()
	connection = Item_Image.query.all()

	return render_template("index.html", item_data = item_data, image_data = image_data, item_image_data = connection) # passes user_data variable into the index.html file.

if __name__=="__main__":
	db.create_all()
	app.run(debug=True, port=3000)


import logging
from logging import config
from deiis.rabbit import Task

logging.config.fileConfig('testlog.ini')

def getLogger(obj):
    if isinstance(obj, basestring):
        return logging.getLogger(obj)
    return logging.getLogger(obj.__class__.__name__)

def root_test():
    logger = getLogger('root')
    logger.debug('root debug')
    logger.info('root info')
    logger.warn('root warn')

def rank_test():
    logger = getLogger('rank')
    logger.debug('rank debug')
    logger.info('rank info')
    logger.warn('rank warn')

def core_test():
    logger = getLogger('core')
    logger.debug('core debug')
    logger.info('core info')
    logger.warn('core warn')

class LoggerTest(Task):
    def __init__(self):
        super(LoggerTest, self).__init__('test')

    def test(self):
        self.logger.debug('class debug')
        self.logger.info('class info')
        self.logger.warn('class debug')

root_test()
rank_test()
core_test()
LoggerTest().test()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
易盾反垃圾云服务点播音视频解决方案离线结果获取接口python示例代码
接口文档: http://dun.163.com/api.html
python版本：python3.7
运行:
    1. 修改 SECRET_ID,SECRET_KEY 为对应申请到的值
    2. $ python videosolution_callback.py
"""
__author__ = 'yidun-dev'
__date__ = '2019/11/27'
__version__ = '0.2-dev'

import hashlib
import time
import random
import urllib.request as urlrequest
import urllib.parse as urlparse
import json
from gmssl import sm3, func

class VideoSolutionCallbackAPIDemo(object):
    """点播音视频解决方案检测结果获取接口示例代码"""

    API_URL = "http://as.dun.163.com/v1/videosolution/callback/results"
    VERSION = "v1.1"  # 点播音视频解决方案版本v1.1及以上语音二级细分类subLabels结构进行调整

    def __init__(self, secret_id, secret_key):
        """
        Args:
            secret_id (str) 产品密钥ID，产品标识
            secret_key (str) 产品私有密钥，服务端生成签名信息使用
        """
        self.secret_id = secret_id
        self.secret_key = secret_key

    def gen_signature(self, params=None):
        """生成签名信息
        Args:
            params (object) 请求参数
        Returns:
            参数签名md5值
        """
        buff = ""
        for k in sorted(params.keys()):
            buff += str(k) + str(params[k])
        buff += self.secret_key
        if "signatureMethod" in params.keys() and params["signatureMethod"] == "SM3":
            return sm3.sm3_hash(func.bytes_to_list(bytes(buff, encoding='utf8')))
        else:
            return hashlib.md5(buff.encode("utf8")).hexdigest()

    def check(self):
        """请求易盾接口
        Returns:
            请求结果，json格式
        """
        params = {}
        params["secretId"] = self.secret_id
        params["version"] = self.VERSION
        params["timestamp"] = int(time.time() * 1000)
        params["nonce"] = int(random.random() * 100000000)
        # params["signatureMethod"] = "SM3"  # 签名方法，默认MD5，支持SM3
        params["signature"] = self.gen_signature(params)

        try:
            params = urlparse.urlencode(params).encode("utf8")
            request = urlrequest.Request(self.API_URL, params)
            content = urlrequest.urlopen(request, timeout=10).read()
            return json.loads(content)
        except Exception as ex:
            print("调用API接口失败:", str(ex))

if __name__ == "__main__":
    """示例代码入口"""
    SECRET_ID = "your_secret_id"  # 产品密钥ID，产品标识
    SECRET_KEY = "your_secret_key"  # 产品私有密钥，服务端生成签名信息使用，请严格保管，避免泄露

    api = VideoSolutionCallbackAPIDemo(SECRET_ID, SECRET_KEY)

    ret = api.check()

    code: int = ret["code"]
    msg: str = ret["msg"]
    if code == 200:
        resultArray: list = ret["result"]
        if len(resultArray) == 0:
            print("暂时没有结果需要获取, 请稍后重试!")
        else:
            for resultItem in resultArray:
                taskId: str = resultItem["taskId"]
                result: int = resultItem["result"]
                print("taskId: %s, result: %s" % (taskId, result))
                if resultItem["evidences"] is not None:
                    evidences: dict = resultItem["evidences"]
                    if evidences["audio"] is not None:
                        audio: dict = evidences["audio"]
                        asrStatus: int = audio["asrStatus"]
                        if asrStatus == 4:
                            asrResult: int = audio["asrResult"]
                            print("检测失败: taskId=%s, asrResult=%s" % (taskId, asrResult))
                        else:
                            action: int = audio["action"]
                            labelArray: list = audio["labels"]
                            if action == 0:
                                print("taskId=%s，结果：通过" % taskId)
                            elif action == 1 or action == 2:
                                for labelItem in labelArray:
                                    label: int = labelItem["label"]
                                    level: int = labelItem["level"]
                                    # 注意二级细分类结构
                                    subLabels: list = labelItem["subLabels"]
                                    if subLabels is not None and len(subLabels) > 0:
                                        for subLabelItem in subLabels:
                                            subLabel: str = subLabelItem["subLabel"]
                                            details: dict = subLabelItem["details"]
                                            hintArray: list = details["hint"]
                                print("taskId=%s, 结果: %s，证据信息如下: %s" % (taskId, "不确定" if action == 1 else "不通过", labelArray))
    else:
        print("ERROR: code=%s, msg=%s" % (ret["code"], ret["msg"]))


import numpy as np
import argparse
import cv2
import os

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image")
args = vars(ap.parse_args())
confind = 0.5
threshold= 0.3
labelsPath = os.path.sep.join([ "E:\Yoloproject\Yolo_img\yolo-coco\coco.names"])
LABELS = open(labelsPath).read().strip().split("\n")

# np.random.seed(42)
# COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8")
weightsPath = os.path.sep.join([ "E:\Yoloproject\Yolo_img\yolo-coco\yolov3.weights"])
configPath = os.path.sep.join(["E:\Yoloproject\Yolo_img\yolo-coco\yolov3-320.cfg"])
print("loading YOLO")
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)

image = cv2.imread(args["image"])
(H, W) = image.shape[:2]
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)

layerOutputs = net.forward(ln)

imgGry = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

ret, thrash = cv2.threshold(imgGry, 240 , 255, cv2.CHAIN_APPROX_NONE)
contours , hierarchy = cv2.findContours(thrash, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

boxes = []
confidences = []
classIDs = []
count = 0

for output in layerOutputs:
    for detection in output:
        scores = detection[5:]
        classID = np.argmax(scores)
        confidence = scores[classID]
        if confidence > confind:
            box = detection[0:4] * np.array([W, H, W, H])
            (centerX, centerY, width, height) = box.astype("int")
            x = int(centerX - (width / 2))
            y = int(centerY - (height / 2))
            boxes.append([x, y, int(width), int(height)])
            confidences.append(float(confidence))
            classIDs.append(classID)

idxs = cv2.dnn.NMSBoxes(boxes, confidences, confind, threshold)

if len(idxs) > 0:
    for i in idxs.flatten():
        (x, y) = (boxes[i][0], boxes[i][1])
        (w, h) = (boxes[i][2], boxes[i][3])
        # color = [int(c) for c in COLORS[classIDs[i]]]
        crop = image[y-5: y+h+5 , x-5: x+w+5]
        image_name1 = LABELS[classIDs[i]] + '_'+ str(count) + '.png'
        image_name2 = 'Detected_Objects.png'
        image_path1 = os.path.join('E:\Yoloproject\Yolo_img\images\Cropped_Images', image_name1 )
        # image_path2 = os.path.join('E:\Yoloproject\Yolo_img\images\Detected_Image',image_name2)
        cv2.imwrite(image_path1,crop)

        # text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
        # cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
        cv2.imshow(LABELS[classIDs[i]]+ '_'+ str(count), crop)
        count +=1
        # rectangle = cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
        print(LABELS[classIDs[i]].upper(), 'with a confidence of : ',  round((confidences[i]*100),4),'%')

# cv2.imwrite(image_path2, image)
cv2.waitKey(0)


import sqlite3
import re

total_insert = 0

def create_table_url_list():
    conn.execute('DROP TABLE IF EXISTS url_list')
    conn.execute('CREATE TABLE url_list(url text UNIQUE)')
    conn.execute('CREATE INDEX url_idx on url_list(url)')

def create_table_headline_list():
    conn.execute('DROP TABLE IF EXISTS headline_list')
    conn.execute('CREATE TABLE headline_list(headline text, url_id integer)')
    conn.execute('CREATE INDEX url_id_idx on headline_list(url_id)')

def create_table_word_list():
    conn.execute('DROP TABLE IF EXISTS word_list')
    conn.execute('CREATE TABLE word_list(word text UNIQUE)')
    conn.execute('CREATE INDEX word_idx on word_list(word)')

def create_table_word_location():
    conn.execute('DROP TABLE IF EXISTS word_location')
    conn.execute('CREATE TABLE word_location(url_id integer,word_id integer,location integer)')
    conn.execute('CREATE INDEX word_id_idx on word_location(word_id)')

def create_tables():
    create_table_url_list()
    create_table_headline_list()
    create_table_word_list()
    create_table_word_location()
    conn.commit()

def insert_to_url_list(url):
    global total_insert
    total_insert += 1
    conn.execute('INSERT INTO url_list VALUES(?)', [url])

def insert_to_headline_list(headline, url_id):
    global total_insert
    total_insert += 1
    conn.execute('INSERT INTO headline_list VALUES(?,?)', [headline, url_id])

def insert_to_word_list(word_list):
    for word in word_list:
        global total_insert
        total_insert += 1
        if word in ignore_words:
            continue
        conn.execute("INSERT OR IGNORE INTO word_list VALUES(?)", [word])
    return

def insert_to_word_location(word_list, url_id):
    location = 0
    for word in word_list:
        global total_insert
        total_insert += 1
        if word in ignore_words:
            continue
        word_id = get_word_id(word)
        conn.execute('INSERT INTO word_location VALUES(?,?,?)', [url_id, word_id, location])
        location += 1

def get_word_id(word):
    return conn.execute('SELECT rowid from word_list where word IN(?)', [word]).fetchone()[0]

# Returns maximum 150 words from the body
def get_word_list(body):
    word_list = separate_words(body)
    word_count = min(len(word_list), 150)
    return word_list[:word_count]

def separate_words(text):
    splitter = re.compile('\\W+')
    return [s.lower() for s in splitter.split(text) if s != '']

# For every news article in news_table insert values to necessary tables
def populate_tables():
    c = conn.cursor()
    c.execute('SELECT rowid, webUrl, headline, body from news_table order by rowid')
    row_id = 1
    global total_insert
    for row in c:
        insert_to_url_list(row[1])
        insert_to_headline_list(row[2], row[0])
        word_list = get_word_list(row[3])
        insert_to_word_list(word_list)
        insert_to_word_location(word_list, row[0])
        if row_id % 1000 == 0:  # Commit only after 1000 rows for fast insertion
            conn.commit()
            print(row_id, total_insert)
        row_id += 1
    conn.commit()

conn = sqlite3.connect('guardian_news.db')
create_tables()
# Create a list of words to ignore
ignore_words = ['the', 'of', 'to', 'and', 'a', 'in', 'is', 'it', 'on', 'by', 'with', 'for']
populate_tables()
print(total_insert)


import base64
import configparser
import io
import json
import numpy as np
from flask import Flask, jsonify, request
from scipy.io import wavfile
from rasa_connector import Rasa_Webhooks
from riva_utils import RivaUtils

CONFIG = configparser.ConfigParser()
CONFIG.read('config.ini')
RIVA_URL_PORT = CONFIG['DEFAULT']['RivaURL'] + ":" + CONFIG['DEFAULT']['RivaPort']
RASA_URL_PORT = CONFIG['DEFAULT']['RasaURL'] + ":" + CONFIG['DEFAULT']['RasaPort']

app = Flask("SmartState Chatbot API")

@app.route('/api/audiototext', methods=['POST'])
def getTextFromAudio():
	riva_utils = RivaUtils(RIVA_URL_PORT)
	message_data = request.get_json()
	return_dict = {}
	return_dict['response_msg'] = []

	user_message = riva_utils.save_blob_to_file(audio_str=message_data['audio'])

	return_dict['response_msg'].append({'text': user_message})
	return jsonify(return_dict), 200

@app.route('/api/sendmessage', methods=['POST'])
def send_message():
	riva_utils = RivaUtils(RIVA_URL_PORT)
	message_data = request.get_json()
	return_dict = {}
	return_dict['response_msg'] = []

	user_message = ""
	if message_data['audio'] != -1:
		user_message = riva_utils.save_blob_to_file(audio_str=message_data['audio'])
	else:
		user_message = message_data['message']

	rasa_hooks = Rasa_Webhooks(url=RASA_URL_PORT)
	resp = []
	if user_message != -1:
		resp, code = rasa_hooks.send_message(json.dumps({'sender': message_data['sender'], 'message': user_message}))
	else:
		code = 200
		resp.append({'text': "Sorry I didn't get that. Can you rephrase?"})
	if code == 200:
		for response in resp:
			the_image = ''
			the_text = ''
			the_audio = ''
			the_user_message = user_message
			if 'image' in response:
				the_image = response['image']
			elif 'text' in response:
				audio, samplerate = riva_utils.send_request_to_riva(text=response['text'], proto='tts')
				if len(audio.shape) > 1:
					audio = np.average(audio, axis=1)
				bytes_wav = bytes()
				byte_io = io.BytesIO(bytes_wav)
				wavfile.write(byte_io, samplerate, audio)
				wav_bytes = byte_io.read()
				the_text = response['text'].replace("\r\n", "
").replace("\r","
").replace("\n", "
")
				the_audio = "data:audio/wav;base64,"+base64.b64encode(wav_bytes).decode("utf-8")
			return_dict['response_msg'].append({'text': the_text, 'audio': the_audio, 'image': the_image, 'sent_message': the_user_message})
			user_message = -1

		return jsonify(return_dict), 200
	else:
		return jsonify({'response_msg': [{'text':'An error occured. Please wait a few minutes or refresh the page.'}]}), 500

@app.after_request
def after_request(response):
	header = response.headers
	header['Access-Control-Allow-Origin'] = '*'
	header['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
	header['Access-Control-Allow-Methods'] = 'OPTIONS, HEAD, GET, POST, DELETE, PUT'
	return response

if __name__ == '__main__':
	app.run(host='0.0.0.0', port=8181, debug=False, ssl_context=('mycert.crt', 'mycert.key'))


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('scenic', '0017_auto_20170512_0906'),
    ]

    operations = [
        migrations.CreateModel(
            name='Facilities',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(default=b'-', max_length=512, blank=True)),
                ('scenic_facilities', models.ForeignKey(to='scenic.Scenic', null=True)),
            ],
            options={
                'ordering': ['-id'],
            },
        ),
        migrations.CreateModel(
            name='Point',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('latitude', models.TextField(null=True, blank=True)),
                ('facilities_point', models.ForeignKey(to='scenic.Facilities', null=True)),
            ],
            options={
                'ordering': ['-id'],
            },
        ),
    ]


# https://atcoder.jp/contests/agc007/tasks/agc007_a
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate

def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと

def read_ints(): return list(map(int, read().split()))

def read_map_as(H, replace={'#': 1, '.': 0}, pad=None):
    '''
    文字列のmapを置換して読み込み。デフォでは#→1,.→0
    '''
    if pad is None:
        ret = []
        for _ in range(H):
            ret.append([replace[s] for s in read()[:-1]])
            # 内包表記はpypyでは若干遅いことに注意
            # #numpy使うだろうからこれを残しておくけど
    else:  # paddingする
        ret = [[pad] * (W + 2)]  # Wはどっかで定義しておくことに注意
        for _ in range(H):
            ret.append([pad] + [replace[s] for s in read()[:-1]] + [pad])
        ret.append([pad] * (W + 2))

    return ret

MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from functools import reduce

# 常に右、下に動いていたなら、経路は一つに決まる。
# →#をたどってそれ以外に#があるかを判別すれば良い
H, W = read_ints()
A = read_map_as(H)
i, j = 0, 0
A[i][j] = 0
while i != H - 1 or j != W - 1:
    if i + 1 < H and j + 1 < W and A[i + 1][j] and A[i][j + 1]:
        print('Impossible')
        exit()
    ni = i
    nj = j
    if i + 1 < H and A[i + 1][j]:
        ni = i + 1
    if j + 1 < W and A[i][j + 1]:
        nj = j + 1
    if ni == i and nj == j:
        print('Impossible')
        exit()
    A[ni][nj] = 0  # 簡単のために0を埋めておく
    i, j = ni, nj

res = reduce(add, reduce(add, A))
# print(*A, sep='\n')
# print(res)
print('Possible' if res == 0 else 'Impossible')


import serial
import time

class Arduino():

    def connect_arduino(self):
        conn = False
        print("Searching for Arduino...")
        while not conn:
            if(self.ser.inWaiting()>0):
                data = self.ser.read()
                data = str(data)
                print(data)
#                if data == 'a':
#                    self.ser.write('a')
#                    time.sleep(0.001)
#                if data == 'b':
#                    self.ser.flush()
#                    self.ser.write('b')
#                    conn = True
#                    print "Connection to Arduino established"
#                time.sleep(0.001)

    def __init__(self):
        self.ser = serial.Serial('COM6', 9600)
        self.ser.flushInput()
        self.connect_arduino()

    def send_command(self, command):
        self.ser.write(command)


from functools import lru_cache
import logging

import netCDF4
import numpy as np
from scipy.signal import butter, filtfilt
import dbdreader

from slocum import ladcp
from profiles import iterprofiles

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(name=__name__)

class DVLProcessor(object):
    '''Class to process DVL data collected by a Slocum glider

    The class requires data from three different sources:

    * a NetCDF file with DVL data, processed by the rdi package
    * a NetCDF file with calibrated glider flight data (using deploymentcalibrator package)
    * dbd files for additional engineering data, such as gps coordinates.

    The glider data are split in profiles (most likely consisting of down casts only due to
    the fact that DVL on the up cast make not so much sense). For each profile a single velocity
    profile is computed using the LoweredADCP approach. Which constraints are used are user-configurable.
    Valid options are:

    use_bottom_track
    use_barotropic_velocity_constraint
    use_surface_velocity_constraint

    Additionally gliderflight information can be used set, by the parameter
    use_glider_flight. Since this is still a relative measure, this is not sufficient
    to constrain the velocity profiles.

    Parameters
    ----------
    dvl_data_file: str
        filename of netCDF file with DVL data
    gld_flght_data_file : str
        filename of netCDF file with glider flight data
    dbd_pattern : str
        glob pattern matching the required dbd files. (ebd files are not required).
    rho_reference : float (1025)
        reference density, used to calculate bar-to-meter conversion
    buoyancy_sensor_name : str, {"m_de_oil_vol", "m_ballast_pumped"}
        name of the sensor indicating the buoyancy drive of the glider
    filter_period : float (15)
        Sets the cut-off frequency of a low pass filter, used to smooth the pressure readings
        in order to remove any effects in pressure anomalies due to surface waves

    Aditional Parameters
    --------------------
    **dbd_kwds : further keywords can be prescribed which are passed on to the dbdreader.MultiDBD class.

    Example
    -------

    >>> dvl_data_file = "/home/lucas/gliderdata/nsb3_201907/process/dvl_3beam_nsb3_201907.nc"
    >>> gld_flght_data_file = "/home/lucas/gliderdata/nsb3_201907/process/comet_nsb3_201907.nc"
    >>> dbd_pattern = "/home/lucas/gliderdata/nsb3_201907/hd/comet*.dbd"

    >>> dvlp = DVLProcessor(dvl_data_file, gld_flght_data_file, dbd_pattern,
                        rho_reference=1025, buoyancy_sensor_name='m_ballast_pumped',
                        filter_period=15)

    >>> ladcp_configuration = dict(use_bottom_track=True,
                               use_barotropic_velocity_constraint=False,
                               use_surface_velocity_constraint=False,
                               use_glider_flight=False,
                               z_min=5,
                               z_max=45,
                               dz=0.5)

    >>> dvlp.configure_ladcp(**ladcp_configuration)

    >>> sv, bv = dvlp.compute_surface_and_barotropic_water_velocities()
    >>> velocity_data = dvlp.compute_velocity_matrix()

    '''
    def __init__(self, dvl_data_file, gld_flght_data_file, dbd_pattern,
                 magnetic_variation = 0.,
                 rho_reference=1025,
                 buoyancy_sensor_name="m_ballast_pumped",
                 filter_period=15,
                 **dbd_kwds):
        self.dvl_dataset = netCDF4.Dataset(dvl_data_file, mode='r')
        self.gld_flght_dataset = netCDF4.Dataset(gld_flght_data_file, mode='r')
        self.dbd = dbdreader.MultiDBD(pattern=dbd_pattern, **dbd_kwds)
        self.bar_to_metre = 10**5/9.81/rho_reference
        self.altitude_correction_factor = 1.01 # see ~/working/git/gliderflight_waves_paper/wave_effects/vertical_motion_from_dvl.py
        self.buoyancy_sensor_name = buoyancy_sensor_name
        self.filter_period = filter_period
        self.magnetic_variation = magnetic_variation
        self.ladcp = ladcp.LoweredADCP()

    def configure_ladcp(self, **kwds):
        '''
        Configuration of the loweredADCP method.

        Parameters
        ----------
        **kwds can be any of

        use_bottom_track : bool
            use bottom track to constrain profile (True),
        use_barotropic_velocity_constraint : bool
            use barotropic velocity as constraint (True)
        use_surface_velocity_constraint : bool
            use surface (drift) velocity as constraint (True)
        use_glider_flight : bool
            use glider flight information (True)

        z_min : float
            minimum depth for computing velocity data
        z_max : float
            maximum depth for computing velocity data
        dz : float
            grid size

        Returns
        -------
        dict:
            unprocessed or recognised keywords
        '''
        remaining_kwds = self.ladcp.method_settings(**kwds)
        remaining_kwds = self.ladcp.grid_settings(**remaining_kwds)
        return remaining_kwds

    @lru_cache()
    def compute_surface_and_barotropic_water_velocities(self):
        logger.info("Computing surface and barotropic velocities from glider data.")
        _, gf_data, surface_data = self.read_data()
        water_velocities = ladcp.WaterVelocities(gf_data, surface_data, self.magnetic_variation)
        sv = water_velocities.compute_surface_velocities()
        bv = water_velocities.compute_barotropic_velocities()
        self._water_velocities = water_velocities # diagnostic reason only
        return sv, bv

    def compute_velocity_matrix(self):
        logger.info("Computing velocity matrix.")
        dvl_data, gf_data, surface_data = self.read_data()
        if self.ladcp.use_surface_velocity_constraint or self.ladcp.use_barotropic_velocity_constraint:
            sv, bv = self.compute_surface_and_barotropic_water_velocities()

        self.ladcp.initialise_grid(r=dvl_data['r'])

        ps = iterprofiles.ProfileSplitter(data=dvl_data)
        ps.split_profiles(interpolate=True)
        n_ps = len(ps)

        water_velocity = np.ma.zeros((2, self.ladcp.zi.shape[0], n_ps), float)
        water_depths = np.ma.zeros(n_ps, float)
        profile_timestamps = np.zeros_like(water_depths)
        for i, p in enumerate(ps):
            logger.info(f"Profile {i+1} from {len(ps)}")
            profile_timestamps[i] = p.t_down
            _, pressure = p.get_downcast("pressure")
            _, altitude = p.get_downcast("altitude")
            ma_altitude = np.ma.masked_array(altitude, pressure*self.bar_to_metre<15)
            water_depth = self.ladcp.compute_waterdepth(ma_altitude, pressure*self.bar_to_metre)
            water_depths[i] = water_depth
            z = pressure*self.bar_to_metre
            for j, s in enumerate('u v'.split()):
                u = self._compute_velocity_profile(p, s, z, water_depth)
                _, water_velocity[j,:, i] = u
        return ladcp.Velocity_Data(profile_timestamps, self.ladcp.zi, *water_velocity, water_depths)

    def _compute_velocity_profile(self, p, s, z, water_depth, u_gf=0, u_barotropic=0, u_surface=0):
        _, u = p.get_downcast(s)
        _, bt_u = p.get_downcast(f'bt_{s}')
        # ensure bt_u is properly masked...
        bt_u.mask = np.isnan(bt_u.data)
        U = self.ladcp.compute_depth_referenced_velocity_matrix(u, z, waterdepth=water_depth)
        ug, uw = self.ladcp.compute_velocity_profile(U, bt_u, z, u_gf,
                                                     u_barotropic, u_surface)
        return ug, uw

    @lru_cache()
    def read_data(self):
        logger.info("Reading dvl data...")
        dvl_data = self._read_dvl_data()
        logger.info("Reading dbd data...")
        surface_data = self._read_dbd_data()
        logger.info("Reading glider flight data...")
        gf_data = self._read_gf_data()
        logger.info("Data read.")
        # add depth of glider to dvl_data:
        dvl_data['z'] = np.interp(dvl_data['time'], gf_data['time'], gf_data['pressure']*self.bar_to_metre)
        dvl_data['pressure'] = dvl_data['z']/self.bar_to_metre
        return dvl_data, gf_data, surface_data

    def _read_dvl_data(self):

        dvl_t = self.dvl_dataset.variables['time'][:].data # no mask
        dvl_r = self.dvl_dataset.variables['z'][:].data    # no mask
        dvl_u = self.dvl_dataset.variables['Velocity1'][:]
        dvl_v = self.dvl_dataset.variables['Velocity2'][:]
        dvl_w = self.dvl_dataset.variables['Velocity3'][:]
        dvl_bt_u = self.dvl_dataset.variables['BTVel1'][:]
        dvl_bt_v = self.dvl_dataset.variables['BTVel2'][:]
        dvl_bt_w = self.dvl_dataset.variables['BTVel3'][:]
        R1 = self.dvl_dataset.variables['Range1'][:]
        R3 = self.dvl_dataset.variables['Range3'][:]
        R = 0.5*(R1 + R3)*self.altitude_correction_factor
        dvl_data = dict(time=dvl_t, r=dvl_r, u=dvl_u, v=dvl_v, w=dvl_w,
                        bt_u=dvl_bt_u, bt_v=dvl_bt_v, bt_w=dvl_bt_w, altitude = R)
        return dvl_data

    def _read_dbd_data(self):
        t_gps, lat_gps, lon_gps, buoyancy_change = self.dbd.get_sync("m_gps_lat", "m_gps_lon", self.buoyancy_sensor_name)
        surface_data = ladcp.Surface_Data(t_gps, lat_gps, lon_gps, buoyancy_change)
        return surface_data

    def _read_gf_data(self):
        t = self.gld_flght_dataset.variables['time'][:].data
        pressure_raw = 0.1 * self.gld_flght_dataset.variables['depth'][:].data
        pressure = self.low_pass_filter(t, pressure_raw, self.filter_period)
        u = self.gld_flght_dataset.variables['u'][:].data
        hdg = self.gld_flght_dataset.variables['heading'][:].data
        gf_U = self.gld_flght_dataset.variables['U'][:].data
        gf_u = u * np.cos(np.pi/2 - hdg)
        gf_v = u * np.sin(np.pi/2 - hdg)
        gf_data = dict(time=t, pressure=pressure, pressure_raw=pressure_raw,
                    gf_u=gf_u, gf_v=gf_v, gf_U=gf_U)
        return gf_data

    def low_pass_filter(self, t, x, T):
        dt = np.median(np.diff(t))
        ti = np.arange(t.min(), t.max()+dt, dt)
        xi = np.interp(ti, t, x)
        fn = 0.5*1/dt
        fc = 1/T
        b, a = butter(N=1, Wn=fc/fn, btype='low', analog=False, output='ba', fs=1/dt)
        xi = filtfilt(b, a, xi)
        return np.interp(t, ti, xi)


import numpy as np
import os, random, copy
import kornia
import torch
import torch.utils.data as data
import torch.nn.functional as F
# from pathlib import Path
# from torchvision.transforms import Compose, ToPILImage, ToTensor, Resize
from torchvision.utils import save_image
# import matplotlib.pyplot as plt
# import torch.nn as nn
# import skimage.io as sio
# import argparse

class DataSequence(data.Dataset):
    def __init__(self, img_array, transform=None):
        """
        Args:
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.img_arr = img_array
        self.transform = transform

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

    def __getitem__(self, idx):
        sample = np.array(self.img_arr[idx])
        if self.transform:
            sample = self.transform(sample)

        return sample

class SuperDeepBillboard():

    def __init__(self, model, seqpath, direction):
        self.model = model
        self.model = model
        self.seqpath = seqpath
        self.direction = direction
        self.sample_dir = os.getcwd() + "/sampledir"
        if not os.path.exists(self.sample_dir):
            os.mkdir(self.sample_dir)

    def stripleftchars(self, s):
        for i in range(len(s)):
            if s[i].isnumeric():
                return s[i:]
        return -1

    def draw_arrows(self, img, angle1, angle2=None):
        import cv2

        img = (img.transpose((1, 2, 0)) * 255).round().astype(np.uint8).copy()

        pt1 = (int(img.shape[1] / 2), img.shape[0])
        pt2_angle1 = (
            int(img.shape[1] / 2 - img.shape[0] / 3 * np.sin(angle1)),
            int(img.shape[0] - img.shape[0] / 3 * np.cos(angle1)),
        )
        img = cv2.arrowedLine(img, pt1, pt2_angle1, (0, 0, 255), 3)
        if angle2 is not None:
            angle2 = -angle2
            pt2_angle2 = (
                int(img.shape[1] / 2 - img.shape[0] / 3 * np.sin(angle2)),
                int(img.shape[0] - img.shape[0] / 3 * np.cos(angle2)),
            )
            img = cv2.arrowedLine(img, pt1, pt2_angle2, (0, 255, 0), 3)
        return img.astype(np.float32).transpose((2, 0, 1)) / 255

    def input_diversification(self, imgs, y_orig, device):
        imgs_rs = imgs
        y_orig_exp = copy.deepcopy(y_orig)
        for i in range(imgs.shape[0]):
            r1 = random.randint(0, imgs.shape[0]-1)
            r2 = random.uniform(0.5, 2.0)
            scale_factor = torch.tensor([[r2]]).float().to(device)
            temp = kornia.geometry.transform.scale(imgs[r1][None], scale_factor)
            imgs_rs = torch.cat((imgs_rs,temp))
            y_orig_exp = torch.cat((y_orig_exp, y_orig[r1][None]),dim=0)
        return imgs_rs, y_orig_exp

    # approach for modified DAVE2
    def perturb_images(self, img_arr, img_patches, model, steering_vector,
                       bb_size=5, iterations=400, noise_level=25,
                       device=torch.device("cuda"), last_billboard=None, loss_fxn="MDirE", input_divers=False):
        # print("Perturb image sequence")
        patch_coords = img_patches[:, 1:].reshape((-1, 4, 2))
        pert_shape = c, h, w = 3, bb_size, bb_size

        src_coords = np.tile(
            np.array(
                [
                    [
                        [0.0, 0.0],
                        [w - 1.0, 0.0],
                        [0.0, h - 1.0],
                        [w - 1.0, h - 1.0],
                    ]
                ]
            ),
            (len(patch_coords), 1, 1),
        )
        src_coords = torch.from_numpy(src_coords).float()
        patch_coords = torch.from_numpy(patch_coords).float()
        # build the transforms to and from image patches
        perspective_transforms = kornia.get_perspective_transform(src_coords, patch_coords).to(device)
        patch_transforms = kornia.get_perspective_transform(patch_coords, src_coords)

        model = model.to(device)
        steering_vector = steering_vector.to(device)
        dataset = DataSequence(
            img_arr, #transform = Compose([ToPILImage(), ToTensor()])
        )
        data_loader = data.DataLoader(dataset, batch_size=len(dataset))
        shape = next(iter(data_loader)).shape[2:]
        orig_shape = shape
        mask_patch = torch.ones(len(perspective_transforms), *pert_shape).float().to(device)

        # if last_billboard is not None:
        #     last_billboard = (torch.from_numpy(last_billboard) / 255.0).permute(2,0,1)[None]
        #     perturbation = last_billboard.float().to(device)
        # else:
        perturbation = (torch.ones(1, *pert_shape)-0.5).float().to(device)

        # perturbation = torch.rand_like(mask_patch[0])[None].to(device)
        # perturbation = torch.ones_like(mask_patch[0])[None].to(device) / 2
        # perturbation = torch.zeros_like(mask_patch[0])[None]
        # perturbation = torch.ones_like(mask_patch[0])[None]
        # perturbation = kornia.filters.max_blur_pool2d(perturbation, 3, ceil_mode=True)
        # perturbation = kornia.filters.median_blur(perturbation, (3, 3))
        # perturbation = kornia.resize(perturbation, pert_shape[1:]).to(device)
        steering_vector_orig = copy.deepcopy(steering_vector)
        # perturbations = []
        for i in range(iterations):
            perturbation = perturbation.detach()
            perturbation.requires_grad = True
            # blurred_pert = perturbation
            # can try blurring it to get smoother images (not so noisy)
            # gauss = kornia.filters.GaussianBlur2d((5, 5), (5.5, 5.5))
            # blurred_pert = gauss(perturbation)
            # blurred_pert = kornia.filters.blur_pool2d(perturbation, 3)
            # blurred_pert = kornia.filters.max_blur_pool2d(perturbation, 3, ceil_mode=True)
            # blurred_pert = kornia.filters.median_blur(perturbation, (3, 3))
            # blurred_pert = kornia.filters.median_blur(perturbation, (10, 10))
            # blurred_pert = kornia.filters.box_blur(perturbation, (3, 3))
            # blurred_pert = kornia.filters.box_blur(perturbation, (5, 5))
            # blurred_pert = kornia.resize(blurred_pert, perturbation.shape[2:])

            imgs = next(iter(data_loader)).to(device)
            y_orig = model(imgs)
            perturbation_warp = kornia.warp_perspective(
                torch.vstack([perturbation for _ in range(len(perspective_transforms))]),
                perspective_transforms,
                dsize=orig_shape,
                mode="nearest",
                align_corners=True
            )
            warp_masks = kornia.warp_perspective(
                mask_patch, perspective_transforms, dsize=orig_shape,
                mode="nearest",
                align_corners=True
            )
            # warp_masks = kornia.resize(warp_masks, shape)
            # perturbation_warp = kornia.resize(perturbation_warp, shape)
            imgs = imgs * (1 - warp_masks)
            imgs += perturbation_warp * warp_masks
            #blurred_imgs = kornia.filters.median_blur(imgs, (3, 3))
            imgs = torch.clamp(imgs + torch.randn(*imgs.shape).to(device) / noise_level, 0, 1)
            if input_divers:
                imgs, steering_vector = self.input_diversification(imgs, steering_vector_orig, device)
                # imgs, steering_vector = self.resizing_noise2(imgs, steering_vector_orig, colorseg_arr, device)
                # temp = imgs.detach().cpu()
                # save_image(
                #     torch.from_numpy(np.asarray(temp.cpu())), self.sample_dir + "/" + f"arrows_{calls}_{i}.png"
                # )
            y = model(imgs)
            assert len(steering_vector.shape)
            assert y.shape[0] == steering_vector.shape[0]
            assert y.shape[1] == 1
            if self.direction == "left" and loss_fxn == "MDirE":
                loss = (y.flatten() - steering_vector).mean() # usual loss fxn
            elif self.direction == "right" and loss_fxn == "MDirE":
                loss = -(y.flatten() - steering_vector).mean()
            elif loss_fxn == "MSE":
                loss = F.mse_loss(y.flatten(), steering_vector)
                # loss = F.mse_loss(y, torch.full_like(y, -1))  # left
                # loss = F.mse_loss(y, torch.full_like(y, 1))  # right
            elif loss_fxn == "MAbsE":
                loss = abs(y.flatten() - steering_vector).mean()
                # loss = -F.smooth_l1_loss(y.flatten(), steering_vector, beta=0.1)
                # loss = F.binary_cross_entropy(F.sigmoid(y.flatten()), steering_vector)
            elif loss_fxn == "inv23" and self.direction == "left":
                # decay_factors = 1 / torch.sqrt(torch.Tensor([i for i in range(1,y.flatten().shape[0]+1)])).to(device)
                decay_factors = 1 / torch.pow(torch.Tensor([i for i in range(1, y.flatten().shape[0] + 1)]), 1/10).to(device)
                loss = (decay_factors * (y.flatten() - steering_vector)).mean()
            elif loss_fxn == "inv23" and self.direction == "right":
                # decay_factors = 1 / torch.sqrt(torch.Tensor([i for i in range(1,y.flatten().shape[0]+1)])).to(device)
                decay_factors = 1 / torch.pow(torch.Tensor([i for i in range(1, y.flatten().shape[0] + 1)]), 1/10).to(device)
                loss = -(decay_factors * (y.flatten() - steering_vector)).mean()

            # print(
            #     f"[iteration {i:5d}/{iterations}] loss={loss.item():2.5f} max(angle)={y.max().item():2.5f} min(angle)={y.min().item():2.5f} mean(angle)={y.mean().item():2.5f} median(angle)={torch.median(y).item():2.5f}"
            # )
            loss.backward()

            # can try doing different things for the gradient descent step
            # perturbation = torch.clamp(
            #     perturbation - perturbation.grad, 0, 1
            # )  # simply follow the gradients
            # perturbation = torch.clamp(
            #     perturbation - perturbation.grad / torch.linalg.norm(perturbation.grad), 0, 1
            # )  # try scaling the gradients
            perturbation = torch.clamp(
                perturbation - torch.sign(perturbation.grad) / 100, 0, 1
            )  # a variation of the fast gradient sign method
            # smoothed_gradients = kornia.filters.gaussian_blur2d(
            #     0.5 * perturbation.grad / torch.linalg.norm(perturbation.grad),
            #     (5, 5),
            #     (3, 3),
            # )  # smooth the gradients first to get less noisy billboards
            # NOTE: only use smoothed gradients on large billboards, otherwise billboard will all be same color due to kernel size
            # perturbation = torch.clamp(perturbation - smoothed_gradients, 0, 1)
            model.zero_grad()
        print(
            f"[iteration {i:5d}/{iterations}] loss={loss.item():2.5f} max(angle)={y.max().item():2.5f} min(angle)={y.min().item():2.5f} mean(angle)={y.mean().item():2.5f} median(angle)={torch.median(y).item():2.5f}"
        )
        y = model(imgs)
        y = y.cpu()
        y = y.detach().numpy().reshape((y.shape[0]))
        steering_vector = steering_vector.cpu()

        with torch.no_grad():
            mask_patch = mask_patch.cpu()
            perspective_transforms = perspective_transforms.cpu()
            perturbation = perturbation.detach().cpu()
            bb = np.round(perturbation[-1].permute(1, 2, 0).numpy() * 255).astype(np.uint8)

            imgs = next(iter(data_loader))
            perturbation_warp = kornia.warp_perspective(
                torch.vstack([perturbation for _ in range(len(perspective_transforms))]),
                perspective_transforms,
                dsize=orig_shape,
                mode="nearest",
                align_corners=True
            )
            warp_masks = kornia.warp_perspective(
                mask_patch, perspective_transforms, dsize=orig_shape,
                mode="nearest",
                align_corners=True
            )
            # warp_masks = kornia.resize(warp_masks, shape)
            # perturbation_warp = kornia.resize(perturbation_warp, shape)
            perturbed_imgs = imgs * (1 - warp_masks)
            perturbed_imgs += perturbation_warp * warp_masks

            model = model.cpu()
            imgs = imgs.cpu()
            orig_angles = model(imgs)
            pert_angles = model(perturbed_imgs)
            MAE = (orig_angles - pert_angles).mean()
            # print("Original outputs:", orig_angles)
            # print("Adversarial outputs:", pert_angles)
            # print(f"{MAE=}")
            # arrowed_imgs = []
            # orig_arrowed_imgs = []
            # for img, pert_img, a1, a2 in zip(
            #     imgs, perturbed_imgs, orig_angles, pert_angles
            # ):
            #     img_processed = self.draw_arrows(img.numpy(), a1.numpy())
            #     orig_arrowed_imgs.append(img_processed)
            #     img_processed = self.draw_arrows(pert_img.numpy(), a1.numpy(), a2.numpy())
            #     arrowed_imgs.append(img_processed)
            # save_image(
            #     torch.from_numpy(np.asarray(arrowed_imgs)), self.sample_dir +"/"+ f"arrows_{calls}.png"
            # )
            # save_image(
            #     torch.from_numpy(np.asarray(orig_arrowed_imgs)),
            #     self.sample_dir +"/"+ "arrows_orig.png",
            # )

            return bb, y, MAE


import unittest

from mock import MagicMock

from bounded_contexts.central_console.models.contact_person import ContactPerson

class TestContactPerson(unittest.TestCase):

    def test_instance(self):
        expected_email = MagicMock()
        expected_name = MagicMock()

        actual_user = ContactPerson(expected_email, expected_name)

        self.assertEqual(actual_user.email, expected_email)
        self.assertEqual(actual_user.name, expected_name)

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


from flask import Flask
import redis
import json

from ...service.entity.book import Book
from ...exception.exception import BookAlreadyExistsException

app = Flask(__name__)

BOOK_COUNTER = "book_counter"
BOOK_ID_PREFIX = "book_"

class BookRepository:

    def __init__(self):
        self.db = redis.Redis(host = "redis", port = 6379, decode_responses = True)
        if self.db.get(BOOK_COUNTER) == None:
            self.db.set(BOOK_COUNTER, 0)

    def save(self, book_req):
        app.logger.debug("Saving new book: {0}.".format(book_req))
        book = self.find_book_by_title(book_req.title)

        if book != None:
            raise BookAlreadyExistsException("Book title \"{0}\" already exist.".format(book_req.title))

        book = Book(self.db.incr(BOOK_COUNTER), book_req.author_id, book_req.title, book_req.year)

        book_id = BOOK_ID_PREFIX + str(book.id)
        book_json = json.dumps(book.__dict__)

        self.db.set(book_id, book_json)

        app.logger.debug("Saved new book: (id: {0}).".format(book.id))
        return book.id

    def find_book_by_title(self, title):
        n = int(self.db.get(BOOK_COUNTER))

        for i in range(1, n + 1):
            book_id = BOOK_ID_PREFIX + str(i)

            if not self.db.exists(book_id):
                continue

            book_json = self.db.get(book_id)
            book = Book.from_json(json.loads(book_json))

            if book.title == title:
                return book

        return None

    def count_all(self):
        app.logger.debug("Starting counting all books")
        n = int(self.db.get(BOOK_COUNTER))

        n_of_books = 0

        for i in range(1, n + 1):
            book_id = BOOK_ID_PREFIX + str(i)

            if self.db.exists(book_id):
                n_of_books += 1

        app.logger.debug("Counted all books (n: {0})".format(n_of_books))
        return n_of_books

    def find_n_books(self, start, limit):
        app.logger.debug("Finding n of books (start: {0}, limit: {1}".format(start, limit))
        n = int(self.db.get(BOOK_COUNTER))

        books = []
        counter = 1

        for i in range(1, n + 1):
            book_id = BOOK_ID_PREFIX + str(i)

            if not self.db.exists(book_id):
                continue

            if counter < start:
                counter += 1
                continue

            book_json = self.db.get(book_id)
            book = Book.from_json(json.loads(book_json))
            books.append(book)

            if len(books) >= limit:
                break

        app.logger.debug("Found {0} books.".format(len(books)))
        return books


import adal

from msrest.authentication import Authentication

from azure.cli._util import CLIError

class AdalAuthentication(Authentication):#pylint: disable=too-few-public-methods

    def __init__(self, token_retriever):
        self._token_retriever = token_retriever

    def signed_session(self):
        session = super(AdalAuthentication, self).signed_session()

        try:
            scheme, token = self._token_retriever()
        except adal.AdalError as err:
            raise CLIError(err)

        header = "{} {}".format(scheme, token)
        session.headers['Authorization'] = header
        return session


import time

import numpy as np
import plotly.offline
from threading import Thread
from threading import Lock
import pandas as pd
from bs4 import BeautifulSoup
import re
import datetime
import warnings
import plotly.graph_objects as go
import requests
import yfinance as yf
from requests.exceptions import ConnectionError
from datetime import timedelta, datetime
from time import perf_counter
warnings.simplefilter(action='ignore', category=FutureWarning)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.reset_option('all')

def loop_fig(fig, df, x, y, y0, name, ColorIncremnt):
    size = 12
    pixel = 0.01
    lock = Lock()
    lock.acquire()
    opacity = 1
    #if ColorIncremnt != 1:

    if ColorIncremnt != 1:
        first = ['#ffffff', '#90ee90', 'rgb(116,195,101)', '#50c878']
        second = ['#ffffff', 'rgb(20,119,31)', 'rgb(19,171,16)', 'rgb(36,197,55)']
        three = ['#ffffff', 'rgb(19,170,82)', 'rgb(0,102,43)', 'rgb(0,100,0)']
        four = ['#ffffff', 'rgb(24,80,44)', 'rgb(24,80,44)', 'rgb(11,47,24)']
    else:
        first = ['rgb(234,60,83)', 'rgb(235, 179, 177, 1)', 'red', 'rgb(191,10,47)']
        second = ['rgba(163,90,82)', 'rgb(235,60,83)', 'rgb(210,35,18)', 'rgb(210,31,60)']
        three = ['#ffffff', 'rgb(210,31,60)', 'rgb(126,25,27)', 'rgb(210,31,60)']
        four = ['#ffffff', 'rgb(214,33,33)', 'rgb(199,44,44)', 'rgb(184,15,10)']

#green
    #     first = ['#ffffff', '#90ee90', 'rgb(116,195,101)', '#50c878']
    #     second = ['#ffffff', 'rgb(20,119,31)', 'rgb(19,171,16)', 'rgb(36,197,55)']
    #     three = ['#ffffff', 'rgb(19,170,82)', 'rgb(0,102,43)', 'rgb(0,100,0)']
    #     four = ['#ffffff', 'rgb(24,80,44)', 'rgb(24,80,44)', 'rgb(11,47,24)']
    #
    # else: #red
    #     first = ['rgb(234,60,83)', 'rgb(235, 179, 177, 1)', 'red', 'rgb(191,10,47)']
    #     second = ['rgba(163,90,82)', 'rgb(235,60,83)', 'rgb(210,35,18)', 'rgb(210,31,60)']
    #     three = ['#ffffff', 'rgb(210,31,60)', 'rgb(126,25,27)', 'rgb(210,31,60)']
    #     four = ['#ffffff', 'rgb(214,33,33)', 'rgb(199,44,44)', 'rgb(184,15,10)']

    if pixel < 0.02:
        ColorIncremnt = first
    lock.release()

    for i in range(len(df)):
        n = go.Scatter(x=[x[i]], y=[y[i]], y0=y0, mode='markers', showlegend=False, name=name, marker=
        dict(size=size, color=ColorIncremnt[3], symbol='circle', opacity=opacity,
             gradient=dict(color=[ColorIncremnt[1], ColorIncremnt[2]], type='radial'),
             line=dict(width=1, color=ColorIncremnt[0])))

        if pixel <= (len(df) * 0.10):
            ColorIncremnt = first

            size = 16.2
            # print('number 10',pixel)
            opacity = 1

        elif pixel <= (len(df) * 0.2):
            size = 19.1
            ColorIncremnt = first

            opacity = 0.9
            # print('number 9', pixel)
        elif pixel <= (len(df) * 0.3):
            size = 21.9
            ColorIncremnt = first

            opacity = 0.8

        elif pixel <= (len(df) * 0.4):
            size = 25.85
            ColorIncremnt = second

            opacity = 0.7

        elif pixel <= (len(df) * 0.5):
            size = 29.45
            ColorIncremnt = second

            opacity = 0.6
        elif pixel <= (len(df) * 0.6):
            size = 33.43
            ColorIncremnt = second

            opacity = 0.5
        elif pixel <= (len(df) * 0.7):
            ColorIncremnt = three

            size = 36.6
            opacity = 0.51

        elif pixel <= len(df) * 0.8:
            size = 39
            ColorIncremnt = three

            opacity = 0.55
        elif pixel < (len(df) * 0.9):
            size = 50.7
            ColorIncremnt = four

            opacity = 0.58

        else:
            ColorIncremnt = four
            size = 53
            opacity = 0.55
        lock.acquire()
        pixel += 1
        lock.release()
        fig.add_trace(n)

def short_sala_data(ticker):
    try:
        start = time.time()
        print(f'start short sale data{start}')
        url = f'https://www.benzinga.com/quote/{ticker}/short-interest'
        oururl = requests.get(url, 'html.parsel')
        soup = BeautifulSoup(oururl.content, 'lxml')
        GenralList = []
        for script in soup("script"):
            GenralList.append(script.extract().text)
        extract = re.compile('{([^}]*)}')
        match = re.findall(extract, GenralList[-1])
        firstList = []
        for second in match:
            k = re.sub(pattern='"', repl='', string=second)
            firstList.append(k)

        firstStop = time.time()
        print(f'until first stop in shor_sale_data {firstStop-start} sec')
        secondList = []
        for second in firstList:
            if second.startswith("re"):
                if second not in secondList:
                    secondList.append(second)

        secondStop = time.time()
        print(f'until second stop in short sale data {secondStop-start}')
        shortPattern = re.compile(r'totalShortInterest:\d+')
        preforemnce52weekPattern = re.compile(r'performance52Wk:(\d+|-\d+|\d+.\d+|-\d+.\d+)')
        shareFloatPatter = re.compile(r'sharesFloat:\d+')
        datePattern = re.compile(r'recordDate:\d+-\d+-\d+')
        lastMonthShortPattrtn = re.compile(r'shortPriorMo:\d+')
        dailyVolPattern = re.compile(r'averageDailyVolume:\d+')
        daytoCoverPattern = re.compile(r'daysToCover:\d+.\d+')

        NumShort = re.compile(r'\d+')
        NumDate = re.compile(r'\d+-\d+-\d+')
        NumDayToCover = re.compile(r'\d+.\d+')
        NumVol = re.compile(r'\d+')
        NunLastMonthShort = re.compile('\d+')

        NumShareFloat = re.compile('\d+')
        Num52week = re.compile('(\d+|-\d+|\d+.\d+|-\d+.\d+)')
        allData = {'record Date': [], 'Short Interst': [], "Day To Cover": [], 'Avg Daily Vol': [], "last Month Short": [],
                   'Share Float': [], '52Weeks': []}

        for i in range(len(secondList)):
            shortInterst = re.findall(pattern=shortPattern, string=secondList[i])
            recordDate = re.findall(pattern=datePattern, string=secondList[i])
            dayToCover = re.findall(pattern=daytoCoverPattern, string=secondList[i])
            avgDailyVol = re.findall(pattern=dailyVolPattern, string=secondList[i])
            lastMonthShort = re.findall(pattern=lastMonthShortPattrtn, string=secondList[i])
            Preforemnce52Weeks = re.findall(pattern=preforemnce52weekPattern, string=secondList[i])
            ShareFloat = re.findall(pattern=shareFloatPatter, string=secondList[i])

            try:
                record = re.findall(pattern=NumDate, string=recordDate[0])
                #if str(record[0])  not in allData['record Date']:

                short = re.findall(pattern=NumShort, string=shortInterst[0])
                allData['Short Interst'].append(int(short[0]))

                day_to_cover = re.findall(pattern=NumDayToCover, string=dayToCover[0])

                allData['Day To Cover'].append(float(day_to_cover[0]))

                avgDay = re.findall(pattern=NumVol, string=avgDailyVol[0])
                allData['Avg Daily Vol'].append(str(avgDay[0]))

                lastshort = re.findall(pattern=NunLastMonthShort, string=lastMonthShort[0])
                allData['last Month Short'].append(int(lastshort[0]))

                try:
                    share = re.findall(pattern=NumShareFloat, string=ShareFloat[-1])
                except TypeError:
                    share = re.findall(pattern=NumShareFloat, string=ShareFloat[0])

                last52 = re.findall(pattern=Num52week, string=Preforemnce52Weeks[0])

                allData['52Weeks'].append(f'{int(last52[0])} %')
                allData['Share Float'].append(int(share[0]))

            except IndexError:
                pass
        thirdStop = time.time()
        print(f'until third stop in short sale func {thirdStop-start}')
        df = pd.DataFrame()
        try:
            df = pd.DataFrame(allData)

        except ValueError:

            try:
                df = pd.DataFrame({'record': allData['record Date'], 'short': allData['Short Interst'],
                                   'day to cove': allData['Day To Cover'], 'last month': allData['last Month Short']})
            except ValueError:
                try:
                    df = pd.DataFrame({'record': allData['record Date'], 'short': allData['Short Interst'],
                                       'day to cover': allData['Day To Cover']})
                except ValueError:
                    try:
                        df = pd.DataFrame({'record': allData['record Date'], 'short': allData['Short Interst']})

                        df1 =  pd.DataFrame({'day to cove': allData['Day To Cover'], 'last month': allData['last Month Short'],"Share Floats":[]})
                    except:
                        pass

        end = time.time()
        print(f'inish short sale data in {end-start}')
        if not df.empty:
            print('return 1')
            return df,allData
        else:
            print('return two')
            return allData,'2'

    except ConnectionError:
        return False

if __name__  == '__main__':
    short_sala_data('NVDA')


import json

from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.html import strip_tags

from tomato.models import ClientLogEntry

class Command(BaseCommand):
    help = "Update constants file with latest data from server"
    CONSTANTS_FILE_PATH = settings.PROJECT_DIR / "constants.json"

    def add_arguments(self, parser):
        parser.add_argument("-b", "--bump-protocol-version", action="store_true", help="Bump protocol version.")

    def handle(self, *args, **options):
        with open(self.CONSTANTS_FILE_PATH, "r") as file:
            constants = json.load(file)

        if options["bump_protocol_version"]:
            constants["protocol_version"] += 1

        constants.update(
            {
                "client_log_entry_types": sorted(ClientLogEntry.Type.values),
                "settings_descriptions": {
                    key: strip_tags(value[1].replace("<br>", " "))
                    for key, value in settings.CONSTANCE_CONFIG.items()
                    if key not in settings.CONSTANCE_SERVER_ONLY_SETTINGS
                },
            }
        )

        with open(self.CONSTANTS_FILE_PATH, "w") as file:
            json.dump(constants, file, indent=2, sort_keys=True)
            file.write("\n")
        self.stdout.write(self.style.SUCCESS("Successfully wrote constants.json file!"))