#!/usr/bin/python3

import sys
from telnetlib import FORWARD_X
import tty
import termios
from enum import IntEnum, unique
from turtle import forward
import RPi.GPIO as GPIO
from motor import Motor, MotorDirection
from sensors import HCSR04, HCSR04Event
from controller import PIDController, IntersectionType
from pins import RaspiPin, RaspiPinError
import time

class Droid:
    def __init__(self, battery_voltage=9.0, motor_voltage=6.0, warn=True,
                 speed=0.5):
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(warn)
        self.speed = speed
        self.motors = Motor(battery_voltage, motor_voltage)

    def __del__(self):
        self._cleanup()

    def _set_led1(self, state):
        GPIO.output(RaspiPin.LED1_PIN, state)

    def _set_led2(self, state):
        GPIO.output(RaspiPin.LED2_PIN, state)

    def _set_oc1(self, state):
        GPIO.output(RaspiPin.OC1_PIN, state)

    def _set_oc2(self, state):
        GPIO.output(RaspiPin.OC2_PIN, state)

    def _cleanup(self):
        GPIO.cleanup()

    def forward(self):
        self.motors.forward(speed=self.speed)

    def backward(self):
        self.motors.backward(speed=self.speed)

    def stop(self):
        self.motors.stop()

    def right(self):
        self.motors.right(speed=self.speed)

    def left(self):
        self.motors.left(speed=self.speed)

class RoverKey(IntEnum):
    UP = 0
    DOWN = 1
    RIGHT = 2
    LEFT = 3
    STOP = 4

class RoverDroid(Droid):
    def __init__(self, battery_voltage=9.0, motor_voltage=6.0, warn=True,
                 speed=0.5):
        Droid.__init__(self, battery_voltage, motor_voltage, warn, speed)

    def _read_char(self):
        """Read chars from keyboard"""
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        if ch == '0x03':
            raise KeyboardInterrupt
        return ch

    def _read_key(self):
        """Converts keys from keyboard to directions to the droid"""
        c1 = self._read_char()
        if ord(c1) != 0x1b:
            # escape char
            return RoverKey.STOP
        c2 = self._read_char()
        if ord(c2) != 0x5b:
            # [ char
            raise KeyboardInterrupt
        c3 = self._read_char()
        return RoverKey(ord(c3) - 65)  # 0=Up, 1=Down, 2=Right, 3=Left arrows

    def run(self):
        """Continuously read keys stroke from the keyboard and applies
        directions to current droid"""
        try:
            print("Use the arrow keys to move the robot")
            print("Press CTRL-c to quit the program")
            while True:
                key = self._read_key()
                if key is RoverKey.UP:
                    print("ROVER", "FORWARD")
                    self.forward()
                elif key is RoverKey.DOWN:
                    print("ROVER", "BACKWARD")
                    self.backward()
                elif key is RoverKey.LEFT:
                    print("ROVER", "TURN LEFT")
                    self.left()
                elif key is RoverKey.RIGHT:
                    print("ROVER", "TURN RIGHT")
                    self.right()
                elif key is RoverKey.STOP:
                    print("ROVER", "STOP")
                    self.stop()
                else:
                    print("ROVER", "UNKNOWN VALUE:", key)
                    raise KeyboardInterrupt
        except KeyboardInterrupt:
            self._cleanup()

class Droid3Sensors(Droid):
    """Class to explore a maze with a controller to drives motors according to
    sensors"""

    def __init__(self, battery_voltage=9.0, motor_voltage=6.0, warn=True,
                 speed=0.5, low=15., high=50., target=20., KP=0.005, KD=0.0002,
                 KI=0.0, sample_time=0.001):
        Droid.__init__(self, battery_voltage, motor_voltage, warn, speed)
        print("Robot 3 Sensors with controller", "speed=%02f" % speed)
        self.left_sensor = HCSR04("LEFT", RaspiPin.ECHO_PIN_3,
                                  RaspiPin.TRIGGER_PIN_3, low=low, high=high)
        self.front_sensor = HCSR04("FRONT", RaspiPin.ECHO_PIN_1,
                                   RaspiPin.TRIGGER_PIN_1, low=2*low, high=high)
        self.right_sensor = HCSR04("RIGHT", RaspiPin.ECHO_PIN_2,
                                   RaspiPin.TRIGGER_PIN_2, low=low, high=high)
        self.controller = PIDController(self.motors, self.left_sensor,
                                        self.front_sensor, self.right_sensor,
                                        speed=speed, target=target, KP=KP,
                                        KD=KD, KI=KI, sample_time=sample_time)

    def explore(self):
        """Explores maze until the exit is found"""
        while True:
            """front_sensor_event, distance = self.controller.get_front_distance()
            right_sensor_event, distance = self.controller.get_right_distance()
            left_sensor_event, distance = self.controller.get_left_distance()

            if front_sensor_event is HCSR04Event.DANGER:
                print("DANGER in front! Turn!")
                self.right()
            else:
                self.forward()

            if right_sensor_event is HCSR04Event.DANGER:
                print("DANGER at right! STOP!")
                self.stop()
            elif right_sensor_event is HCSR04Event.WALL:
                print("WALL DETECTED !")

            if left_sensor_event is HCSR04Event.DANGER:
                print("DANGER at right! STOP!")
                self.stop()"""

            front_sensor_event, front_distance = self.controller.get_front_distance()
            right_sensor_event, right_distance = self.controller.get_right_distance()
            left_sensor_event, left_distance = self.controller.get_left_distance()

            print("front sensor event: " + str(front_sensor_event)
                + " front_distance: " + str(front_distance)
                + "\nright_sensor_event: " + str(right_sensor_event)
                + " right_distance: " + str(right_distance)
                + "\nleft_sensor_event: " + str(left_sensor_event)
                + " left_distance: " + str(left_distance))

            if self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathFour:
                print("DEHORS")
                self.motors.stop()
                self.controller.passPathThreeRightFront()
            elif self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathThreeLeftFront:
                print("Allez tout droit")
                self.controller.passPathThreeLeftFront()
            elif self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathThreeRightFront:
                print("Allez à droite")
                self.controller.passPathThreeRightFront()
            elif self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathThreeLeftRight:
                print("Allez à droite")
                self.controller.passPathThreeLeftRight()
                while (self.controller.get_right_distance()[1] is HCSR04Event.NOTHING):
                    self.motors.forward()
            elif self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathTwoLeft:
                print("Allez à gauche")
                self.controller.passPathTwoLeft()
            elif self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathTwoRight:
                print("Allez a droite")
                self.controller.passPathTwoRight()
            elif self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathTwoFront:
                print("C'est un couloir, allez tout droit")
                print("Going to the next intersection")
                self.controller.go_to_the_next_intersection()
            elif self.controller.get_intersection_type(left_sensor_event, front_sensor_event, right_sensor_event) is IntersectionType.PathOne:
                print("Cul de sac, Demi tour")
                self.controller.passDeadEnd()
            else:
                print("Erreur, stop")


from consumible import views
from django.urls import path

urlpatterns = [
    path('lista_consumible/', views.ConsumibleListView.as_view() , name="Listado-Consumible"),
    path('detalle_consumible/<int:pk>/', views.ConsumibleDetailView.as_view(), name="Detalle-Consumible"),
    path('crear_consumible/', views.CreateConsumibleView.as_view(), name="Crear-Consumible"),
    path('editar_consumible/<int:pk>/', views.ConsumibleEditView.as_view(), name="Editar-Consumible"),
    path('eliminar_consumible/<int:pk>/', views.DeleteConsumibleView.as_view(), name="Eliminar-Consumible"),
]


# Testing viewing the products
# as well as the adding, editing, and deleting that the superuser is able to do
from django.test import TestCase
from models import Product, Category
from forms import ProductForm

# Create your tests here.

def create_test_prod():
    cat = Category(name='sale', friendly_name='sale')
    cat.save()
    product = Product(
        category=cat,
        sku='111111',
        name='test',
        description='Test Description',
        price='10.99',
        image='img.jpg',
        )

    # save product

    product.save()
    return (product, cat)

class ProductsViewsTest(TestCase):

    """
    Test main products view returns properly
    """

    def test_get_products_page(self):
        response = self.client.get('/products/')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/products.html')

    def test_get_one_product_page(self):
        (product, cat) = create_test_prod()
        response = self.client.get('/products/' + str(product.id))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'products/one_product.html')

class TestProductModel(TestCase):

    """
    Test the Product Model is working
    """

    def test_product_model(self):

        # create a product

        (product, cat) = create_test_prod()

        # check to see that the product fields equal the saved products values

        self.assertEqual(product.category, cat)
        self.assertEqual(product.sku, '111111')
        self.assertEqual(product.name, 'test')
        self.assertEqual(product.description, 'Test Description')
        self.assertEqual(product.price, '10.99')
        self.assertEqual(product.image, 'img.jpg')

class ProductFormTest(TestCase):

    """
    Check that validation works when a user doesn't fill in at
    least one field, all fields are required, tests forms.py
    """

    def test_product_form(self):
        form = ProductForm({
            'category': 'sale',
            'sku': '111111',
            'name': 'test',
            'description': 'Test Description',
            'price': '10',
            })
        self.assertFalse(form.is_valid())

    def test_product_name_is_required(self):
        form = ProductForm({'name': ''})
        self.assertFalse(form.is_valid())
        self.assertIn('name', form.errors.keys())
        self.assertEqual(form.errors['name'][0],
                         'This field is required.')

    def test_product_description_is_required(self):
        form = ProductForm({'description': ''})
        self.assertFalse(form.is_valid())
        self.assertIn('description', form.errors.keys())
        self.assertEqual(form.errors['description'][0],
                         'This field is required.')

    def test_product_price_is_required(self):
        form = ProductForm({'price': ''})
        self.assertFalse(form.is_valid())
        self.assertIn('price', form.errors.keys())
        self.assertEqual(form.errors['price'][0],
                         'This field is required.')


import os
from rest_framework import serializers
from rest_framework.exceptions import AuthenticationFailed
from . import facebook, google
from .register import register_social_user
from ..users.serializers import UserSerializer

class FacebookSocialAuthSerializer(serializers.Serializer):
    """Handles serialization of facebook related data"""
    auth_token = serializers.CharField()

    def validate_auth_token(self, auth_token):
        user_data = facebook.Facebook.validate(auth_token)
        try:
            if not user_data.get('email'):
                raise serializers.ValidationError('Invalid email address. Please login again.')

            data = {
                'user_id': user_data.get('id'),
                'email': user_data.get('email'),
                'first_name': user_data.get('first_name'),
                'last_name': user_data.get('last_name'),
                'dob': user_data.get('birthday'),
                'provider': 'facebook'
            }
            user_data = register_social_user(data=data)

            return {'user': UserSerializer(instance=user_data.get('user')).data, 'token': user_data.get('token')}

        except Exception as identifier:
            raise serializers.ValidationError(
                'The token is invalid or you have already registered. Please login again.')

class GoogleSocialAuthSerializer(serializers.Serializer):
    """Handles serialization of google related data"""
    auth_token = serializers.CharField()

    def validate_auth_token(self, auth_token):
        user_data = google.Google.validate(auth_token)
        try:
            if user_data['aud'] != os.environ.get('GOOGLE_CLIENT_ID'):
                serializers.ValidationError('The token is invalid or expired. Please login again.')
            if not user_data.get('email'):
                raise serializers.ValidationError('Invalid email address. Please login again.')

            data = {
                'user_id': user_data.get('sub'),
                'email': user_data.get('email'),
                'first_name': user_data.get('given_name'),
                'last_name': user_data.get('family_name'),
                'dob': user_data.get('birthday'),
                'provider': 'google'
            }
            user_data = register_social_user(data=data)

            return {'user': UserSerializer(instance=user_data.get('user')).data, 'token': user_data.get('token')}

        except Exception as identifier:
            raise serializers.ValidationError('The token is invalid or expired. Please login again.')


import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import solve_ivp
from scipy.optimize import fsolve

# setting up the problem

# defining Boundary conditions
y0 = 0.0  # initial position
yL = 0.0  # final position

# defining time interval
x0 = 0.0
xE = 1.0
xeval = np.linspace(x0, xE, 100)

# defining function for system of ODEs
def derivs(x, s):
    """
    Function for the system of ODEs
    dy/dt = v
    dv/dt = y^2 + cos(2*pi*x) - sin^4(pi*x)

    Parameters
    ----------
    x : position (float)
    s : state vector  (float)
        s[0] = y(x)
        s[1] = dy/dx == v(x)
    """
    y = s[0]
    v = s[1]

    dvdx = y**2 + np.cos(2*np.pi*x) - (np.sin(np.pi*x))**4
    return [v, dvdx]

# getting accurate dy/dx using root finding approach

# defining objective function as a function of guess derivative

def objective(v):
    sol = solve_ivp(derivs, [x0, xE], np.array(
        [y0, v], dtype='object'), t_eval=xeval)
    end_point = sol.y[0][-1]
    return end_point - yL

vRoot = fsolve(objective, -1)
print("Optimal dy/dx: %2.3f" % vRoot)

# obtaining solution for vRoot
sol = solve_ivp(derivs, [x0, xE], np.array(
    [y0, vRoot], dtype='object'), t_eval=xeval)

# plotting the solution
plt.figure(figsize=(10, 5.5), dpi=100)
plt.plot(xeval, sol.y[0], color='blue', label="y(x): $y\'(x=0)$=%2.3f" % vRoot)
# plotting the boundary conditions
plt.plot([x0, xE], [y0, y0], 'ro', ms=10, label='Boundary Conditions')
plt.grid(alpha=0.2)
plt.legend(loc=1)
plt.xlabel('x', fontsize=16)
plt.ylabel('y', fontsize=16)
plt.title(
    'Solution for $\\dfrac{d^2y}{dt^2} = y^2 +\cos(2\pi x) - \sin^4(\pi x)$\n')
plt.savefig('Assignment3.jpg', bbox_inches='tight', dpi=300)
plt.show()


# ---
# jupyter:
#   jupytext:
#     formats: ipynb,py
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.3.3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %reload_ext autoreload
# %autoreload 2
# %matplotlib inline

# +
import torch
from torchvision import models
import random
import os
import numpy as np
import pandas as pd
from fastai.vision import ImageList, transform
from functools import partial

from hw_grapheme.densenet import Dnet_1ch

from hw_grapheme.dl_utils.radam import Over9000
from hw_grapheme.loss_func import Loss_combine
from hw_grapheme.callbacks import Metric_idx, Metric_tot, MixUpCallback

from hw_grapheme.dl_utils.csvlogger import CSVLogger
from fastai.basic_train import Learner

from torch import nn
from fastai.callbacks import SaveModelCallback

# import fastai
# import warnings

# import matplotlib.pyplot as plt
# import torch.nn.functional as F

# from sklearn.model_selection import KFold
# from fastai.callbacks import SaveModelCallback, TrackerCallback
# from fastai.callback import Callback
# from torch import nn, Tensor

# from hw_grapheme.dl_utils.radam import RAdam, Over9000
# from hw_grapheme.dl_utils.mish_activation import Mish, to_Mish

# warnings.filterwarnings("ignore")

# fastai.__version__

# from functools import partial
#

# +
sz = 128
bs = 128
nfolds = 4 #keep the same split as the initial dataset
fold = 1
SEED = 2019
TRAIN = '../data/processed_data/train_images/'
LABELS = '../data/train.csv'

def seed_everything(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = True

seed_everything(SEED)
# -

df = pd.read_csv(LABELS)
nunique = list(df.nunique())[1:-1]
print(nunique)
df.head()

range(fold*len(df)//nfolds,(fold+1)*len(df)//nfolds)

# +
stats = ([0.0692], [0.2051])
data = (ImageList.from_df(df, path='.', folder=TRAIN, suffix='.png',
        cols='image_id', convert_mode='L')
        .split_by_idx(range(fold*len(df)//nfolds,(fold+1)*len(df)//nfolds))
        .label_from_df(cols=['grapheme_root','vowel_diacritic','consonant_diacritic'])
        .transform(transform.get_transforms(do_flip=False,max_warp=0.1), size=sz, padding_mode='zeros')
        .databunch(bs=bs)).normalize(stats)

# data.show_batch()
# -

data

# +
Metric_grapheme = partial(Metric_idx,0)
Metric_vowel = partial(Metric_idx,1)
Metric_consonant = partial(Metric_idx,2)

arch = models.densenet121
model = Dnet_1ch(arch, nunique)

learn = Learner(data, model, loss_func=Loss_combine(), opt_func=Over9000,
        metrics=[Metric_grapheme(),Metric_vowel(),Metric_consonant(),Metric_tot()])

learn.model = nn.DataParallel(learn.model)

logger = CSVLogger(learn,f'log{fold}')
learn.clip_grad = 1.0
learn.split([model.head1])
learn.unfreeze()
# -

learn.summary()

learn.fit_one_cycle(100, max_lr=slice(0.2e-2,1e-2), wd=[1e-3,0.1e-1], pct_start=0.0,
    div_factor=100, callbacks = [logger, SaveModelCallback(learn,monitor='metric_tot',
    mode='max',name=f'model_{fold}'),MixUpCallback(learn)])
#metrics: Metric_grapheme, Metric_vowel, Metric_consonant, Metric_tot (competition metric)


# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

def solve(N, K, A):
    if K > 50:
        print(" ".join([str(N) for i in range(N)]))
        sys.exit()

    for i in range(K):
        A_new = [0 for i in range(N)]
        imos_table = [0 for i in range(N)]

        for j in range(N): # ここで一度状態の差分についてをimos_tableに保存
            left = max(0, j - A[j])
            right = j + A[j] + 1
            imos_table[left] += 1
            if right >= N:
                pass
            else:
                imos_table[right] -= 1

        # tmp を使って前から順に状態を計算していく
        tmp = 0
        for j in range(N):
            tmp += imos_table[j]
            A_new[j] += tmp

        A = copy.deepcopy(A_new)

    print(" ".join(map(str, A)))

def main():
    N, K = LI()
    A = LI()
    # N = 2 * 10 ** 5
    # K = 41
    # A = [0 for i in range(2* 10 ** 5)]
    solve(N, K, A)

if __name__ == "__main__":
    main()


import time
from datetime import datetime, timedelta
from math import atan2, cos, radians, sin, sqrt
from typing import Dict, List, Optional, Tuple, TypeVar, Union

import openrouteservice as ors  # type: ignore
import pytz
import requests
from django.conf import settings
from django.contrib.gis.geos.point import Point
from django.core.management.base import BaseCommand, CommandError

from auto.models import AutoRide, GPSData, Vehicle

Point_ = TypeVar("Point_", bound=Union[List[float], Tuple[float, float]])

class Command(BaseCommand):
    help = "Запускает движение авто по заданному маршруту между 2 точками"
    WRITE_TIME_OPTION = 60

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.client = ors.Client(key=settings.OPENROUTESERVISE_API)
        self.current_speed: int = 0
        self.timestamp: datetime = datetime.now(tz=pytz.UTC)

    def add_arguments(self, parser) -> None:
        parser.add_argument("vehicle_id", type=int, help="ID автомобиля")
        parser.add_argument("start_address", type=str, help="Начальный адрес поездки")
        parser.add_argument("end_address", type=str, help="Конечный адрес поездки")
        parser.add_argument("vehicle_speed", type=int, help="Скорость автомобиля")
        parser.add_argument("vehicle_acceleration", type=int, help="Ускорение автомобиля")
        parser.add_argument("in_real_time", type=bool, help="В реальном времени")
        parser.add_argument("--yes", action="store_true")
        parser.add_argument("--no", dest="in_real_time", action="store_false")
        parser.set_defaults(in_real_time=False)

    def handle(self, *args, **options) -> None:
        self.vehicle_id = options["vehicle_id"]
        self.start_address = options["start_address"]
        self.end_address = options["end_address"]
        self.vehicle_speed = options["vehicle_speed"]
        self.vehicle_acceleration = options["vehicle_acceleration"]
        self.in_real_time = options.get("in_real_time")

        enterprise = Vehicle.objects.get(pk=self.vehicle_id).enterprise
        if enterprise:
            self.tzinfo = pytz.timezone(enterprise.timezone)
        else:
            self.tzinfo = pytz.UTC
        self.start_time = self.timestamp

        start_point = self.get_coordinates_from_address(self.start_address)
        if start_point is None:
            CommandError("Начальный адрес не корректный")
            return

        end_point = self.get_coordinates_from_address(self.end_address)
        if end_point is None:
            CommandError("Конечный адрес не корректный")
            return

        this_ride = AutoRide(
            vehicle_id=self.vehicle_id,
            start_date=self.timestamp,
            end_date=None,
            start_point=Point(*start_point),
            end_point=None,
            distance=0,
        )
        this_ride.save()

        self.stdout.write(
            self.style.SUCCESS(f"{self.timestamp} - Начата поездка из {self.start_address} в {self.end_address}")
        )
        coordinates = self.get_track(start_point, end_point)

        # MAIN LOOP
        i = 0
        distance_p1_p2 = None
        total_distance = 0

        while i < len(coordinates) - 1:
            point_1 = coordinates[i]
            point_2 = coordinates[i + 1]

            if self.in_real_time:
                time.sleep(self.WRITE_TIME_OPTION)
            new_speed = self.WRITE_TIME_OPTION * self.vehicle_acceleration

            if new_speed > self.vehicle_speed:
                self.current_speed = self.vehicle_speed
                self.vehicle_acceleration = 0
            else:
                self.current_speed = new_speed

            vehicle_dist: int = (
                self.current_speed * self.WRITE_TIME_OPTION
                + self.vehicle_acceleration * (self.WRITE_TIME_OPTION**2) // 2
            )
            total_distance += vehicle_dist
            print(self.current_speed, total_distance)
            return

            if not distance_p1_p2:
                distance_p1_p2 = self.distance_between_coordinates(point_1, point_2)

            if vehicle_dist < distance_p1_p2:
                distance_p1_p2 = distance_p1_p2 - vehicle_dist
                t: float = vehicle_dist / distance_p1_p2
                self.write_point_to_base(point_1, point_2, t)
                self.timestamp += timedelta(seconds=self.WRITE_TIME_OPTION)
                continue
            else:
                distance_p1_p2 = vehicle_dist - distance_p1_p2
                distance_p1_p2 = None
                i += 1
                continue

        this_ride.end_point = Point(end_point)
        this_ride.end_date = self.timestamp
        this_ride.distance = total_distance
        this_ride.save()

    def get_coordinates_from_address(self, address: str) -> Optional[Point_]:
        endpoint_search = "https://api.openrouteservice.org/geocode/search"
        params: Dict[str, Union[int, str]] = {
            "api_key": settings.OPENROUTESERVISE_API,
            "text": address,
            "size": 1,
            "lang": "ru",
        }
        response = requests.get(endpoint_search, params=params)
        result = response.json()["features"][0]["geometry"]["coordinates"]
        if result:
            return result
        return None

    def get_track(
        self,
        start_point: Point_,
        end_point: Point_,
    ) -> List[Point_]:
        route = self.client.directions(
            coordinates=([start_point] + [end_point]),
            profile="driving-car",
            format="geojson",
        )
        return route["features"][0]["geometry"]["coordinates"]

    @staticmethod
    def distance_between_coordinates(start: Point_, end: Point_):
        R = 6371000
        lon1, lat1 = start
        lon2, lat2 = end
        d_lat = radians(lat2 - lat1)
        d_lon = radians(lon2 - lon1)
        a = (sin(d_lat / 2) ** 2) + cos(radians(lat1)) * cos(radians(lat2)) * (sin(d_lon / 2) ** 2)
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        distance = R * c
        return round(distance, 2)

    def write_point_to_base(self, point_1: Point_, point_2: Point_, t: float) -> None:
        lat1, lon1 = point_1
        lat2, lon2 = point_2

        delta_lat = lat2 - lat1
        delta_lon = lon2 - lon1

        lat = round(lat1 + delta_lat * t, 6)
        lon = round(lon1 + delta_lon * t, 6)
        point = Point(lat, lon)
        GPSData.objects.create(vehicle_id=self.vehicle_id, point=point, timestamp=self.timestamp)
        current_time = self.timestamp.replace(tzinfo=self.tzinfo).strftime("%Y-%m-%d: %H:%M")
        in_road = str(self.timestamp - self.start_time)
        self.stdout.write(
            self.style.SUCCESS(f"INFO: {current_time} записана точка - lat: {lat}, lon: {lon}, в пути {in_road}")
        )


#!/usr/bin/env python3
# Author:   Michael E. Rose <michael.ernst.rose@gmail.com>
"""Computes centralities for all nodes of a all networks."""

import time
from pathlib import Path

import networkx as nx
import numpy as np
import pandas as pd

COAUTHOR_FOLDER = Path("./206_coauthor_networks")
INFORMAL_FOLDER = Path("./209_informal_networks")
TARGET_FOLDER = Path("./220_centralities")

ATTENUATIONS = list(map(lambda x: x/100.0, list(range(5, 96, 10))))

def compute_centralities(G, H, degree=True, eigvec=True, weight=None):
    """Return DataFrame with node-wise network measures."""
    start = time.time()
    # Neighborhood centrality
    df = discounted_neighborhood(G)
    print("... computing other centralities")
    label = ""
    if weight:
        label = "_w"
    # Other centralities
    if degree:
        df["degree" + label] = pd.Series(dict(nx.degree(H, weight=weight)))
    if eigvec:
        df["eigenvector" + label] = pd.Series(
            nx.eigenvector_centrality_numpy(G, weight=weight))
    diff = time.strftime("%H:%M:%S", time.gmtime(time.time() - start))
    print("... Time for computation:", diff)
    return df

def discounted_neighborhood(net):
    """Compute discounted neighborhood centrality."""
    print("... computing neighborhood centrality")
    nodes = sorted(net.nodes())
    M_sum = G = M = nx.adjacency_matrix(net, nodelist=nodes, weight=None).todense()
    I = np.identity(len(G))
    # First iteration
    idx = 1
    vec = np.array(M.sum(axis=0))[0]
    centr = {alpha: (alpha**idx)*vec for alpha in ATTENUATIONS}
    # All other iterations
    while np.any(M):
        idx += 1
        W = np.matmul(M, G)
        Z = W.clip(max=1) - (I + M_sum)
        M = Z.clip(min=0)
        vec = np.array(M.sum(axis=0))[0]
        centr = {alpha: np.add(old, (alpha**idx) * vec) for
                 alpha, old in centr.items()}
        M_sum = np.add(M_sum, M)
    # Create DataFrame from dictionary
    df = pd.DataFrame.from_dict(centr)
    df.index = nodes
    df.columns = [f"neighborhood_{int(alpha*100)}" for alpha in df.columns]
    return df

def giant(H):
    """Return giant component of a network."""
    try:
        components = sorted(nx.connected_components(H), key=len, reverse=True)
    except nx.NetworkXNotImplemented:  # Directed network
        components = sorted(nx.weakly_connected_components(H),
                            key=len, reverse=True)
    return H.subgraph(components[0])

def main():
    files = INFORMAL_FOLDER.glob("*.gexf")
    files.extend(COAUTHOR_FOLDER.glob("*.gexf"))

    print(">>> Now working on:")
    for file in sorted(files):
        # Read in
        year = file.name[:-5]
        if file.parts[-2] == "206_coauthor_networks":
            net_type = 'coauth'
        else:
            net_type = 'informal'
        ident = "_".join([net_type, year])
        print(f"... {ident} ...")
        H = nx.read_gexf(file)
        G = giant(H)

        # Centralities (with predefined attenuation factors)
        centr = compute_centralities(G, H, weight=None).sort_index()
        centr.to_csv((TARGET_FOLDER/ident).with_suffix(".csv"), index_label="node")

if __name__ == '__main__':
    main()


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

import numpy as np
import time
import subprocess
from typing import Iterable, List
from read_eval import read_eval_output
from tqdm import tqdm

def shell_command(command):
    peval = subprocess.Popen(
                command,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT)
    stdout, stderr = peval.communicate()
    return stdout.decode("utf-8")

def read_starsem(fn: str) -> Iterable[List[str]]:
    sentence = []
    with open(fn) as fh:
        for line in fh:
            if line == "\n":
                yield sentence
                sentence = []
            else:
                sentence.append(line.split())

def eval_singles(gold: Iterable[str], pred: Iterable[str]):
    tpfpfns = []
    for g, p in tqdm(zip(gold, pred), total=len(gold)):
        with open("g.tmp", "w") as gh, open("p.tmp", "w") as ph:
            print("\n".join(["\t".join(x) for x in g]), end="\n\n", file=gh)
            print("\n".join(["\t".join(x) for x in p]), end="\n\n", file=ph)
        peval = shell_command("perl eval.cd-sco.pl -g g.tmp -s p.tmp".split())
        results = read_eval_output(peval.split("\n"))
        tpfpfns.append(np.array([[v["tp"], v["fp"], v["fn"]] for v in
                                 results.values()]
                                )
                       )
    return np.array(tpfpfns)

class color:
    PURPLE = '\033[95m'
    CYAN = '\033[96m'
    DARKCYAN = '\033[36m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    END = '\033[0m'

def prec(x, i, j):
    return np.divide(x[..., i], (x[..., i] + x[..., j] + 1e-6))

def rec(x, i, j):
    return np.divide(x[..., i], (x[..., i] + x[..., j] + 1e-6))

def fscore(p, r):
    return np.divide(2 * p * r, (p + r + 1e-6))

def compute_scores(scores, counts, p, r, x):
    scores[:, x] = fscore(p, r)
    return scores

def fill_scores(scores, evals, debug=False):
    eval_cnts = evals.shape[0] if len(evals.shape) > 2 else 1
    if eval_cnts == 1:
        a, b = evals.shape
        evals = np.reshape(evals, (1, a, b))
    for i in range(eval_cnts):
        scores[i, ..., 0] = prec(evals[i], 0, 1)
        scores[i, ..., 1] = rec(evals[i], 0, 2)
        scores[i, ..., 2] = fscore(scores[i, ..., 0], scores[i, ..., 1])

        if debug:
            print(scores[i, ..., 0])
            print(scores[i, ..., 1])
            print(scores[i, ..., 2])
    return scores

def main(b, folder, devtest, setup1, setup2, debug=False):

    if debug:
        s = time.time()

    r = 5
    gold = list(read_starsem(f"{folder}/neg_{devtest}.starsem"))
    pred1 = []
    pred2 = []
    for i in range(1, r+1):
        print(f"Run: {i}")
        p1_fn = f"{folder}/{setup1}/{devtest}.starsem.{i}.pred"
        p2_fn = f"{folder}/{setup2}/{devtest}.starsem.{i}.pred"
        p1 = read_starsem(p1_fn)
        p2 = read_starsem(p2_fn)
        pred1.extend(eval_singles(gold, p1))
        pred2.extend(eval_singles(gold, p2))

    # number of runs
    n = len(gold)
    b = int(b)

    M1 = np.array(pred1)
    M2 = np.array(pred2)

    if debug:
        print(f"reading in data {time.time() - s}")
        s = time.time()

    # sample 'b' ids for a test set of size 'n' with 'r' runs
    # np.random.choice(n * r, n*b).reshape(b, n)

    if debug:
        print(f"data as matrix {time.time() - s}")
        s = time.time()

    # sample_ids samples b datasets of size n with indices ranging the five
    # runs creating a sample out of all runs
    sample_ids = np.random.choice(n * r, n*b).reshape(b, n)

    # fill a zero matrix with how often each sentence was drawn in one sample
    samples = np.zeros((n*r, b))
    for j in range(b):
        for i in range(n):
            samples[sample_ids[j, i], j] += 1

    if debug:
        print(f"get samples {time.time() - s}")
        s = time.time()

    # get the counts for the sample
    # Mx has the counts per sentence and samples chooses how often each sample
    # is taken resulting in a matrix of b rows with sums of tp, fp, fn etc.
    evals1 = (np.einsum('ijk,il->ljk', M1, samples))
    evals2 = (np.einsum('ijk,il->ljk', M2, samples))

    if debug:
        print(f"extract sample counts {time.time() - s}")
        s = time.time()

    # compute the eval measures for each row/sample
    sample_scores1 = fill_scores(np.zeros((b, M1.shape[1], M1.shape[2])), evals1, False)
    sample_scores2 = fill_scores(np.zeros((b, M2.shape[1], M2.shape[2])), evals2, False)

    if debug:
        print(f"compute scores {time.time() - s}")
        s = time.time()
        print(np.sum(M1, axis=0))

    # scores for the dataset across all runs
    true_scores1 = fill_scores(np.zeros((1, M1.shape[1], M1.shape[2])), np.sum(M1, axis=0))
    true_scores2 = fill_scores(np.zeros((1, M2.shape[1], M2.shape[2])), np.sum(M2, axis=0))

    # bootstrap scores
    deltas = true_scores1 - true_scores2
    deltas *= 2

    diffs = sample_scores1 - sample_scores2
    diffs_plus = np.where(diffs >= 0, diffs, 0)
    diffs_minus = np.where(diffs < 0, diffs, 0)

    if debug:
        print("sample_scores1")
        print(sample_scores1)
        print("sample_scores2")
        print(sample_scores2)
        print("diffs")
        print(diffs)
        print("diffs_plus")
        print(diffs_plus)
        print("diffs_minus")
        print(diffs_minus)

    deltas_plus = np.where(deltas > 0, deltas, np.float("inf"))

    deltas_minus = np.where(deltas < 0, deltas, -np.float("inf"))
    s1 = np.sum(diffs_plus > deltas_plus, axis=0)
    s2 = np.sum(diffs_minus < deltas_minus, axis=0)

    if debug:
        print(f"the rest {time.time() - s}")

        print(true_scores1)
        print(true_scores2)

        print(s1 / b)
        print(s2 / b)

        print()
    s1 = s1 / b
    s2 = s2 / b
    end = color.END

    print(f"{color.BOLD}{color.BLUE}{setup1} || {color.RED}{setup2}{color.END}")
    _m, _p, _r, _f = "Measure Precision Recall Fscore".split()
    print(f"{_m:<15}{_p:<21}\t{_r:<21}\t{_f:<21}")
    for i, name in enumerate("CUE SM ST FN".split()):
        print(f"{name:<13}:", end=" ")
        for j, mea in enumerate("Prec. Rec. Fscore".split()):
            x = true_scores1[0][i][j]
            y = true_scores2[0][i][j]
            z = s1[i][j] if x > y else s2[i][j]
            if z < 0.05 and x > y:
                bold = color.BLUE
            elif z < 0.05 and y > x:
                bold = color.RED
            else:
                bold = color.END
            # print(f"{bold}{name:<13}: {x:.2%}\t{y:.2%}\t{z:.4f}{end}", end="\t")
            print(f"{bold}{x:.2%}\t{y:.2%}\t{z:.4f}{end}", end="\t")
        print()

    print()
    if debug:
        for r_i in [1, 2, 3, 4, 5]:
            print(r_i)
            # scores for the dataset across all runs (i-1)*int(n/5):int(n/5)*i
            print(M1.shape)
            m1 = M1[(r_i-1)*int(n):int(n)*r_i, ...]
            m2 = M2[(r_i-1)*int(n):int(n)*r_i, ...]
            print(m1.shape)
            print(np.sum(m1, axis=0))
            print(np.sum(m2, axis=0))
            true_scores1 = fill_scores(np.zeros((1, m1.shape[1], m1.shape[2])), np.sum(m1, axis=0))
            true_scores2 = fill_scores(np.zeros((1, m2.shape[1], m2.shape[2])), np.sum(m2, axis=0))
            # true_scores1 = fill_scores(np.zeros((1, 8)), np.sum(M1[(r_i-1)*int(N/r):int(N/r)*r_i, :], axis=0))
            # true_scores2 = fill_scores(np.zeros((1, 8)), np.sum(M2[(r_i-1)*int(N/r):int(N/r)*r_i, :], axis=0))
            print(true_scores1)
            print(true_scores2)

if __name__ == "__main__":
    import sys
    b = 10e5
    debug = True
    folder = sys.argv[1]
    devtest = sys.argv[2]
    setup1 = sys.argv[3]
    setup2 = sys.argv[4]

    main(b, folder, devtest, setup1, setup2, debug=debug)


KEYS = {
'access_token' :'自分のやつ書いてね',
'access_token_secret':'自分のやつ書いてね',
'api_key' :'自分のやつ書いてね',
'api_secret' : '自分のやつ書いてね'

}

#correct Twitter data
import json
from requests_oauthlib import  OAuth1Session
twitter = OAuth1Session(KEYS['api_key'],KEYS['api_secret'],KEYS['access_token'],KEYS['access_token_secret'])

def getTwitterData(key_word,latitude,longtitude,radius,repeat=10):
    url = "https://api.twitter.com/1.1/search/tweets.json"
    #取得パラメータ
    params = {'q':key_word,'count':'100','geocode':'%s,%s,%skm' % (latitude,longtitude,radius),'result_type':'recent'}
    tweets = []
    mid = -1

    for i in range(repeat):
        params['max_id'] = mid #midより古いIDをツイート
        res = twitter.get(url,params = params)
        if res.status_code ==200:
            #レスポンスからツイートする
            sub_tweets = json.loads(res.text)['statuses']

            user_ids = []

            for tweet in sub_tweets:
                user_ids.append(int(tweet['id']))
                tweets.append(tweet)

            if len(user_ids)>0:
                min_user_id = min(user_ids)
                mid = min_user_id -1
            else:
                mid = -1
                print(mid)
        else:
                print("Failed:%d" % res.status_code)

    print("ツイート取得数:%s" % len(tweets))
    return tweets

import time,calendar

def YmdHMS(created_at):
    time_utc = time.strptime(created_at,'%a %b %d %H:%M:%S +0000 %Y')
    unix_time = calendar.timegm(time_utc)
    time_local = time.localtime(unix_time)
    return time.strftime("%Y/%m¥/%d %H:%M:%S",time_local)

#latitude35.1686111111 longtitude:136.910277778

tweets_atuma = getTwitterData("サイトウ",35.1761591,136.9082679,20,repeat=10)
for tweet in tweets_atuma:
    print(YmdHMS(tweet["created_at"]))
    print(tweet["text"]+"\n")


#  file: api/urls.py
from django.urls import path, register_converter
from api.converters import DateConverter

from api.views import StockListListAPIView, StockListRetrieveAPIView
from api.views import StockInformationHistoryListAPIView, StockInformationHistoryRetrieveAPIView, StockInformationSparkListAPIView
from api.views import StockPriceHistoryListAPIView, StockPriceHistoryRetrieveAPIView, StockPriceSparkListAPIView
from api.views import HistoricalStockPriceAPIView, StockSummaryAPIView, StockYearlyFinancialStatementsAPIView, StockQuarterlyFinancialStatementsAPIView, StockProfileAPIView

app_name = 'api'
register_converter(DateConverter, 'date')

urlpatterns = [
    path('stocklist/', StockListListAPIView.as_view(), name='stocklist-list'),
    path('stocklist/<str:ticker>/',
         StockListRetrieveAPIView.as_view(), name='stocklist-detail'),

    path('stockinformationhistory/<str:ticker>/',
         StockInformationHistoryListAPIView.as_view(), name='stockinformationhistory-list'),
    path('stockinformationhistory/<str:ticker>/<date:update_date>/',
         StockInformationHistoryRetrieveAPIView.as_view(), name='stockinformationhistory-detail'),
    path('stockinformationspark/<str:ticker>/<date:s_date>-<date:e_date>/',
         StockInformationSparkListAPIView.as_view(), name='stockinformationspark-list'),

    path('stockpricehistory/<str:ticker>/',
         StockPriceHistoryListAPIView.as_view(), name='stockpricehistory-list'),
    path('stockpricehistory/<str:ticker>/<date:update_date>/',
         StockPriceHistoryRetrieveAPIView.as_view(), name='stockpricehistory-detail'),
    path('stockpricespark/<str:ticker>/<date:s_date>-<date:e_date>/',
         StockPriceSparkListAPIView.as_view(), name='stockpricespark-list'),

    path('historicalstockprice/<str:ticker>/<date:s_date>-<date:e_date>/',
          HistoricalStockPriceAPIView.as_view(), name='historicalstockprice-list'),
    path('stocksummary/<str:ticker>/',
          StockSummaryAPIView.as_view(), name='stocksummary-detail'),
    path('stockyearlyfinancialstatements/<str:ticker>/',
          StockYearlyFinancialStatementsAPIView.as_view(), name='stockyearlyfinancialstatements-detail'),
    path('stockquarterlyfinancialstatements/<str:ticker>/',
          StockQuarterlyFinancialStatementsAPIView.as_view(), name='stockquarterlyfinancialstatements-detail'),
    path('stockprofile/<str:ticker>/',
          StockProfileAPIView.as_view(), name='stockprofile-detail'),
]


import random
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QColor
from qgis.core import QgsExpression, QgsExpressionContext, QgsExpressionContextUtils

from .qgis2threejscore import calculateDEMSize
from .qgis2threejstools import logMessage
from .stylewidget import StyleWidget, ColorWidgetFunc, OpacityWidgetFunc, OptionalColorWidgetFunc, ColorTextureWidgetFunc

colorNames = []

class DEMPropertyReader:

  def __init__(self, layerId, properties=None):
    properties = properties or {}
    self.layerId = layerId
    self.properties = properties

  def demSize(self, canvasSize):
    if self.layerId == "FLAT":
      return QSize(2, 2)

    sizeLevel = self.properties.get("horizontalSlider_DEMSize", 2)
    roughening = 0
    if self.properties.get("checkBox_Surroundings", False):
      roughening = self.properties.get("spinBox_Roughening", 0)
    return calculateDEMSize(canvasSize, sizeLevel, roughening)

class VectorPropertyReader:

  def __init__(self, objectTypeManager, renderContext, layer, properties):
    assert(properties is not None)
    self.renderContext = renderContext
    self.expressionContext = QgsExpressionContext()
    self.expressionContext.appendScope(QgsExpressionContextUtils.layerScope(layer))
    self.layer = layer
    properties = properties or {}
    self.properties = properties

    if properties:
      self.objType = objectTypeManager.objectType(layer.geometryType(), properties["comboBox_ObjectType"])
      self.visible = properties.get("visible", True)
    else:
      self.visible = False

    self._exprs = {}
    self.exprAlt = QgsExpression(properties.get("fieldExpressionWidget_altitude") or "0")
    self.exprLabel = QgsExpression(properties.get("labelHeightWidget", {}).get("editText") or "0")

  def evaluateExpression(self, expr_str, f):
    if expr_str not in self._exprs:
      self._exprs[expr_str] = QgsExpression(expr_str)

    self.expressionContext.setFeature(f)
    return self._exprs[expr_str].evaluate(self.expressionContext)

  def readFillColor(self, vals, f):
    return self._readColor(vals, f)

  def readBorderColor(self, vals, f):
    return self._readColor(vals, f, isBorder=True)

  # read color from COLOR or OPTIONAL_COLOR widget
  def _readColor(self, widgetValues, f, isBorder=False):
    global colorNames

    mode = widgetValues["comboData"]
    if mode == OptionalColorWidgetFunc.NONE:
      return None

    if mode == ColorWidgetFunc.EXPRESSION:
      val = self.evaluateExpression(widgetValues["editText"], f)
      try:
        if isinstance(val, str):
          a = val.split(",")
          if len(a) >= 3:
            a = [max(0, min(int(c), 255)) for c in a[:3]]
            return "0x{:02x}{:02x}{:02x}".format(a[0], a[1], a[2])
          return val.replace("#", "0x")

        raise
      except:
        logMessage("Wrong color value: {}".format(val))
        return "0"

    if mode == ColorWidgetFunc.RANDOM or f is None:
      if len(colorNames) == 0:
        colorNames = QColor.colorNames()
      colorName = random.choice(colorNames)
      colorNames.remove(colorName)
      return QColor(colorName).name().replace("#", "0x")

    # feature color
    symbol = self.layer.renderer().symbolForFeature(f, self.renderContext)
    if symbol is None:
      logMessage('Symbol for feature not found. Please use a simple renderer for {0}.'.format(self.layer.name()))
      return "0"

    else:
      sl = symbol.symbolLayer(0)
      if sl:
        if isBorder:
          return sl.strokeColor().name().replace("#", "0x")

        if symbol.hasDataDefinedProperties():
          expr = sl.dataDefinedProperty("color")
          if expr:
            # data defined color
            rgb = expr.evaluate(f, f.fields())

            # "rrr,ggg,bbb" (dec) to "0xRRGGBB" (hex)
            r, g, b = [max(0, min(int(c), 255)) for c in rgb.split(",")[:3]]
            return "0x{:02x}{:02x}{:02x}".format(r, g, b)

    return symbol.color().name().replace("#", "0x")

  def readOpacity(self, widgetValues, f):
    vals = widgetValues

    if vals["comboData"] == OpacityWidgetFunc.EXPRESSION:
      try:
        val = self.evaluateExpression(widgetValues["editText"], f)
        return min(max(0, val), 100) / 100
      except:
        logMessage("Wrong opacity value: {}".format(val))
        return 1

    symbol = self.layer.renderer().symbolForFeature(f, self.renderContext)
    if symbol is None:
      logMessage('Symbol for feature not found. Please use a simple renderer for {0}.'.format(self.layer.name()))
      return 1
    #TODO [data defined property]
    return self.layer.opacity() * symbol.opacity()

  @classmethod
  def toFloat(cls, val):
    try:
      return float(val)
    except Exception as e:
      logMessage('{0} (value: {1})'.format(e.message, str(val)))
      return 0

  # functions to read values from height widget (z coordinate)
  def useZ(self):
    return self.properties.get("radioButton_zValue", False)

  def useM(self):
    return self.properties.get("radioButton_mValue", False)

  def isHeightRelativeToDEM(self):
    return self.properties.get("comboBox_altitudeMode") is not None

  def altitude(self):
    return float(self.exprAlt.evaluate(self.expressionContext) or 0)

  def labelHeight(self):
    return float(self.exprLabel.evaluate(self.expressionContext) or 0)

  def setContextFeature(self, f):
    self.expressionContext.setFeature(f)

  # read values from style widgets
  def values(self, f):
    assert(f is not None)
    vals = []
    for i in range(16):   # big number for style count
      p = "styleWidget" + str(i)
      if p not in self.properties:
        break

      widgetValues = self.properties[p]
      if len(widgetValues) == 0:
        break

      widgetType = widgetValues["type"]
      comboData = widgetValues.get("comboData")
      if widgetType == StyleWidget.COLOR:
        vals.append(self.readFillColor(widgetValues, f))

      elif widgetType == StyleWidget.OPTIONAL_COLOR:
        vals.append(self.readBorderColor(widgetValues, f))

      elif widgetType == StyleWidget.COLOR_TEXTURE:
        if comboData == ColorTextureWidgetFunc.MAP_CANVAS:
          vals.append(comboData)
        elif comboData == ColorTextureWidgetFunc.LAYER:
          vals.append(widgetValues.get("layerIds", []))
        else:
          vals.append(self.readFillColor(widgetValues, f))

      elif widgetType == StyleWidget.OPACITY:
        vals.append(self.readOpacity(widgetValues, f))

      elif widgetType == StyleWidget.CHECKBOX:
        vals.append(widgetValues["checkBox"])

      else:
        expr = widgetValues["editText"]
        val = self.evaluateExpression(expr, f)
        if val is None:
          logMessage("Failed to evaluate expression: " + expr)
          if widgetType == StyleWidget.FILEPATH:
            val = ""
          else:
            val = 0
        vals.append(val)
    return vals


from __future__ import print_function

import logging
import grpc
import safeentry_pb2
import safeentry_pb2_grpc
import time #used for setting delays

from datetime import datetime # Used for getting current date and time during checkin

#Person object for main user
class Person:
  def __init__(self, name, NRIC):
    self.name = name
    self.NRIC = NRIC

#function for creating new person
def getUserCredential():
    name = input("Please enter name: \n").lower()
    NRIC = input("Please enter NRIC: \n").lower()
    user = Person(name,NRIC)
    return user

#function for creating number of people to check in
def gCheckIn(location,currentdate):
    x = int(input("Number of people: "))
    for i in range(x):
        Gname = input(str(i+1)+")Please enter name: ").lower()
        GNRIC = input(str(i+1)+")Please enter NRIC: ").lower()
        response = safeentry_pb2.Request(name=Gname, NRIC=GNRIC,location=location, type="checkin",datetime=currentdate)
        yield response
        time.sleep(1)
#function for creating number of people to check in
def gCheckOut(user,NRIC,stub):
    users = []
    #Append main user to list of users to be checked out
    users.append([user,NRIC])
    #Get number of users to be checked out
    x = int(input("Number of people excluding yourself: "))
    #Add users to group of users to be checked out using group checkout function
    for i in range(x):
        Gname = input("Please enter name: ").lower()
        GNRIC = input("Please enter NRIC: ").lower()
        users.append([Gname,GNRIC])

    #For each user, individually check thier location history and check them out seperately
    for user in users:
        print("\nCheck out for "+str(user[0])+"\n")
        #Send RPC request using username & NRIC
        #Retrieve stream of RPC response of user location transaction
        historyResponses = stub.History(safeentry_pb2.Request(name=user[0], NRIC=user[1] ))
        #Call function to get pending locations to be checked out
        locationsToBeCheckedOut =checkout(historyResponses)
        #Send a stream of checked out rows(Request) with name,nric,location,type,datetime to server to be written to persistent storage
        serverResponse = stub.Checkout(locationsToBeCheckedOut)
        #print server response
        print(serverResponse.message)

def checkout(historyResponses):
    #Stack to hold user checked in location
    checkinLocationStack = []
    #stack to check user checked out location
    checkoutLocationStack =[]
    #get entire transaction of the user
    userHistory=historyResponses

    #Populate stack with current location, by subtracting checked in with checked out status
    for row in userHistory:
        if(row.type=="checkin"):
            checkinLocationStack.append(row)
        else:
            checkinLocationStack.pop()

    # iterrate pending checkout location ordered by latest checked in status
    for row in reversed(checkinLocationStack):
        #Current time of checkin
        date_time = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
        userInput = input("checkout of "+row.location+"?(Y/N)").lower()
        #if user wishes to check out, create new checkout transaction and append to checkout stack
        if(userInput == 'y'):
            checkoutLocationStack.append([row.name,row.NRIC,row.location,'checkout',date_time])
            response =safeentry_pb2.Request(name=row.name, NRIC=row.NRIC,location=row.location, type='checkout',datetime=date_time)
            yield response
        #if user cancels checkout, break out of loop
        elif userInput =="n":
            print("no")
            break

def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = safeentry_pb2_grpc.SafeEntryStub(channel)

        #Make RPC call to server to check if current user has been exposed to COvid-19
        response = stub.Covid(safeentry_pb2.Request(name=user.name, NRIC=user.NRIC))
        #Print notice of all possible covid-19 exposures upon login
        for i in response:
            print(str(i.message))
        while(1):
            #Get current time to be appended to current transaction
            current_date_time = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
            #Display options for current user to perform
            print("1. Check in")
            print("2. History (Past 14 days)")
            print("3. Group Check in")
            print("4. Check out")
            print("5. Group Check out")
            print("6. Test to check that you are in close contact from MOH data")
            print("7. Exit")
            rpc_call = input("Choose an option: \n")
            if rpc_call == "1":
                #RPC call to add safeEntry transaction for current user
                location = input("Please enter location: ").lower()
                response = stub.Checkin(safeentry_pb2.Request(name=user.name, NRIC=user.NRIC,location=location, type="checkin",datetime=current_date_time))
                print(str(response.message))
            elif rpc_call == "2":
                #Send RPC request using username & NRIC
                #Retrieve stream of RPC response of user location transaction history
                historyResponses = stub.History(safeentry_pb2.Request(name=user.name, NRIC=user.NRIC ))
                for x in historyResponses:
                    print(x.name+"\t"+ x.NRIC+"\t"+x.location+"\t"+ x.type+"\t"+x.datetime)
            elif rpc_call == "3":
                #Obtain location for group checkin
                location = input("Please enter location: ").lower()
                #Check in the main user
                response = stub.Checkin(safeentry_pb2.Request(name=user.name, NRIC=user.NRIC,location=location, type="checkin",datetime=current_date_time))
                print(str(response.message))
                #Call Group Check In Function to check in remaining members of the group
                responses = stub.GroupCheckin(gCheckIn(location,current_date_time))
                for x in responses:
                    print(str(x.message))

            elif rpc_call == "4":
                #Send RPC request using username & NRIC
                #Retrieve stream of RPC response of user location transaction
                historyResponses = stub.History(safeentry_pb2.Request(name=user.name, NRIC=user.NRIC ))
                print("Location for : "+user.name)
                #Call function to get location to be checked out
                locationsToBeCheckedOut =checkout(historyResponses)
                #Send a stream of checked out rows(Request) with name,nric,location,type,datetime to server to be written to persistent storage
                serverResponse = stub.Checkout(locationsToBeCheckedOut)
                #print server response
                print(serverResponse.message)
            elif rpc_call == "5":
                #Call checkout function for group checkout
                gCheckOut(user.name,user.NRIC,stub)

            elif rpc_call == "6":
                #RPC call to obtain information on potential exposure to covid-19
                response = stub.Covid(safeentry_pb2.Request(name=user.name, NRIC=user.NRIC,datetime=current_date_time))
                for i in response:
                    print(str(i.message))

            elif rpc_call == "7":
                #Terminate program
                print("Terminating.....")
                time.sleep(1)
                break
            else:
                print("\nWrong entry, please try again.....\n")

if __name__ == '__main__':
    logging.basicConfig()
    #Create new user for current checkin transaction
    user = getUserCredential()
    run()


# This file serves creates an example netCDF file to demonstrate the tools
# for reading and writing parameter scan data.

from exceptions import *
from dakota_file import DakotaFile
import numpy as np
import xarray as xr
import main
import os

# First create an instance of the dakota_netcdf class!
my_netcdf = DakotaFile()

# Unlike the Monte-Carlo sampling case we do not need any run settings
# so the add_settings function should not be called

# We need to add some parameter scan variables

# ------------------------------------------------------------------------
# WRITE EXAMPLE 1
# ------------------------------------------------------------------------

# The simplest way is to build a dictionary of needed variables and call
# the add_variable_from_dict function.

# Performing a parameter scan requires 3 variables. Upper and lower bounds
# and the number of partitions.

# Give our variable a name!
name = 'test_scan1'

# Specify that the variable should be scanned
var_type = 'scan'

# Make a dictionary of the needed data
lower_bounds  = np.random.rand(4,3)
upper_bounds  = np.random.rand(4,3)
partitions    = np.random.randint(2,10,size=(4,3))

variable_dict = { 'lower_bounds' : lower_bounds, 'upper_bounds' : upper_bounds,
                  'partitions' : partitions }

my_netcdf.add_variable_from_dict( name, var_type, variable_dict )

# -----------------------------------------------------------------------
# WRITE EXAMPLE 2
# -----------------------------------------------------------------------

# We can also add a variable by constructing an Xarray dataset
# To do this the data must be created as data arrays and then added to the dataset

# Create a name for this new variable
name = 'test_scan2'

var_type = 'scan'

lower_bounds1 = xr.DataArray(lower_bounds)
upper_bounds1 = xr.DataArray(upper_bounds)
partitions1   = xr.DataArray(partitions)

dataset = xr.Dataset( {'lower_bounds' : lower_bounds1, 'upper_bounds' : upper_bounds1,
                       'partitions' : partitions1 } )

my_netcdf.add_variable_from_dataset( name, var_type, dataset )

# ----------------------------------------------------------------------
# WRITE EXAMPLE 3
# ----------------------------------------------------------------------

# One of the advantages of using a dataset is that one can also specify the dimensions
# and coordinates of variables. It may be convenient to have these available in the
# interface script.

# Create a name for this new variable
name = 'test_scan3'

var_type = 'scan'

# Coordinates
x = np.array( [0.2,0.3,0.4,0.5,0.6] )
y = np.array( [2.5,3.5,4.5] )
z = np.array( [5.2,5.3] )

# Convert coordinates to data arrays
x = xr.DataArray(x,dims=['x'])
y = xr.DataArray(y,dims=['y'])
z = xr.DataArray(z,dims=['z'])

# Construct variable data arrays which include dimensions and coordinates
lower_bounds = np.random.rand(5,3,2)
lower_bounds = xr.DataArray(lower_bounds, coords=[x,y,z], dims=['x','y','z'])

upper_bounds = np.random.rand(5,3,2) + 2.0
upper_bounds = xr.DataArray(upper_bounds, coords=[x,y,z], dims=['x','y','z'])

partitions   = np.random.randint(2,10,size=(5,3,2))
partitions   = xr.DataArray(partitions,   coords=[x,y,z], dims=['x','y','z'])

dataset = xr.Dataset( {'lower_bounds':lower_bounds, 'upper_bounds':upper_bounds,
                       'partitions':partitions}, attrs={'type':'scan'} )

my_netcdf.add_variable_from_dataset( name, var_type, dataset )

# -----------------------------------------------------------------------
# WRITE EXAMPLE 4
# -----------------------------------------------------------------------

# We can also add a variable by constructing an Xarray dataset directly from numpy
# arrays if coordinate information is not required

# Create a name for this new variable
name = 'test_scan4'

var_type = 'scan'

lower_bounds = 0.0
upper_bounds = 1.0
partitions   = 7

dataset = xr.Dataset( {'lower_bounds' : lower_bounds, 'upper_bounds' : upper_bounds,
                       'partitions' : partitions } )

my_netcdf.add_variable_from_dataset( name, var_type, dataset )

# ----------------------------------------------------------------------
# WRITE THE FILE
# ----------------------------------------------------------------------

filename = 'DAKOTA.nc'
my_netcdf.write(filename)

#=======================================================================
# READING EXAMPLES
#=======================================================================

# To build an interface between DAKOTA and your code you will need to write something
# to read a netcdf file like the one just created and pass the new data to your code.
# The following examples demonstrate reading a netcdf.

# Create a new instance of the
my_netcdf2 = DakotaFile()

filename = 'DAKOTA.nc'
my_netcdf2.read(filename)

# ----------------------------------------------------------------------
# READ EXAMPLE 1
# ----------------------------------------------------------------------

# We can read one of the variables in the netcdf file back into a dictionary of data
# We shall read test_scan2 which was originally written as a dataset

variable_dict = my_netcdf2.get_variable_as_dict('test_scan2')

print('Read Example 1:')
print()

for key, var in variable_dict.items():

    print( key+':' )
    print( var )
    print()

# After running DAKOTA the varied data for each variable should be available as the
# 'values' entry of that variable's dictionary: variable_dict['values']

# ----------------------------------------------------------------------
# READ EXAMPLE 2
# ----------------------------------------------------------------------

# We can also read a variable in the netcdf file back into a dataset
# If the coordinates were also written to the data arrays or the dataset itself
# these will also be available

dataset = my_netcdf2.get_variable_as_dataset('test_scan3')

print('Read Example 2:')
print()

for key in dataset:

    print( key+':' )
    print( dataset[key].dims )
    print( dataset[key].coords )
    print( dataset[key].data )
    print()

# After running DAKOTA the varied data for each variables should be available within the
# 'values' data array of the variables dataset: dataset['values'].data


from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit, QMessageBox

def handCalc():
    print("统计按钮被点击了")
    info = textEdit.toPlainText()

    salary_above_20k = ''
    salary_below_20k = ''

    for line in info.splitlines():
        if not line.strip():
            continue
        parts = line.split(' ')
        name, salary, age = parts

app = QApplication([])
window = QMainWindow()
window.resize(500, 400)
window.move(300, 310)
window.setWindowTitle("薪资统计")

textEdit = QPlainTextEdit(window)
textEdit.setPlaceholderText("请输入薪资表")
textEdit.move(10, 25)
textEdit.resize(300, 340)

button = QPushButton('统计', window)
button.move(360,160)
# Slot
button.clicked.connect(handCalc())

window.show()

app.exec_()


# preRecognition.py aims to print out the number of TRs for the current recognition run.

'''
input:
    cfg
    curr_run

output:
    maxTR=xxx

usage example:
    python expScripts/recognition/preRecognition.py -c pilot_sub001.ses1.toml -r 3
'''
import sys,os
if 'watts' in os.getcwd():
    sys.path.append("/home/watts/Desktop/ntblab/kailong/rtSynth_rt/")
elif 'kailong' in os.getcwd():
    sys.path.append("/Users/kailong/Desktop/rtEnv/rtSynth_rt/")
elif 'milgram' in os.getcwd():
    sys.path.append('/gpfs/milgram/project/turk-browne/projects/rtSynth_rt/')

import argparse
import numpy as np
import pandas as pd
if 'watts' in os.getcwd():
    main_dir = "/home/watts/Desktop/ntblab/kailong/rtcloud_rt/" # main_dir = "/home/watts/Desktop/ntblab/kailong/rtcloud_kp/"
else:
    main_dir="/Users/kailong/Desktop/rtEnv/rtSynth_rt/"

sys.path.append(main_dir)

import rtCommon.utils as utils
from rtCommon.utils import loadConfigFile
argParser = argparse.ArgumentParser()
argParser.add_argument('--config', '-c', default='sub001.ses2.toml', type=str, help='experiment file (.json or .toml)')
argParser.add_argument('--run', '-r', default='1', type=str, help='current run')
args = argParser.parse_args()
from rtCommon.cfg_loading import mkdir,cfg_loading
cfg = cfg_loading(args.config)

curr_run=int(args.run)

choose = np.load(f"{cfg.subjects_dir}/{cfg.subjectName}/ses{cfg.session}/recognition/choose.npy")
for curr_run in range(1,1+len(choose)):
    order = f'{cfg.orderFolder}/recognitionOrders_{choose[curr_run - 1]}.csv'
    trial_list = pd.read_csv(order)
    maxTR=trial_list['time'].iloc[-1]/cfg.TR
    print(f"curr_run={curr_run} maxTR={maxTR}")


import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

import numpy as np

# class Loss_DCSNN2(nn.Module):
#     def __init__(self, device, lam_recon=10., lam_reg = 0.1, lam_local = 1., s = 1.):
#         super().__init__()
#         self.lam_recon = lam_recon
#         self.lam_reg = lam_reg
#         self.lam_local = lam_local
#         self.device = device
#         self.s = s
#         self.BCEWithLogitsLoss = nn.BCEWithLogitsLoss()

#     def forward(self, global_features, local_features, local_features_recon, local_aggre_features, D_global, D_local, S):
#         omega_x = global_features.mm(Variable(D_global,requires_grad=False).t())/self.s # logit values

#         # global loss
#         global_loss = self.BCEWithLogitsLoss(omega_x, S)
#         norm = global_features.norm(dim=1)
#         reg_global = (norm - torch.ones_like(norm).cuda()).pow(2).mean()

#         # reconstruction loss
#         recon_loss = 1/local_features.flatten().size(0)*torch.norm(local_features.flatten()-local_features_recon.flatten())

#         # local aggregation loss
#         local_omega_x = local_aggre_features.mm(Variable(D_local,requires_grad=False).t())/self.s
#         local_loss = self.BCEWithLogitsLoss(local_omega_x, S)
#         norm = local_aggre_features.norm(dim=1)
#         reg_local = (norm - torch.ones_like(norm).cuda()).pow(2).mean()

#         # total loss
#         loss =  global_loss + self.lam_local*local_loss + self.lam_recon*recon_loss + self.lam_reg * (reg_global + reg_local)

#         return loss, global_loss, local_loss, recon_loss, reg_global, reg_local

def _compute_homography_est_loss(h4_est, h4):
    return F.mse_loss(h4_est.view(1,-1), h4.view(1,-1).float())

def _compute_matching_loss(ms,kp,kpw,h,gt_radious):
    kp = kp.flip(1) # flip to x,y coordinates from h,w
    kp = torch.cat((kp, kp.new_ones((kp.size(0),1))), axis=1).float()
    kpw_ = torch.mm(h.float(), kp.t()).t()
    kpw_ /= kpw_[:,2].view(kpw_.size(0),1)
    kpw_ = kpw_[:,:2]
    kpw_ = kpw_.flip(1) # flip back to h,w coordinates
    # kp = kp[:,:2]
    dist = torch.norm(kpw_.view(kpw_.size(0),1,2) - kpw.view(1,kpw.size(0),2), p=2, dim=-1) # nkp (k) X nkpw (q)
    s = (dist<=gt_radious).float()
    s_pred = ms[:-1,:-1]

    s_pred_k = ms[:-1,-1]
    s_pred_q = ms[-1,:-1]

    s_k = torch.where(s.sum(1) == 0, s.new_tensor(1.), s.new_tensor(0.))
    s_q = torch.where(s.sum(0) == 0, s.new_tensor(1.), s.new_tensor(0.))

    # loss =  - (s_k*s_pred_k.log()).sum() - (s_q*s_pred_q.log()).sum()
    loss_term1 = -(s*s_pred).sum()
    loss_term2 = - (s_k*s_pred_k).sum()
    loss_term3 = - (s_q*s_pred_q).sum()
    # if (s*s_pred).sum() > 0.:
    #     loss -= (s*s_pred.log()).sum()
    # print(loss_term1.item(), loss_term2.item(),loss_term3.item())
    # if np.isnan(loss_term1.item()):
    #     aa = 1
    loss = loss_term1 + loss_term2 + loss_term3
    return loss
    # return F.binary_cross_entropy(s_pred, s) +F.binary_cross_entropy(s_pred_k, s_k)+ \
                # F.binary_cross_entropy(s_pred_q, s_q)

def compute_matcher_loss(matching_scores, h4_ests, H, H4, kpts_k, kpts_q, lam, device, gt_radious=4.):
    nimg = len(matching_scores)
    matching_loss = torch.zeros(1).float().to(device)
    homoest_loss = torch.zeros(1).float().to(device)

    for ms, kp, kpw, h, h4_est, h4 in zip(matching_scores, kpts_k, kpts_q, H, h4_ests, H4):
        matching_loss += _compute_matching_loss(ms,kp,kpw,h,gt_radious)
        homoest_loss += _compute_homography_est_loss(h4_est, h4)
    matching_loss /= nimg
    homoest_loss /= nimg
    homoest_loss*=lam

    return matching_loss + homoest_loss, matching_loss, homoest_loss

def descriptor_loss(net_out, H, positive_margin=1., negative_margin=0.2):
    lds, lds_warp = net_out['localdesc_k'], net_out['localdesc_q']
    kpts, kpts_warp = net_out['kpts_k'], net_out['kpts_q']
    assert len(kpts) == len(kpts_warp) & len(lds) == len(lds_warp) & len(lds) == H.size(0)
    nimg = len(kpts)
    loss = lds[0].new_zeros(1)

    for ld, ldw, kp, kpw, h in zip(lds, lds_warp, kpts, kpts_warp, H):
        kp = kp.flip(1) # flip to x,y coordinates from h,w
        kp = torch.cat((kp, kp.new_ones((kp.size(0),1))), axis=1).float()

        kpw_ = torch.mm(h.float().to(kp), kp.t()).t()
        kpw_ = kpw_/kpw_[:,2].view(-1,1)
        kpw_ = kpw_[:,:2]
        kp = kp[:,:2]

        kpw_ = kpw_.flip(1) # flip back to h,w coordinates

        # kpw_ = torch.where(kpw_<0, kpw_.new_tensor(0), kpw_)
        # kpw_ = torch.where(kpw_>223., kpw_.new_tensor(223), kpw_)

        dist = torch.norm(kpw_.view(kpw_.size(0),1,2) - kpw.view(1,kpw.size(0),2), p=2, dim=-1) # nkpw X nkpw_
        s = (dist<=8.).float()
        # print(s.sum().item())
        ld_mat = torch.einsum('dl,dw->lw', ld, ldw)
        loss_p = torch.max(torch.zeros_like(ld_mat), positive_margin-ld_mat*s)*s
        loss_p = loss_p.sum()/torch.nonzero(s,as_tuple=False).size(0)
        loss_m = torch.max(torch.zeros_like(ld_mat), ld_mat*(1.-s) - negative_margin)
        loss_m = loss_m.sum()/torch.nonzero(1-s,as_tuple=False).size(0)
        loss += loss_p + loss_m

    loss /= nimg
    # if torch.isnan(loss):
    #     aa = 1

    return loss

class Loss_DCSNN(nn.Module):
    def __init__(self, device, lamda = [0.1, 10, 0.001], temp = 0.5, self_learning=True):
        super().__init__()
        self.lam_reg, self.lam_kp, self.lam_ld = lamda
        self.device = device
        self.T = temp
        self.self_learning = self_learning

    # def forward(self, global_feature_q, global_feature_k, key_queue, S, kp_gt, kp_out, kp_warp_gt, kp_warp_out):
    def forward(self, key_queue, adj_mat, kpts_gt_k, kpts_gt_q, net_out, H):
        globaldesc_q = net_out['globaldesc_q']
        globaldesc_k = net_out['globaldesc_k']
        kpts_q = net_out['scores_map_q']
        kpts_k = net_out['scores_map_k']

        # regularization
        norm = globaldesc_q.norm(dim=1)
        reg_global = (norm - torch.ones_like(norm).cuda()).pow(2).mean()

        # global loss
        omega_x = globaldesc_q.mm(Variable(key_queue,requires_grad=False))/self.T # logit values
        global_loss = F.binary_cross_entropy_with_logits(omega_x, adj_mat)

        # self learning
        if self.self_learning:
            norm_k = globaldesc_k.norm(dim=1)
            reg_global_self = (norm_k - torch.ones_like(norm_k).cuda()).pow(2).mean()
            reg_global += reg_global_self

            self_omega_x = (globaldesc_q*Variable(globaldesc_k,requires_grad=False)).sum(dim=1)/self.T
            label = torch.ones_like(self_omega_x) # self label
            global_loss_self = F.binary_cross_entropy_with_logits(self_omega_x, label)
            global_loss += global_loss_self

        # kp_example = kpts_k[0].cpu().detach().numpy()
        # from PIL import Image
        # kp_example = Image.fromarray(kp_example, mode='L')

        # kpgt_example = kpts_gt_k.squeeze()[0].cpu().detach().numpy()
        # from PIL import Image
        # kpgt_example = Image.fromarray(kpgt_example, mode='L')

        # keypoint - element-wise bce
        # kp_bce = F.binary_cross_entropy_with_logits(kpts_q, kpts_gt_q.squeeze()) \
        #        + F.binary_cross_entropy_with_logits(kpts_k, kpts_gt_k.squeeze())
        # kp_bce = F.binary_cross_entropy(torch.sigmoid(kpts_q), kpts_gt_q.squeeze()) \
        #        + F.binary_cross_entropy(torch.sigmoid(kpts_k), kpts_gt_k.squeeze())
        kp_loss = F.binary_cross_entropy(kpts_q, kpts_gt_q.squeeze()) \
               + F.binary_cross_entropy(kpts_k, kpts_gt_k.squeeze())

        ld_loss = descriptor_loss(net_out, H) # local descriptor loss

        # total loss
        reg_global *= self.lam_reg
        kp_loss *= self.lam_kp
        ld_loss *= self.lam_ld
        loss =  global_loss + reg_global + kp_loss + ld_loss

        return loss, global_loss, reg_global, kp_loss, ld_loss

def setup_loss(method, device, **kwargs):
    method = method.lower()
    # if method in ['dpsh']:
    #     criterion = Loss_DPSH(device, **kwargs)
    # elif method in ['dhn']:
    #     criterion = Loss_DHN(device, **kwargs)
    # elif method in ['dhnnl2']:
    #     criterion = Loss_DHNNL2(device, **kwargs)
    # elif method in ['dcsnn']:
    #     criterion = Loss_DCSNN(device, **kwargs)
    # if method in ['dcsnn2']:
    #     criterion = Loss_DCSNN2(device, **kwargs)
    # else:
    #     raise ValueError('Unknown method type!')
    criterion = Loss_DCSNN(device, **kwargs)
    return criterion


"""Contains landing page forms.
"""
from django import forms
from frontend.utils import get_essay_types_for_display

class CallToActionForm(forms.Form):
    """This form initiates user interaction. It collects
    their email and prompts them to make an order

    Args:
        forms (class): describes the logical structure of an object
    """
    error_messages = {
        'email': {
            'required': 'Please provide an email address',
            'invalid': 'The email format is invalid'
        },
        'number_of_pages': {
            'required': 'You must provide the total number of pages',
            'invalid': 'Please provide a whole number for number of pages',
            'min_value': 'Number of pages cannot be less than one'
        },
        'date_deadline': {
            'required': 'Please provide a deadline for your essay',
            'invalid': (
                'Please provide a valid date, format should be '
                '(Month day, year) eg. Oct 25, 2006'
            )
        },
        'time_deadline': {
            'invalid': (
                'The time deadline provided is invalid'
                'it should be in the format (H:M) eg. 6:30'
            ),
        },
        'essay_type': {
            'required': 'Please provide the type of essay',
            'invalid_choice': 'Please select one of the choices provided'
        },
        'terms_and_conditions': {
            'required': 'Please accepts our terms and conditions to proceed'
        }
    }
    essays = get_essay_types_for_display()
    CHOICES = ()
    if essays['code'] == 'success':
        CHOICES = tuple((k, v) for k, v in essays['payload'].items())
    email = forms.EmailField(error_messages=error_messages['email'])
    number_of_pages = forms.IntegerField(
        min_value=1, error_messages=error_messages['number_of_pages']
    )
    date_deadline = forms.DateField(
        input_formats=['%b %d, %Y'],
        error_messages=error_messages['date_deadline']
    )
    time_deadline = forms.TimeField(
        required=False,
        input_formats=['%H:%M'],
        error_messages=error_messages['time_deadline']
    )
    essay_type = forms.ChoiceField(
        choices=CHOICES,
        error_messages=error_messages['essay_type']
    )
    terms_and_conditions = forms.BooleanField(
        error_messages=error_messages['terms_and_conditions']
    )
    bonus_and_promotions = forms.BooleanField(required=False)


import numpy as np
import cv2
import os
import imutils

NMS_THRESHOLD=0.3
MIN_CONFIDENCE=0.2

def pedestrian_detection(image, model, layer_name, personidz=0):
	(H, W) = image.shape[:2]
	results = []

	blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),
		swapRB=True, crop=False)
	model.setInput(blob)
	layerOutputs = model.forward(layer_name)

	boxes = []
	centroids = []
	confidences = []

	for output in layerOutputs:
		for detection in output:

			scores = detection[5:]
			classID = np.argmax(scores)
			confidence = scores[classID]

			if classID == personidz and confidence > MIN_CONFIDENCE:

				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)])
				centroids.append((centerX, centerY))
				confidences.append(float(confidence))
	# apply non-maxima suppression to suppress weak, overlapping
	# bounding boxes
	idzs = cv2.dnn.NMSBoxes(boxes, confidences, MIN_CONFIDENCE, NMS_THRESHOLD)
	# ensure at least one detection exists
	if len(idzs) > 0:
		# loop over the indexes we are keeping
		for i in idzs.flatten():
			# extract the bounding box coordinates
			(x, y) = (boxes[i][0], boxes[i][1])
			(w, h) = (boxes[i][2], boxes[i][3])
			# update our results list to consist of the person
			# prediction probability, bounding box coordinates,
			# and the centroid
			res = (confidences[i], (x, y, x + w, y + h), centroids[i])
			results.append(res)
	# return the list of results
	return results

labelsPath = "coco.names"
LABELS = open(labelsPath).read().strip().split("\n")

weights_path = "yolov4-tiny.weights"
config_path = "yolov4-tiny.cfg"

model = cv2.dnn.readNetFromDarknet(config_path, weights_path)

layer_name = model.getLayerNames()
layer_name = [layer_name[i[0] - 1] for i in model.getUnconnectedOutLayers()]

import keras

def detectByPathImage(path):

	image = cv2.imread(path)

	image = imutils.resize(image, width=700)
	results = pedestrian_detection(image, model, layer_name,
		personidz=LABELS.index("person"))
	count = 0
	for res in results:
		cv2.rectangle(image, (res[1][0],res[1][1]), (res[1][2],res[1][3]), (0, 255, 0), 2)
		cv2.putText(image, f'person {count}', (res[1][0],res[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)
		count += 1
	cv2.rectangle(image,(350,20),(750,120),(0,0,0),-1)
	cv2.putText(image,  f'People Detected : {count-1}', (350,40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255), 2)
	if count>15:
		cv2.putText(image, 'Social Distancing Violated', (350,80), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255), 2)
	cv2.imshow("Detection",image)

	cv2.waitKey(0)
	cv2.destroyAllWindows()

def detectByImage(image):

#     image = cv2.imread(path)

	socvio = False
	image = imutils.resize(image, width=700)
	results = pedestrian_detection(image, model, layer_name,
		personidz=LABELS.index("person"))
	count = 0
	for res in results:
		cv2.rectangle(image, (res[1][0],res[1][1]), (res[1][2],res[1][3]), (0, 255, 0), 2)
		cv2.putText(image, f'person {count}', (res[1][0],res[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)
		count += 1
	if count>15:
		socvio = True
	return (image,count-1,socvio)

def detectByVideo(file):

	file.save('abc.mp4')

	count = 0
	cap = cv2.VideoCapture('abc.mp4')
	ret,frame = cap.read()
	output = cv2.VideoWriter(
        "output.mp4", cv2.VideoWriter_fourcc('M','P','G','4'), 30, (frame.shape[1], frame.shape[0]))
	# writer = None
	cap.release()
	cap = cv2.VideoCapture('abc.mp4')
	k = 1
	while(cap.isOpened() and k<244):
		count = 1
		print(k)
		k += 1
		ret, frame = cap.read()
		if(ret):
			print(frame.shape)
			results = pedestrian_detection(frame, model, layer_name,
			personidz=LABELS.index("person"))

			for res in results:
				cv2.rectangle(frame, (res[1][0],res[1][1]), (res[1][2],res[1][3]), (0, 255, 0), 2)
				cv2.putText(frame, f'person {count}', (res[1][0],res[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)

				count += 1
			cv2.imshow("name",frame)
			cv2.waitKey(10)
			output.write(frame)
	# 		if 0xFF == ord('s') or k > 1000:
	# 			print("break condition")
	# 			break
		else:
			break
	# 	# break
	output.release()
	cap.release()
	return output

def detectByCamera():

	count = 0
	cap = cv2.VideoCapture(0)
	ret,frame = cap.read()
	output = cv2.VideoWriter(
        "output.mp4", cv2.VideoWriter_fourcc('M','P','G','4'), 30, (frame.shape[1], frame.shape[0]))
	cap.release()
	cap = cv2.VideoCapture(0)
	k = 1
	while(True):
		count = 1
		print(k)
		k += 1
		ret, frame = cap.read()
		print(frame.shape)
		results = pedestrian_detection(frame, model, layer_name,
		personidz=LABELS.index("person"))

		for res in results:
			cv2.rectangle(frame, (res[1][0],res[1][1]), (res[1][2],res[1][3]), (0, 255, 0), 2)
			cv2.putText(frame, f'person {count}', (res[1][0],res[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)

			count += 1
		cv2.imshow("name",frame)
		cv2.waitKey(100)
		if cv2.waitKey(100) & 0xFF == ord('x'):
			break
		output.write(frame)
	output.release()
	cap.release()
	return output

def detectByPathVideo(path):

	cap = cv2.VideoCapture(path)
	writer = None

	while True:
		count = 1
		(grabbed, image) = cap.read()

		if not grabbed:
			break
		image = imutils.resize(image, width=700)
		results = pedestrian_detection(image, model, layer_name,
			personidz=LABELS.index("person"))

		for res in results:
			cv2.rectangle(image, (res[1][0],res[1][1]), (res[1][2],res[1][3]), (0, 255, 0), 2)
			cv2.putText(image, f'person {count}', (res[1][0],res[1][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1)
			count += 1

		cv2.putText(image,  f'People Detected : {count-1}', (230,70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,0), 2)
		if count>15:
			cv2.putText(frame, 'Social Distancing Violated', (230,120), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,0), 2)
		cv2.imshow("Detection",image)

		key = cv2.waitKey(1)
		if key == 27:
			break

	cap.release()
	cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 14:54:03 2018

@author: Administrator
"""
from selenium import webdriver
#操作键盘鼠标
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.PhantomJS()
driver.get("http://www.baidu.com/")

driver.find_element_by_id("kw").send_keys(u"美女")
driver.save_screenshot("image/美女.png")
print("ok")
driver.find_element_by_id("su").click()
time.sleep(10)
driver.save_screenshot("image/nv.png")
print("ok1")


from django.db import models
from django.db.models.deletion import CASCADE, SET_NULL

MANSIONI_TIPI = (
    ('VE', 'Verificatore'),
    ('VI', 'Vigilantes'),
    ('VO', 'Volontario'),
)

EVENTI_TIPI = (
    ('IM',      'In servizio di mattina'),
    ('IS',      'In servizio di sera'),
    ('I2S',     'In servizio di seconda serata'),
    ('ASS',     'Assente'),
    ('VIME',    'Visita medica'),
    ('A33',     'Articolo 33'),
    ('LIC',     'In licenza'),
    ('MAL',     'In malattia'),
    ('ALTRO',   'Altro'),
)

POLO_OPZ = (
    ('R1',      'Rimessa 1'),
    ('R8',      'Rimessa 8'),
    ('AL',      'P.za Borsellino'),
    ('ST',      'Stazione Centrale'),
    ('RP',      'P.za Repubblica'),
    ('PB',      'P.za Borsa'),
    ('SZ',      'P.za Sanzio'),
    ('OB',      'Due Obelischi'),
    ('NE',      'Nesima'),
    ('FN',      'Fontanarossa'),
    ('VA',      'Villaggio S\'Agata'),
)

FASCIA_OPZ = (
    ('M',        'Mattinale'),
    ('S',        'Serale'),
    ('S2',       'Seconda Serata'),
)

GIORNO_RIPOSO = (
    (7,        '1° Domenica'),
    (14,       '2° Domenica'),
    (21,       '3° Domenica'),
    (28,       '4° Domenica'),
    (35,       '5° Domenica'),
    (42,       '6° Domenica'),
    (49,       '7° Domenica'),
    (56,       '8° Domenica'),
    (63,       '9° Domenica'),
    (70,       '10° Domenica'),
    (77,       '11° Domenica'),
    (83,       'Sabato'),
    (89,       'Venerdi'),
    (95,       'Giovedi'),
    (101,      'Mercoledi'),
    (107,      'Martedi'),
    (1,        'Lunedi'),
)

class SequenzaRiposi(models.Model):
    nome                        = models.CharField(max_length=50, unique=True)
    sequenza                    = models.TextField()

    class Meta:
        verbose_name_plural     = "Sequenze Riposi"
        ordering                = ["nome"]

    def __str__(self):
        return self.nome

#TODO: Rinomina campi: data_inizio... e indice_in...
class Lavoratore(models.Model):
    matricola           = models.CharField(max_length=5, unique=True, blank=True)
    nominativo          = models.CharField(max_length=50)
    mansione            = models.CharField(max_length=2, choices=MANSIONI_TIPI)
    sequenza_riposi     = models.ForeignKey(SequenzaRiposi, on_delete=models.SET_NULL, null=True, blank=True)
    #data_di_riferimento_per_sequenza_riposi
    data_per_sequenza   = models.DateField("data di riferimento ...", null=True, blank=True)
    #indice_giorno_di_riferimento_per_sequenza_riposi
    indice_per_sequenza = models.IntegerField("corrisponde alla ...", null=True, blank=True, choices=GIORNO_RIPOSO)

    class Meta:
        verbose_name_plural     = "Lavoratori"
        ordering                = ["nominativo"]

    def __str__(self):
        return self.nominativo

class Linea(models.Model):
    nome                        = models.CharField(primary_key=True, unique=True, max_length=5)
    polo                        = models.CharField(max_length=2, blank=True, null=True, default='R8', choices=POLO_OPZ,)
    descrizione                 = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        verbose_name_plural     = "Linee"
        ordering                = ['nome']

    def __str__(self):
        return self.nome

class Turno(models.Model):
    empty_value_display         = '---'
    mansione                    = models.CharField(max_length=2, choices=MANSIONI_TIPI)
    priorita                    = models.IntegerField(default=99)
    linea                       = models.ForeignKey(Linea, to_field='nome', on_delete=models.SET_NULL, blank=True, null=True)
    fascia                      = models.CharField(max_length=2, blank=True, null=True, choices=FASCIA_OPZ,)
    ora_inizio                  = models.TimeField()
    ora_fine                    = models.TimeField()
    polo_monta                  = models.CharField(max_length=2, blank=True, null=True, choices=POLO_OPZ,)
    note                        = models.TextField(max_length=100, blank=True)

    class Meta:
        verbose_name_plural     = "Turni"
        ordering                = ['ora_inizio', 'polo_monta']
        #constraints             = [models.UniqueConstraint(fields=['mansione', 'linea', 'ora_inizio', 'ora_fine', 'polo_monta'], name='unique_turno')]

    def __str__(self):
        return ('%s %s %s %s %s %s %s' % (self.mansione, self.priorita, self.linea, self.ora_inizio.strftime("%H:%M"), self.ora_fine.strftime("%H:%M"), self.polo_monta, self.note))

class Matrice(models.Model):
    data                        = models.DateField(auto_now_add=True)
    nome                        = models.CharField(unique=True, max_length=20)
    turni                       = models.ManyToManyField(Turno)

    class Meta:
        verbose_name_plural     = "Matrici"
        ordering                = ['nome']

    def __str__(self):
        return ('%s del %s' % (self.nome, self.data.strftime("%d %B %Y")))

class Evento(models.Model):
    data                        = models.DateField()
    lavoratore                  = models.ForeignKey(Lavoratore, on_delete=models.CASCADE)
    tipologia                   = models.CharField(max_length=5, choices=EVENTI_TIPI)
    turno                       = models.ForeignKey(Turno, on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        verbose_name_plural     = "Eventi"
        ordering                = ['data', 'lavoratore']


import re
from django.shortcuts import render, redirect
from .models import Document, File
from .forms import DocumentForm
from Emp.models import Employee, Department
from django.contrib.auth.decorators import login_required

@login_required
def document_upload(request):

    document_types = [1, 2, 3]
    user = request.user
    try:
        employee = Employee.objects.get(Emp_User=user)
    except Employee.DoesNotExist:
        return redirect('login')

    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.instance.Doc_Dept = employee.Emp_Dept
            # form.instance.Doc_Type = request.POST.get('Doc_Type')
            form.save()
            return redirect('/document_upload?success_page=true')
        else:
            print(form.errors)
    else:
        form = DocumentForm()

    return render(request, 'fileupload.html', {'form': form, 'employee': employee, 'document_types': document_types})