from django.shortcuts import render
from .models import Task, TaskCategory
from .serilizers import TaskCategoryCreateSerializer, TaskCategoryListSerializer, TaskCategoryRetrieveSerializer, \
    TaskListSerializer, TaskCreateSerializer, TaskRetrieveSerializer
from rest_framework.generics import CreateAPIView, UpdateAPIView, RetrieveAPIView, ListAPIView, DestroyAPIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated, AllowAny
from drf_yasg.utils import swagger_auto_schema

class TaskCategoryListAPIView(ListAPIView):
    serializer_class = TaskCategoryListSerializer
    queryset = TaskCategory.objects.all()

    @swagger_auto_schema(tags=['Task Category'])
    def get(self, request, *args, **kwargs):
        queryset = TaskCategory.objects.all()
        serializer = TaskCategoryListSerializer(queryset, many=True)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

class TaskCategoryRetrieveAPIView(RetrieveAPIView):
    serializer_class = TaskCategoryRetrieveSerializer
    queryset = TaskCategory.objects.all()

    @swagger_auto_schema(tags=['Task Category'])
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk', None)
        category_obj = TaskCategory.objects.filter(pk=pk).first()
        serializer = TaskCategoryRetrieveSerializer(category_obj)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

class TaskCategoryCreateAPIView(CreateAPIView):
    serializer_class = TaskCategoryCreateSerializer
    queryset = TaskCategory.objects.all()

    @swagger_auto_schema(tags=['Task Category'])
    def post(self, request, *args, **kwargs):
        data = request.data
        category_name = data.get('category_name', None)
        category_obj = TaskCategory(category_name=category_name)
        category_obj.save()
        return Response(data={'details': 'Task Category Created'}, status=status.HTTP_201_CREATED)

class TaskCategoryUpdateAPIView(UpdateAPIView):
    serializer_class = TaskCategoryRetrieveSerializer
    queryset = TaskCategory.objects.all()

    @swagger_auto_schema(tags=['Task Category'])
    def put(self, request, *args, **kwargs):
        data = request.data
        pk = kwargs.get('pk', None)
        # category_obj = TaskCategory.objects.filter(pk=pk).update(**data)
        category_obj = TaskCategory.objects.filter(pk=pk).first()
        category_obj.category_name = data['category_name']
        category_obj.save()
        return Response(data={'details': 'Task Category Updated'}, status=status.HTTP_200_OK)

class TaskCategoryDeleteAPIView(DestroyAPIView):
    queryset = TaskCategory.objects.all()

    @swagger_auto_schema(tags=['Task Category'])
    def delete(self, request, *args, **kwargs):
        pk = kwargs.get('pk', None)
        category_obj = TaskCategory.objects.filter(pk=pk).first()
        category_obj.delete()
        return Response(data={'details': 'Task Category objects deleted.'}, status=status.HTTP_204_NO_CONTENT)

class TaskListAPIView(ListAPIView):
    serializer_class = TaskListSerializer
    queryset = Task.objects.all()

    @swagger_auto_schema(tags=['Task'])
    def get(self, request, *args, **kwargs):
        queryset = Task.objects.all()
        serializer = TaskListSerializer(queryset, many=True)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

class TaskRetrieveAPIView(RetrieveAPIView):
    serializer_class = TaskRetrieveSerializer
    queryset = Task.objects.all()

    @swagger_auto_schema(tags=['Task'])
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk', None)

        task_obj = Task.objects.filter(pk=pk).first()
        serializer = TaskRetrieveSerializer(task_obj)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

class TaskCreateAPIView(CreateAPIView):
    serializer_class = TaskCreateSerializer
    queryset = Task.objects.all()

    @swagger_auto_schema(tags=['Task'])
    def post(self, request, *args, **kwargs):
        data = request.data
        title = data.get('title', None)
        category = data.get('category', None)
        assigned_by = data.get('assigned_by', None)
        assigned_to = data.get('assigned_to', None)
        details = data.get('details', None)
        task_status = data.get('task_status', None)
        start_date = data.get('start_date', None)
        end_date = data.get('start_date', None)

        task_obj = Task(title=title, category_id=category, assigned_by_id=assigned_by, assigned_to_id=assigned_to,
                        details=details, task_status=task_status, start_date=start_date, end_date=end_date)
        task_obj.save()
        serializer = TaskRetrieveSerializer(task_obj)
        return Response(data=serializer.data, status=status.HTTP_201_CREATED)

class TaskUpdateAPIView(UpdateAPIView):
    serializer_class = TaskCreateSerializer
    queryset = Task.objects.all()

    @swagger_auto_schema(tags=['Task'])
    def put(self, request, *args, **kwargs):
        data = request.data
        pk = kwargs.get('pk', None)
        # Task.objects.filter(pk=pk).update(**data)
        # abc = task_obj
        # return Response(data={'details': 'Task Category Updated'}, status=status.HTTP_200_OK)

        task_obj = Task.objects.filter(pk=pk).first()
        task_obj.title = data['title']
        # task_obj.save()
        task_obj.category.category_name = data['category']
        task_obj.category.save()
        task_obj.start_date = data['start_date']
        # task_obj.save()
        task_obj.end_date = data['end_date']
        # task_obj.save()
        task_obj.assigned_by.id = data['assigned_by']
        task_obj.assigned_to.save()
        task_obj.assigned_to.id = data['assigned_to']
        task_obj.assigned_to.save()
        task_obj.details = data['details']
        # task_obj.save()
        task_obj.task_status = data['task_status']
        task_obj.save()
        return Response(data={'details': 'Task details Updated'}, status=status.HTTP_200_OK)

class TaskDeleteAPIView(DestroyAPIView):
    queryset = Task.objects.all()

    @swagger_auto_schema(tags=['Task'])
    def delete(self, request, *args, **kwargs):
        pk = kwargs.get('pk', None)
        task_obj = Task.objects.filter(pk=pk).first()
        task_obj.delete()
        return Response(data={'details': 'Task object deleted. '}, status=status.HTTP_204_NO_CONTENT)


#Made by Joe Kaninberg
import sys
from random import randint

#according to the paper, (0,0,1) is S, (1,0,0) is B, (0,1,0) is F.

#Assuming undirected graphs:
class Graph:
	def __init__(self,forget,verify,alpha,beta):
		self.verts=set()
		self.states={}
		self.edges={}
		self.edgeNums=0
		self.alpha=alpha
		self.beta=beta
		self.pforget=forget
		self.pverify=verify
	def AddNode(self,index):
		if not index in self.verts:
			self.verts.add(index)
			self.edges[index]=set()
			self.states[index]=(0,0,1)
	def AddEdge(self,index1,index2):
		self.AddNode(index1)
		self.AddNode(index2)
		self.edges[index1].add(index2)
		self.edges[index2].add(index1)
		self.edgeNums+=1
	#returns the number of neighbors in each state.
	def neighborsStates(self,index):
		neighbors=self.edges[index]
		numF=0
		numB=0
		numS=0
		for adj in neighbors:
			result=self.states[adj]
			if result[0]==1:
				numB+=1
			elif result[1]==1:
				numF+=1
			else:
				numS+=1
		return (numB,numF,numS)
	def probabilityB(self,index,fx,gx):
		return fx*self.states[index][2]+(1-self.pforget-self.pverify)*self.states[index][0]
	def probabilityF(self,index,fx,gx):
		return gx*self.states[index][2]+self.pverify*self.states[index][0]+(1-self.pforget)*self.states[index][1]
	def probabilityS(self,index,fx,gx):
		return self.pforget*(self.states[index][0]+self.states[index][1])+(1-fx-gx)*self.states[index][2]
	def getProbabilities(self,index):
		fx=self.function_fx(index)
		gx=self.function_gx(index)
		return (self.probabilityB(index,fx,gx),self.probabilityF(index,fx,gx),self.probabilityS(index,fx,gx))
	def function_fx(self,index):
		currentNeighborStates=self.neighborsStates(index)
		if currentNeighborStates[0]==0 and currentNeighborStates[1]==0:
			return 0
		return self.beta*((currentNeighborStates[0]*(1+self.alpha))/( (currentNeighborStates[0]*(1+self.alpha)) + (currentNeighborStates[1]*(1-self.alpha)) ) )
	def function_gx(self,index):
		currentNeighborStates=self.neighborsStates(index)
		if currentNeighborStates[0]==0 and currentNeighborStates[1]==0:
			return 0
		return self.beta*((currentNeighborStates[1]*(1-self.alpha))/( (currentNeighborStates[0]*(1+self.alpha)) + (currentNeighborStates[1]*(1-self.alpha)) ) )
	def do_simulation(self,amount_of_rounds):
		rounds=0
		while rounds<amount_of_rounds:
			next_states={}
			amounts={0:0,1:0,2:0}
			for vert in self.verts:
				probs=self.getProbabilities(vert)
				randChance=randint(0,100)/100
				index=0
				total=0
				while index<3 and total<randChance:
					total+=probs[index]
					if total>=randChance:
						break
					index+=1
				if index==3:
					index-=1
				next_state=(0,0,1)
				if index==0:
					next_state=(1,0,0)
				elif index==1:
					next_state=(0,1,0)
				next_states[vert]=next_state
				amounts[index]+=1
			self.states=next_states
			rounds+=1
			print("S:",amounts[2],"\tF:",amounts[1],"\tB:",amounts[0])
def Loadgraph(filename, fg, vf,alpha,beta):
	g=Graph(fg,vf,alpha,beta)
	with open(filename,'r') as fp:
		line=fp.readline()
		while line:
			if line[0]!='#':
				indicies=line.split('\t')
				g.AddEdge(int(indicies[0].strip()),int(indicies[1].strip()))
			line=fp.readline()
	return g

if len(sys.argv)!=6:
	print("Improper usage, call as",sys.argv[0],"graph_filename p_forget_value p_verify_value alpha_value beta_value\n")
	sys.exit(1)

pforget=float(sys.argv[2])
pverify=float(sys.argv[3])
alpha=float(sys.argv[4])
beta=float(sys.argv[5])
graph=Loadgraph(sys.argv[1],pforget,pverify,alpha,beta)
infected=randint(0,len(graph.verts))
graph.states[infected]=(1,0,0)
graph.do_simulation(1000)
#nif -> number of fact checker neighbors.
#nib -> number of believer neighbors.


from sys import argv, exit
import pandas as pd
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from gensim.models.doc2vec import Doc2Vec

class Classificador():

    def salvar_scores(self, scores, label):

        pt = open('d2v_clfs/pvdm_random_'+label+'.txt', 'w')
        for score in scores:
            pt.write(str(score)+'\n')
        pt.close()

    def pre_proc(self, df):

        # Pré processamento do dataset.
        # Definindo quais vão ser as categorias enviadas para classificação.
        categorias_finais = ['tec', 'esporte',
                             'ilustrada', 'mercado', 'poder', 'mundo']

        categorias_diluidas = list(
            set(df['category']) - set(categorias_finais))

        # Agrupando coluna de tecnologia com ciência.
        df["category"] = df["category"].replace("ciencia", "tec")

        # Criando dataset com categorias finais para teste
        docs_classif = df  # .copy(deep=True)#.sample(frac=1)
        for cat in categorias_diluidas:
            docs_classif = docs_classif[docs_classif['category'] != cat]

        docs_sem_classif = df  # .copy(deep=True)#.sample(frac=1)
        for cat in categorias_finais:
            docs_sem_classif = docs_sem_classif[docs_sem_classif['category'] != cat]

        return docs_classif, docs_sem_classif

    def dataset_treino(self, docs_classif, docs_sem_classif, modelo):

        vetores_treino = list()
        classes_treino = list()

        vetores_teste = list()
        classes_teste = list()

        for index, row in docs_classif.iterrows():
            try:
                vetores_treino.append(modelo['DOC_'+str(index)])
                classes_treino.append(row['category'])
            except:
                print("PADRÃO: ", index)

        for index, row in docs_sem_classif.iterrows():
            try:
                vetores_teste.append(modelo['DOC_'+str(index)])
                classes_teste.append(row['category'])
            except:
                print("GENÉRICOS: ", index)

        docs_classif["vetor"] = vetores_treino
        docs_classif["classe"] = classes_treino
        docs_sem_classif["vetor"] = vetores_teste

    def cross_valid(self, clf, vetores, classes):

        scores = cross_val_score(clf, vetores, classes,
                                 cv=5, scoring='f1_macro', n_jobs=25)
        print("F1-Measure: ", scores)

    def avaliar_classificacao(self, treino, alg=1, vezes=1):

        scores = []

        label = {
            1: "LogisticRegression",
            2: "GaussianNB",
            3: "MLPClassifier",
            4: "AdaBoostClassifier",
            5: "LinearSVC",
            6: "KNeighborsClassifier",
            7: "RandomForestClassifier"
        }

        for i in range(0, vezes):

            # Regressão Logistica.
            if alg == 1:
                print("Aplicando: ", label[alg])
                clf = LogisticRegression(
                    random_state=1, max_iter=300, solver='saga')
            # Naive Bayes.
            elif alg == 2:
                print("Aplicando: ", label[alg])
                clf = GaussianNB()
            # Rede Neural.
            elif alg == 3:
                print("Aplicando: ", label[alg])
                clf = MLPClassifier(solver='adam', hidden_layer_sizes=(
                    100), learning_rate_init=0.002, learning_rate='adaptive', max_iter=200)
            elif alg == 4:
                print("Aplicando: ", label[alg])
                clf = clf = AdaBoostClassifier(n_estimators=100)
            elif alg == 5:
                print("Aplicando: ", label[alg])
                clf = LinearSVC(random_state=0, tol=1e-5)
            elif alg == 6:
                print("Aplicando: ", label[alg])
                clf = KNeighborsClassifier(n_neighbors=3)
            elif alg == 7:
                print("Aplicando: ", label[alg])
                clf = RandomForestClassifier(max_depth=2, random_state=0)

            kf = KFold(n_splits=5, shuffle=True, random_state=None)
            scores += list(cross_val_score(clf, list(treino['vetor']), list(treino['classe']),
                                cv=kf, scoring='f1_macro', n_jobs=25))
        print("F1-Measure: ", label[alg], scores)
        self.salvar_scores(scores, label[alg])

    def treinar(self, treino, alg):

        # Regressão Logistica.
        if alg == 1:
            clf=LogisticRegression(
                random_state=0, max_iter=150, solver='sag')  # C=0.8)
        # Naive Bayes.
        elif alg == 2:
            clf=GaussianNB()
        # Rede Neural.
        elif alg == 3:
            clf=MLPClassifier(solver='adam', hidden_layer_sizes=(100), learning_rate_init=0.002,
                                learning_rate='adaptive', max_iter=200)
        else:
            clf=RandomForestClassifier(max_depth=2, random_state=0)

        self.cross_valid(clf, list(treino['vetor']), list(treino['classe']))

    def classificar(self, treino, teste, alg):

        # Regressão Logistica.
        if alg == 1:
            clf=LogisticRegression(
                random_state=0, max_iter=150, solver='sag')  # , C=0.8)
        # Naive Bayes.
        elif alg == 2:
            clf=GaussianNB()
        # Rede Neural.
        elif alg == 3:
            clf=MLPClassifier(solver='adam', alpha=1e-5,
                                hidden_layer_sizes=(100, 2), random_state=1, learning_rate='adaptive', max_iter=300)
        elif alg == 5:
            print("Aplicando: LinearSVC")
            clf=LinearSVC(random_state=0, tol=1e-5)
        else:
            clf=RandomForestClassifier(max_depth=2, random_state=0)

        clf.fit(list(treino['vetor']), list(treino['classe']))

        # Classificando as novas amostras.
        classificados=clf.predict(list(teste['vetor']))
        teste["classe"]=classificados
        ndf_teste=teste.drop(columns=['vetor'])
        ndf_treino=treino.drop(columns=['vetor'])
        ndf=pd.concat([ndf_treino, ndf_teste]).sort_index()
        ndf.to_csv('../dados/classificados.csv', index=False)

    def executar(self, dataset, caminho_modelo, operacao='treinar', alg=1):

        modelo=Doc2Vec.load(caminho_modelo)
        df=pd.read_csv(dataset)
        docs_classif, docs_sem_classif=self.pre_proc(df)
        self.dataset_treino(docs_classif, docs_sem_classif, modelo)
        if operacao == 'treinar':
            self.treinar(docs_classif, alg)
        elif operacao == 'classif':
            self.classificar(docs_classif, docs_sem_classif, alg)
        else:
            vezes = 6
            for algoritimo in range(1, 8):
                self.avaliar_classificacao(docs_classif, algoritimo, vezes)

if __name__ == '__main__':

    c=Classificador()
    c.executar(argv[1], argv[2], argv[3], int(argv[4]))


import sys
import ac
import acsys

def acMain(ac_version):
        global shadow, label, appWindow
        appWindow = ac.newApp("SimpleSpeedo")
        ac.setSize(appWindow, 120, 60)
        ac.setBackgroundOpacity(appWindow,0)
        ac.drawBorder(appWindow,0)
        ac.setIconPosition(appWindow, 0, -9000)
        -ac.setTitlePosition(appWindow,0, -9000)

        shadow = ac.addLabel(appWindow, "?")
        ac.setFontSize(shadow, 44)
        ac.setPosition(shadow, 61, 1)
        ac.setFontColor(shadow, 0.2, 0.2, 0.2, 0.2)
        ac.setFontAlignment(shadow, "center")

        label = ac.addLabel(appWindow, "?")
        ac.setFontSize(label, 44)
        ac.setPosition(label, 60, 0)
        ac.setFontColor(label, 1, 1, 1, 0.4)
        ac.setFontAlignment(label, "center")

        return "SimpleSpeedo"

def acUpdate(deltaT):
        global speed, appWindow
        ac.setBackgroundOpacity(appWindow,0)
        speed = ac.getCarState(0, acsys.CS.SpeedKMH)
        ac.setText(label, "{}".format(int(speed)))
        ac.setText(shadow, "{}".format(int(speed)))


import pandas as pd
import math

# Load result table

result_table = pd.read_csv('Formula Concept Discovery/FCR_search_rankings_10examples.csv')

def get_MRR_score(source):
	# from https://en.wikipedia.org/wiki/Mean_reciprocal_rank
	# \text{MRR} = \frac{1}{|Q|} \sum_{i=1}^{|Q|}
	ranks = source['ranks']
	Q = len(ranks)
	MRR_score = 1/Q*[1/rank for rank in ranks]
	return MRR_score

print('end')


import tensorflow as tf

class FullyConnected:
    def __init__(self, num_hidden_layers, num_hidden_nodes,
                 num_final_nodes, activation="relu"):
        """
        :param num_layers (int): number of hidden layers of data in a batch
        :param num_nodes (int): number of nodes in a hidden layer
        :param activation (str): name of activation function
        :return:
        """

        self.num_hidden_layers = num_hidden_layers
        self.num_hidden_nodes = num_hidden_nodes
        self.num_final_nodes = num_final_nodes
        self.activation = activation

    def get_model(self, input_dim, batch_size=None):
        """

        :param input_dim: size of vector
        :param batch_size:
        :return:
        """
        model = tf.keras.models.Sequential()
        model.add(tf.keras.Input(shape=(input_dim,),
                                 batch_size=batch_size)
                  )

        for layer_index in range(self.num_hidden_layers):
            model.add(tf.keras.layers.Dense(
                units=self.num_hidden_nodes,
                activation=self.activation
            ))

        model.add(tf.keras.layers.Dense(
                units=self.num_final_nodes,
                activation=self.activation
            ))

        return model


# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : 获取ts文件.py
# Time       ：2022/1/13 11:38
# Author     ：jieWei_yang
# version    ：python 3.7.9
# Description：
"""

import time
import os
import requests
import random

header = {
    'Referer': 'https://www.amazon.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}

# https://www.amazon.com/vdp/0d4986d43b804dd9bc69332cf7ce7948?ref=dp_vse_rvc_1

for i in range(1,13):
    count=str(i).rjust(2,'0')
    url=f'https://m.media-amazon.com/images/S/vse-vms-transcoding-artifact-us-east-1-prod/4a7210ba-4bd1-4c82-b7a0-d011f8370c02/default.jobtemplate.hls360_000{count}.ts'
    file_name=url.split('/')[-1]
    print(file_name)
    with open(f'./ts格式/{file_name}', 'wb') as f:
        r = requests.get(url,   headers=header)
        r.raise_for_status()
        r.encoding = 'utf-8'
        f.write(r.content)
        time.sleep(2)


n, m = map(int, input().split())

array = []
for i in range(n): # 화폐 종류에 대해 입력받기
    array.append(int(input()))

d = [10001] * (m + 1)
d[0] = 0 # 0원을 만드는데는 0개의 화폐가 필요
for i in range(n): # 화폐 종류의 갯수
    for j in range(array[i], m + 1):
        d[j] = min(d[j], d[j - array[i]] + 1)

if d[m] == 10001:
    print(-1)
else:
    print(d[m])

효율적인 화폐 구성.py


"""A Space Invader variant.
"""
import pygame
from pygame.constants import (
    QUIT, KEYDOWN, KEYUP, K_LEFT, K_RIGHT, K_ESCAPE, K_SPACE)
import os
from random import randint

class Settings:
    """Project global informations.

    This static class contains informations that
    are used on many places in the project.
    """
    window_width = 1000
    window_height = 650
    window_border = 10
    enemy_bottom_border = window_height - 80
    enemy_nof_cols = 13
    file_path = os.path.dirname(os.path.abspath(__file__))
    image_path = os.path.join(file_path, "images")
    score = (100, 50, 10)
    max_bombs = 6
    max_rockets = 6

class Background(object):
    """Bitmap class to manage the background of the game.
    """

    def __init__(self, filename):
        """Constructor

        Loading and scaling the background image.
        Args:
            filename (string): name (without path) of the background bitmap
        """
        self.image = pygame.image.load(
            os.path.join(Settings.image_path, filename))
        self.image = pygame.transform.scale(
            self.image, (Settings.window_width, Settings.window_height)).convert()
        self.rect = self.image.get_rect()

    def draw(self, screen):
        """Draws the bitmap on a surface (screen).

        Args:
            screen (python.Surface): surface object - usally the screen.
        """
        screen.blit(self.image, self.rect)

class Defender(pygame.sprite.Sprite):
    """Defender sprite class.
    """

    def __init__(self, filename):
        """Constructor

        Loading, converting, and scaling the defender images.
        Args:
            filename (string): name (without path) of the defender alive bitmap
        """
        super().__init__()
        self.image_alive = pygame.image.load(
            os.path.join(Settings.image_path, filename))
        self.image_alive = pygame.transform.scale(
            self.image_alive, (30, 30)).convert_alpha()
        self.rect = self.image_alive.get_rect()
        self.rect.centerx = Settings.window_width // 2
        self.rect.bottom = Settings.window_height - Settings.window_border
        self.direction = 0
        self.speed = 3
        self.image = self.image_alive

        self.images_explosion = []
        self.imageindex_explosion = -1
        for i in range(1, 5):
            bitmap = pygame.image.load(os.path.join(
                Settings.image_path, "explosion0{0}.png".format(i)))
            bitmap = pygame.transform.scale(bitmap, (30, 30)).convert_alpha()
            self.images_explosion.append(bitmap)

        self.fliptime_explosion = 3
        self.flipcounter = self.fliptime_explosion

    def update(self):
        """Implemention of the horizontal movements, status and animation
        """
        if self.imageindex_explosion == -1:
            self.image = self.image_alive
        else:
            if self.imageindex_explosion < len(self.images_explosion):
                self.image = self.images_explosion[self.imageindex_explosion]
                if self.flipcounter <= 0:
                    self.imageindex_explosion += 1
                    self.flipcounter = self.fliptime_explosion
                else:
                    self.flipcounter -= 1
            else:
                self.kill()

        newrect = self.rect.move(self.direction * self.speed, 0)
        if newrect.left <= Settings.window_border:
            self.move_stop()
        if newrect.right >= Settings.window_width - Settings.window_border:
            self.move_stop()
        self.rect.move_ip(self.direction * self.speed, 0)

    def move_left(self):
        """Sets the direction to left.
        """
        self.direction = -1

    def move_right(self):
        """Sets the direction to right.
        """
        self.direction = 1

    def move_stop(self):
        """The defender stops.
        """
        self.direction = 0

    def hit_by_bomb(self):
        """Set status to "I was hit by a bomb".
        """
        self.imageindex_explosion = 0
        self.flipcounter = self.fliptime_explosion

class Brick(pygame.sprite.Sprite):
    """One brick of the wall.
    """

    def __init__(self):
        """Constructor
        """
        super().__init__()
        self.images = []
        for index in range(1, 4):
            bitmap = pygame.image.load(os.path.join(
                Settings.image_path, "brick0{0}.png".format(index)))
            bitmap = pygame.transform.scale(bitmap, (10, 10)).convert_alpha()
            self.images.append(bitmap)
        self.imageindex = 0
        self.image = self.images[self.imageindex]
        self.rect = self.image.get_rect()

    def hit(self):
        """The brick was hit by bomb or rocket.
        """
        self.imageindex += 1
        if self.imageindex >= len(self.images):
            self.kill()
        else:
            self.image = self.images[self.imageindex]

class Rocket(pygame.sprite.Sprite):
    """Rockets fired by the defender.
    """

    def __init__(self, filename, defender):
        """Constructor.

        Loading, converting, and scaling the rocket image.
        Args:
            filename (string): name (without path) of the rocket bitmap
            defender (Defender): the defender who launches the rocket
        """
        super().__init__()
        self.image = pygame.image.load(
            os.path.join(Settings.image_path, filename))
        self.image = pygame.transform.scale(
            self.image, (5, 10)).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.centerx = defender.rect.centerx
        self.rect.bottom = defender.rect.top
        self.direction = -1
        self.speed = 3

    def update(self):
        """Updates the position and the status of the rocket.
        """
        newrect = self.rect.move(0, self.direction * self.speed)
        if newrect.bottom > 0:
            self.rect = newrect
        else:
            self.kill()

class Bomb(pygame.sprite.Sprite):
    """Bombs dropped by the enemies
    """

    def __init__(self, filename, enemy):
        """Constructor.

        Loading, converting, and scaling the bomb image.
        Args:
            filename (string): name (without path) of the bomb bitmap
            enemy (Enemy): the enemy who drops the bomb
        """
        super().__init__()
        self.image = pygame.image.load(
            os.path.join(Settings.image_path, filename))
        self.image = pygame.transform.scale(
            self.image, (10, 10)).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.centerx = enemy.rect.centerx
        self.rect.top = enemy.rect.bottom
        self.direction = 1
        self.speed = 3

    def update(self):
        """Updates the position and the status of the bomb.
        """
        newrect = self.rect.move(0, self.direction * self.speed)
        if newrect.top < Settings.window_height - Settings.window_border:
            self.rect = newrect
        else:
            self.kill()

class Enemy(pygame.sprite.Sprite):
    """Enemy sprite class.

    Direction and speed are organized as static member variables.
    """
    speed_horizontal = 2
    direction_horizontal = 1
    speed_vertical = 0
    direction_vertical = 0

    def __init__(self, filename, aliennumber, colindex, rowindex):
        """Constructor.

        Loading, converting, and scaling the enemy bitmaps.

        Args:
            filename (string): name (without path) of the enemy bitmap
            aliennumber (int): number of the alien (according to the filename)
            colindex (int): number of the column of the sprite
            rowindex (int): number of the row of the sprite
        """
        super().__init__()
        self.images_alive = []
        self.imageindex_alive = -1
        for i in range(1, 3):
            bitmap = pygame.image.load(os.path.join(
                Settings.image_path, filename.format(aliennumber, i)))
            bitmap = pygame.transform.scale(bitmap, (50, 45)).convert_alpha()
            self.images_alive.append(bitmap)
        self.imageindex_alive = 0

        self.images_explosion = []
        self.imageindex_explosion = -1
        for i in range(1, 5):
            bitmap = pygame.image.load(os.path.join(
                Settings.image_path, "explosion0{0}.png".format(i)))
            bitmap = pygame.transform.scale(bitmap, (50, 45)).convert_alpha()
            self.images_explosion.append(bitmap)

        self.image = self.images_alive[self.imageindex_alive]
        self.rect = self.image.get_rect()
        self.distance = 5
        self.score = Settings.score[rowindex//4]
        newx = Settings.window_border + \
            (self.rect.width + self.distance) * colindex
        newy = Settings.window_border + \
            (self.rect.height + self.distance) * rowindex
        self.rect.move_ip(newx, newy)
        Enemy.speed_vertical = self.rect.height // 4
        self.fliptime_alive = 10
        self.flipcounter = self.fliptime_alive
        self.fliptime_explosion = 3

    def update(self):
        """Updates the status an behaviour.

        Implemention of the horizontal and vertical movements and
        if and how the animation runs.
        """
        if self.imageindex_explosion == -1:
            self.image = self.images_alive[self.imageindex_alive]
            if self.imageindex_alive == 1:
                if self.flipcounter <= 0:
                    self.imageindex_alive = 0
                    self.flipcounter = self.fliptime_alive
                else:
                    self.flipcounter -= 1
        else:
            self.imageindex_alive = -1
            if self.imageindex_explosion < len(self.images_explosion):
                self.image = self.images_explosion[self.imageindex_explosion]
                if self.flipcounter <= 0:
                    self.imageindex_explosion += 1
                    self.flipcounter = self.fliptime_explosion
                else:
                    self.flipcounter -= 1
            else:
                self.kill()

        self.rect.move_ip(Enemy.direction_horizontal * Enemy.speed_horizontal,
                          Enemy.direction_vertical * Enemy.speed_vertical)

    @staticmethod
    def switch_horizontal_direction():
        """Switches the horizontal direction from left to right or from right to left.

        It is implemented as a static method because all enemies must switch at the same time.
        """
        Enemy.direction_horizontal *= -1

    @staticmethod
    def switch_vertical_direction():
        """Switches the vertical direction from up to down or from down to up.

        It is implemented as a static method because all enemies must switch at the same time.
        """
        if Enemy.direction_vertical == 0:
            Enemy.direction_vertical = 1
        else:
            Enemy.direction_vertical = 0

    def is_horizontal_border_reached(self):
        """Has the enemy reached left or right borders of the screen.

        Returns:
            bool: True if it has reached the border, otherwise False.
        """
        newrect = self.rect.move(Enemy.direction_horizontal * Enemy.speed_horizontal,
                                 Enemy.direction_vertical * Enemy.speed_vertical)
        if newrect.left <= Settings.window_border:
            return True
        if newrect.right >= Settings.window_width - Settings.window_border:
            return True
        return False

    def is_vertical_border_reached(self):
        """Has the enemy reached upper or lower borders his area.

        Returns:
            bool: True if it has reached the border, otherwise False.
        """
        newrect = self.rect.move(Enemy.direction_horizontal * Enemy.speed_horizontal,
                                 Enemy.direction_vertical * Enemy.speed_vertical)
        if newrect.bottom >= Settings.enemy_bottom_border:
            return True
        return False

    def dropped_bomb(self):
        """Indicates that the enemy has dropped a bomb.
        """
        self.imageindex_alive = 1

    def can_drop(self):
        """Checks if the enemy can drop a bomb.

        Returns:
            bool: True if dropping animation hasn't startet or is finished, otherwise Fals
        """
        return self.flipcounter == self.fliptime_alive

    def hit_by_rocket(self):
        """Indicates that the enemy is hit by a rocket.
        """
        self.imageindex_explosion = 0
        self.flipcounter = self.fliptime_explosion

class Game(object):
    """The game management.

    After creating the game object you start the game with the
    method run().
    """

    def __init__(self):
        """Constructor.
        """
        self.screen = pygame.display.set_mode(
            (Settings.window_width, Settings.window_height))
        self.clock = pygame.time.Clock()
        self.score = 0
        self.font = pygame.font.Font(pygame.font.get_default_font(), 24)
        self.running = False

        self.all_defenders = pygame.sprite.Group()
        self.all_enemies_alive = pygame.sprite.Group()
        self.all_enemies_exploding = pygame.sprite.Group()
        self.all_rockets = pygame.sprite.Group()
        self.all_bombs = pygame.sprite.Group()
        self.all_bricks = pygame.sprite.Group()

        self.background = Background("background03.png")
        self.defender = Defender("defender01.png")
        self.all_defenders.add(self.defender)
        for rowindex in range(0, 8):
            for colindex in range(0, Settings.enemy_nof_cols):
                aliennumber = rowindex // 2
                enemy = Enemy("alienbig0{0}0{1}.png",
                              aliennumber, colindex, rowindex)
                self.all_enemies_alive.add(enemy)

        self.init_bricks()

    def run(self):
        """Starting the game.
        """
        self.running = True
        while self.running:
            self.clock.tick(60)
            self.watch_for_events()
            self.update()
            self.drop_bomb()
            self.collision_rockets()
            self.collision_bombs()
            self.draw()

    def draw(self):
        """Drawing all bitmaps/sprites.

        This method draws all sprites objects and
        blits the text output to the screen.
        After that it flips the doublebuffer.
        """
        self.background.draw(self.screen)
        self.all_defenders.draw(self.screen)
        self.all_enemies_alive.draw(self.screen)
        self.all_enemies_exploding.draw(self.screen)
        self.all_rockets.draw(self.screen)
        self.all_bombs.draw(self.screen)
        self.all_bricks.draw(self.screen)

        text_surface_scrore = self.font.render(
            "Score: {0}".format(self.score), True, (255, 255, 255))
        self.screen.blit(text_surface_scrore,
                         (Settings.window_border, Settings.window_height - 50))
        pygame.display.flip()

    def collision_bombs(self):
        """Game logic: defender and bomb.
        """
        # bomb hits defender
        if pygame.sprite.spritecollide(self.defender, self.all_bombs, True):
            self.defender.hit_by_bomb()

        # bomb hits brick
        for brick in self.all_bricks:
            if pygame.sprite.spritecollide(brick, self.all_bombs, True):
                brick.hit()

    def collision_rockets(self):
        """Game logic: enemies and rockets.
        """
        # Check rockets
        for rocket in self.all_rockets:
            tmp = pygame.sprite.spritecollide(
                rocket, self.all_enemies_alive, False)
            if len(tmp) > 0:
                rocket.kill()
                self.all_enemies_alive.remove(tmp)
                self.all_enemies_exploding.add(tmp)
                for enemy in tmp:
                    enemy.hit_by_rocket()
                    self.score += enemy.score

        # Rocket hits brick
        for brick in self.all_bricks:
            if pygame.sprite.spritecollide(brick, self.all_rockets, True):
                brick.hit()

    def drop_bomb(self):
        """Game logic: when will and how drops a bomb.
        """
        if len(self.all_bombs) < Settings.max_bombs and len(self.all_enemies_alive) > 0:
            enemy_index = randint(0, len(self.all_enemies_alive) - 1)
            enemy = self.all_enemies_alive.sprites()[enemy_index]
            if enemy.can_drop():
                bomb = Bomb("bomb.png", enemy)
                self.all_bombs.add(bomb)
                enemy.dropped_bomb()

    def update(self):
        """Game logic: updates all sprites according to intended behaviour.
        """
        self.defender.update()

        # Update enemies
        has_horizontal_border_reached = False
        for enemy in self.all_enemies_alive:
            if enemy.is_horizontal_border_reached():
                has_horizontal_border_reached = True
                break
        has_vertical_border_reached = False
        for enemy in self.all_enemies_alive:
            if enemy.is_vertical_border_reached():
                has_vertical_border_reached = True
                break

        if has_horizontal_border_reached:
            Enemy.switch_horizontal_direction()
            if not has_vertical_border_reached:
                Enemy.switch_vertical_direction()
        self.all_enemies_alive.update()
        self.all_enemies_exploding.update()
        if has_horizontal_border_reached:
            if not has_vertical_border_reached:
                Enemy.switch_vertical_direction()

        self.all_rockets.update()
        self.all_bombs.update()

    def watch_for_events(self):
        """Listing for key events.

        According of which key went down or up the corresponding
        messages will send to the objects.
        """
        for event in pygame.event.get():
            if event.type == QUIT:
                self.running = False
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    self.running = False
                elif event.key == K_LEFT:
                    self.defender.move_left()
                elif event.key == K_RIGHT:
                    self.defender.move_right()
                elif event.key == K_SPACE:
                    if len(self.all_rockets) < Settings.max_rockets:
                        rocket = Rocket("shoot.png", self.defender)
                        self.all_rockets.add(rocket)
            elif event.type == KEYUP:
                if event.key == K_LEFT or event.key == K_RIGHT:
                    self.defender.move_stop()

    def init_bricks(self):
        """Setup the 4 walls to protect the defender.
        """
        map = []
        map.append("..XXXXXXX..")
        map.append(".XXXX.XXXX.")
        map.append("XXXX...XXXX")
        map.append("XXXX...XXXX")

        startx = 150
        starty = Settings.window_height - 100
        distance = 200
        for wall in range(4):
            for row in range(len(map)):
                for col in range(len(map[row])):
                    if map[row][col] == "X":
                        brick = Brick()
                        brick.rect.left = startx + \
                            (wall * distance) + (col * brick.rect.width)
                        brick.rect.top = starty + (row * brick.rect.height)
                        self.all_bricks.add(brick)

if __name__ == '__main__':
    os.environ['SDL_VIDEO_WINDOW_POS'] = "10, 50"
    #pylint: disable=no-member
    pygame.init()
    #pylint: enable=no-member

    game = Game()
    game.run()

    #pylint: disable=no-member
    pygame.quit()
    #pylint: enable=no-member


from argparse import Namespace
from typing import List
import jsonargparse
from data.preprocessing.pipeline import IDSPipeline, MATPipeline

def parse_args() -> Namespace:
    parser = jsonargparse.ArgumentParser()

    # Paths
    parser.add_argument(
        "-d", "--path", type=str,
        help="Absolute path to original CSV file or path to root directory containing CSV files."
    )
    parser.add_argument(
        "-o", "--output_path", type=str,
        help="Path to the output directory. Folders will be added to this directory."
    )
    parser.add_argument(
        "--output_fname", type=str,
        default="processed.csv",
        help="Output CSV file name."
    )

    parser.add_argument(
        "--nan_atol", type=float,
        default=0.01,
        help="Ratio of NaN values tolerated before dropping the columns"
    )
    parser.add_argument(
        "--negative_atol", type=float,
        default=0.01,
        help="Ratio of negative values tolerated before dropping the columns"
    )
    parser.add_argument(
        "--drop_cols", type=List[str],
        default=None,
        help="The name of the column(s) to be deleted."
    )

    return parser.parse_args()

def main():
    args = parse_args()
    pipeline = IDSPipeline(
        path=args.path,
        output_path=args.output_path,
        output_name=args.output_fname,
        drop_cols=args.drop_cols
    )
    pipeline.process()

if __name__ == "__main__":
    main()


from __future__ import print_function
from flask import Flask
import soco
from threading import Thread

# Page scraping
import requests
from bs4 import BeautifulSoup
from dateutil.parser import parse
import operator

app = Flask(__name__)
print("Server started.")

def get_sonos():
    sonos = soco.discovery.any_soco()
    if not sonos.group:
        return sonos
    return sonos.group.coordinator

def get_latest_playlist():
    base = 'https://www.npr.org/programs/'
    urls = {
        'all_things_considered': 'all-things-considered/',
        'weekend_edition': 'weekend-edition-saturday/',
        'weekend_edition_sunday': 'weekend-edition-sunday/',
        'morning_edition': 'morning-edition/'
    }
    dates = {}
    m3u = {}

    for program, url in urls.items():
        url = base + url
        page = requests.get(url)
        soup = BeautifulSoup(page.text, 'html.parser')
        audioLabels = soup.find_all(class_='audio-tool-label')
        dates[program] = parse(
            soup.find("div", {"class": "current"}).find("time")['datetime'])

        files = []
        for a in audioLabels:
            if (a.text == 'Download'):
                p = a.parent
                href = p.get('href')
                href = href.split('?')[0]
                #story_title = a.find_parent('article', {"class": "rundown-segment"}).find('h3', {"class": "rundown-segment__title"}).find('a').text
                story_title = "Title"
                files.append([href, story_title])

        m3u[program] = files

    # Get latest program
    if m3u["all_things_considered"] == []:
        del dates['all_things_considered']
    if m3u["morning_edition"] == []:
        del dates['morning_edition']
    if m3u["weekend_edition"] == []:
        del dates['weekend_edition']
    if m3u["weekend_edition_sunday"] == []:
        del dates['weekend_edition_sunday']

    if 'morning_edition' in dates and 'all_things_considered' in dates and dates['morning_edition'] == dates['all_things_considered']:
        del dates['morning_edition']
    if 'weekend_edition' in dates and 'all_things_considered' in dates and dates['all_things_considered'] == dates['weekend_edition']:
        del dates['weekend_edition']
    if 'weekend_edition_sunday' in dates and 'all_things_considered' in dates and dates['all_things_considered'] == dates['weekend_edition_sunday']:
        del dates['weekend_edition_sunday']
    latest = max(dates.items(), key=operator.itemgetter(1))[0]

    return m3u[latest]

def start_npr():
    # Grab the Sonos
    s = get_sonos()

    # Create a group with all devices
    s.partymode()

    # Stop and clear queue
    s.stop()
    s.clear_queue()

    # Set volume to 25%
    if not s.group:
        s.volume = 25
    else:
        for g in s.group.members:
            g.volume = 25

    # Send the playlist and start playing
    playlist = get_latest_playlist()
    for mp3 in playlist:
        print("Adding MP3: %s" % mp3)
        s.add_uri_to_queue(mp3[0])
    s.play_from_queue(0)
    return "OK"

@app.route("/listen", methods=['POST','GET'])
def listen():
    print("Starting...")
    Thread(target=start_npr).start()
    return "OK"

@app.route("/pause")
def pause():
    print("Pausing")
    s = get_sonos()
    s.pause()
    return "OK"

@app.route("/resume")
def resume():
    print("Resuming track")
    s = get_sonos()
    s.play()
    return "OK"

@app.route("/skip")
def skip():
    print("Skipping track")
    s = get_sonos()
    s.next()
    return "OK"

@app.route("/volumeup")
def volumeup():
    print("Turning volume up")
    sonos = get_sonos()
    vol = sonos.volume
    sonos.volume = vol+10
    return "OK"

@app.route("/volumedown")
def volumedown():
    print("Turning volume down")
    sonos = get_sonos()
    vol = sonos.volume
    sonos.volume = vol-10
    return "OK"


import sys
import re
import uuid

try:
    file = open('input.txt', 'r')
    string = file.read()
    words = re.split('[ \n]', string)
    unique,li = [],[]
    c_to,c_ing=0,0
    count_dict={}
    word_dict={}
    palindromes=[]
    index=0
    for x in words:
        unique.append(x)
    for x in unique:
        if x.startswith('To'):
            c_to+=1
        if x.endswith('ing'):
            c_ing+=1
        count_dict[x]=unique.count(x)
        word_dict[index]=x
        index+=1
        if x==x[::-1]:
            palindromes.append(x)
    str_max=list(sorted(count_dict.items(),key=lambda x:x[1], reverse=True))[0][0]
    print("count 'To':",c_to,"\ncount 'ing':",c_ing)
    print("repeted string:",str_max)
    print("palindromes:",palindromes)
    print("word dict:",word_dict)
    for i in range(len(words)):
        li.extend(re.split('a|e|i|o|u|A|E|I|O|U', words[i]))
        li=' '.join(li).split()
    for i in range(len(li)):
        if (i+1)%5==0:
            li[i]=li[i].upper()

    filen=str(uuid.uuid4())
    filen+='.txt'
    with open(filen,"x") as f:
        f.write("-".join(li))

except Exception as e:
    print (e)


import argparse
import re
from pathlib import Path

def check(dataset: Path):
    print(f"Start checking tesseract data: {dataset}")

    # Check if all objects has related txt file
    for img_path in dataset.glob('*.png'):
        tesseract_txt_path = dataset / f"{img_path.stem}.gt.txt"

        if not tesseract_txt_path.exists():
            print(
                f"Provide Tesseract {tesseract_txt_path.name} file for object '{img_path.name}'")
            continue

        fixed_img_name = re.sub('[() ]', '_', img_path.name)

        if fixed_img_name != img_path.name:
            print(f"Rename invalid file name {img_path.name} to {fixed_img_name}")
            img_path = img_path.rename(dataset / fixed_img_name)
            tesseract_txt_path = tesseract_txt_path.rename(
                dataset / f"{img_path.stem}.gt.txt")

        # Check how many lines in each tesseract txt file
        with open(tesseract_txt_path, 'r') as txt:
            lines_count = sum(1 for _ in txt)

            if lines_count != 1:
                print(
                    f"'{tesseract_txt_path.name}' file contains wrong line numbers: {lines_count}. Should be 1!")

    print("Tesseract data checked.")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Check tesseract train data')

    parser.add_argument('--dataset',
                        default='./tesseract/eng_seeneva-ground-truth',
                        help='Tesseract train dataset')

    args = parser.parse_args()

    check(Path(args.dataset))


import numpy as np
import h5py
from astropy.time import Time
import os
from joblib import Parallel, delayed

def gps2dyr(time):
    """ Convert GPS time to decimal years. """
    return Time(time, format='gps').decimalyear

def track_type(time,lat,tmax=1):
    """
    Separate tracks into ascending and descending.

    Defines tracks as segments with time breaks > tmax,
    and tests whether lat increases or decreases w/time.
    """
    tracks = np.zeros(lat.shape)  # generate track segment
    tracks[0:np.argmax(np.abs(lat))] = 1  # set values for segment
    i_asc = np.zeros(tracks.shape,dtype=bool)  # output index array

    # Loop trough individual segments
    for track in np.unique(tracks):

        i_track, = np.where(track == tracks)  # get all pts from seg

        if len(i_track) < 2: continue

        # Test if lat increases (asc) or decreases (des) w/ time
        i_min = time[i_track].argmin()
        i_max = time[i_track].argmax()
        lat_diff = lat[i_track][i_max] - lat[i_track][i_min]

        # Determine track type
        if lat_diff > 0:  i_asc[i_track] = True

    return i_asc,np.invert(i_asc)  # index vectors

def process(fname,bbox=None):
    """
    Read 1 ATL06 file and output 6 reduced files.

    Extract variables of interest and separate the ATL06 file
    into each beam (ground track) and ascending/descending orbits.
    """

    # Each beam is a group
    group = ['/gt1l','/gt1r','/gt2l','/gt2r','/gt3l','/gt3r']

    # Loop trough beams
    for k,g in enumerate(group):  # 6 groups for 6 beams

        # -----------------------------------#
        # 1) Read in data for a single beam #
        # -----------------------------------#

        # Load variables into memory (more can be added!)
        with h5py.File(fname,'r') as fi:
            lat = fi[g + '/land_ice_segments/latitude'][:]
            lon = fi[g + '/land_ice_segments/longitude'][:]
            h_li = fi[g + '/land_ice_segments/h_li'][:]
            h_li_sigma = fi[g + '/land_ice_segments/h_li_sigma'][:]
            t_dt = fi[g + '/land_ice_segments/delta_time'][:]
            q_flag = fi[g + '/land_ice_segments/atl06_quality_summary'][:]
            dem_h = fi[g + '/land_ice_segments/dem/dem_h'][:]
            bsnow_h = fi[g + '/land_ice_segments/geophysical/bsnow_h'][:]
            bsnow_conf = fi[g + '/land_ice_segments/geophysical/bsnow_conf'][:]
            bsnow_od = fi[g + '/land_ice_segments/geophysical/bsnow_od'][:]
            cloud_flg_asr = fi[g + '/land_ice_segments/geophysical/cloud_flg_asr'][:]
            cloud_flg_atm = fi[g + '/land_ice_segments/geophysical/cloud_flg_atm'][:]
            msw_flag = fi[g + '/land_ice_segments/geophysical/msw_flag'][:]
            t_ref = fi['/ancillary_data/atlas_sdp_gps_epoch'][:]
            orb = np.full_like(h_li,k)

        # ---------------------------------------------#
        # 2) Filter data according region and quality #
        # ---------------------------------------------#

        # Select a region of interest
        if bbox:
            lonmin,lonmax,latmin,latmax = bbox
            bbox_mask = (lon >= lonmin) & (lon <= lonmax) & \
                        (lat >= latmin) & (lat <= latmax)
        else:
            bbox_mask = np.ones_like(lat,dtype=bool)  # get all

        # Only keep good data, and data inside bbox
        mask = (q_flag == 0) & (np.abs(h_li) < 10e3) & (bbox_mask == 1)

        # Update variables
        lat,lon,h_li,h_li_sigma,t_dt,q_flag,dem_h,bsnow_conf,bsnow_h,bsnow_od,cloud_flg_asr,cloud_flg_atm,msw_flag = \
            lat[mask],lon[mask],h_li[mask],h_li_sigma[mask],t_dt[mask],q_flag[mask],dem_h[mask], \
            bsnow_conf[mask],bsnow_h[mask],bsnow_od[mask], \
            cloud_flg_asr[mask],cloud_flg_atm[mask],msw_flag[mask]

        # Test for no data
        if len(h_li) == 0:
            print('ERROR from read_atl06(): granule found with zero length <<h_li>>\n')
            continue

        # -------------------------------------#
        # 3) Convert time and separate tracks #
        # -------------------------------------#

        # Time in GPS seconds (secs sinde 1980...)
        t_gps = t_ref + t_dt

        # Time in decimal years
        t_year = gps2dyr(t_gps)

        # Determine orbit type
        i_asc,i_des = track_type(t_year,lat)

        # -----------------------#
        # 4) Save selected data #
        # -----------------------#

        # Define output file name
        ofile = fname.replace('.h5','_') + 'reduced.h5'

        # Save variables
        with h5py.File(ofile,'a') as f:
            f[g[1:] + '/orbit'] = orb
            f[g[1:] + '/lon'] = lon
            f[g[1:] + '/lat'] = lat
            f[g[1:] + '/t_year'] = t_year
            f[g[1:] + '/t_sec'] = t_gps
            f[g[1:] + '/q_flag'] = q_flag
            f[g[1:] + '/h_li'] = h_li
            f[g[1:] + '/h_li_sigma'] = h_li_sigma
            f[g[1:] + '/dem_h'] = dem_h
            f[g[1:] + '/bsnow_conf'] = bsnow_conf
            f[g[1:] + '/bsnow_h'] = bsnow_h
            f[g[1:] + '/bsnow_od'] = bsnow_od
            f[g[1:] + '/cloud_flg_asr'] = cloud_flg_asr
            f[g[1:] + '/cloud_flg_atm'] = cloud_flg_atm
            f[g[1:] + '/msw_flag'] = msw_flag
            f[g[1:] + '/trk_type'] = i_asc

    print('out ->',ofile)

def process_all(dir,use_parallel=True,njobs=7):
    """ Processes all .h5 files in the given directory using process().
    """
    all_files = os.listdir(dir)
    os.chdir(dir)

    if '.DS_Store' in all_files: all_files.remove('.DS_Store')
    remove_these = []
    for fn in all_files:
        if '.h5' not in fn:
            remove_these.append(fn)
    for rfn in remove_these:
        all_files.remove(rfn)

    if not use_parallel:
        for fn in all_files:
            process(fn)
    else:
        Parallel(n_jobs=njobs,verbose=5)(delayed(process)(fn) for fn in all_files)

def read_h5(fname,vnames=[]):
    """ Simple HDF5 reader. """
    with h5py.File(fname,'r') as f:
        beam_keys = f.keys()
        data = dict()
        for bk in beam_keys:
            data[bk] = dict()
            if len(vnames) == 0:
                vnames = f[bk].keys()
            for vn in vnames:
                data[bk][vn] = f[bk][vn][:]
    return data


import json
from Device import *
from enum import IntEnum

class StateRinging(IntEnum):
    # 서브폰의 호출 상태
    IDLE = 0
    FRONT = 1  # 현관문 초인종
    COMMUNAL = 2  # 공동출입문

class StateDoorLock(IntEnum):
    Unsecured = 0
    Secured = 1
    Jammed = 2
    Unknown = 3

class SubPhone(Device):
    state_streaming: int = 0
    state_ringing: StateRinging = StateRinging.IDLE
    state_ringing_prev: StateRinging = StateRinging.IDLE
    state_doorlock: StateDoorLock = StateDoorLock.Secured

    def __init__(self, name: str = 'SubPhone', index: int = 0, room_index: int = 0):
        super().__init__(name, index, room_index)
        self.dev_type = DeviceType.SUBPHONE
        self.unique_id = f'subphone_{self.room_index}_{self.index}'
        self.mqtt_publish_topic = f'home/state/subphone/{self.room_index}/{self.index}'
        self.mqtt_subscribe_topic = f'home/command/subphone/{self.room_index}/{self.index}'
        self.setHomeAssistantConfigTopic()
        self.sig_state_streaming = Callback(int)
        self.streaming_config = {
            'conf_file_path': '',
            'feed_path': '',
            'input_device': '/dev/video0',
            'frame_rate': 24,
            'width': 320,
            'height': 240,
        }

    def setDefaultName(self):
        self.name = 'SubPhone'

    def publishMQTT(self):
        if self.mqtt_client is not None:
            obj = {
                "streaming_state": self.state_streaming,
                "doorlock_state": self.state_doorlock.name,  # 도어락은 상태 조회가 안되고 '열기' 기능만 존재한다
            }
            self.mqtt_client.publish(self.mqtt_publish_topic, json.dumps(obj), 1)

            if self.state_ringing != self.state_ringing_prev:  # 초인종 호출 상태 알림이 반복적으로 뜨는 것 방지
                writeLog(f"Ringing Publish: Prev={bool(self.state_ringing.name)}, Current={self.state_ringing_prev.name}", self)
                if self.state_ringing in [StateRinging.FRONT, StateRinging.COMMUNAL]:
                    # obj = {"doorbell_state": 'ON'}
                    self.mqtt_client.publish(self.mqtt_publish_topic + '/doorbell', 'ON', 1)
                else:
                    # obj = {"doorbell_state": 'OFF'}
                    self.mqtt_client.publish(self.mqtt_publish_topic + '/doorbell', 'OFF', 1)

    def setHomeAssistantConfigTopic(self):
        self.mqtt_config_topic = f'{self.ha_discovery_prefix}/lock/{self.unique_id}/config'

    def configMQTT(self):
        obj = {
            "name": self.name + "_doorlock",
            "object_id": self.unique_id + "_doorlock",
            "unique_id": self.unique_id + "_doorlock",
            "state_topic": self.mqtt_publish_topic,
            "command_topic": self.mqtt_subscribe_topic,
            "value_template": '{{ value_json.state }}',
            "payload_lock": '{ "state": "Secured" }',
            "payload_unlock": '{ "state": "Unsecured" }',
            "state_locked": "Secured",
            "state_unlocked": "Unsecured",
            "icon": "mdi:door-closed-lock"
        }
        if self.mqtt_client is not None:
            self.mqtt_client.publish(self.mqtt_config_topic, json.dumps(obj), 1, True)

    def updateState(self, _: int, **kwargs):
        streaming = kwargs.get('streaming')
        if streaming is not None:
            self.state_streaming = streaming
            self.sig_state_streaming.emit(self.state_streaming)
            self.publishMQTT()
        ringing_front = kwargs.get('ringing_front')
        if ringing_front is not None:
            if ringing_front:
                self.state_ringing = StateRinging.FRONT
            else:
                self.state_ringing = StateRinging.IDLE
            self.publishMQTT()
            self.state_ringing_prev = self.state_ringing
        ringing_communal = kwargs.get('ringing_communal')
        if ringing_communal is not None:
            if ringing_communal:
                self.state_ringing = StateRinging.COMMUNAL
            else:
                self.state_ringing = StateRinging.IDLE
            self.publishMQTT()
            self.state_ringing_prev = self.state_ringing
        doorlock = kwargs.get('doorlock')
        if doorlock is not None:
            self.state_doorlock = StateDoorLock(doorlock)
            self.publishMQTT()
        writeLog(f"Streaming: {bool(self.state_streaming)}, Ringing: {self.state_ringing.name}, DoorLock: {self.state_doorlock.name}", self)

    def makePacketCommon(self, header: int) -> bytearray:
        return bytearray([0x7F, max(0, min(0xFF, header)), 0x00, 0x00, 0xEE])

    def makePacketSetVideoStreamingState(self, state: int) -> bytearray:
        if state:
            if self.state_ringing == StateRinging.FRONT:
                # 현관 초인종 카메라 영상 서브폰 우회
                return self.makePacketCommon(0xB7)
            elif self.state_ringing == StateRinging.COMMUNAL:
                # 공동현관문 영상 우회
                return self.makePacketCommon(0x5F)
            else:
                # 단순 문열기용 (주방 서브폰 활성화)
                return self.makePacketCommon(0xB9)
        else:
            if self.state_ringing == StateRinging.FRONT:
                # 현관 초인종 카메라 영상 서브폰 우회 종료
                return self.makePacketCommon(0xB8)
            elif self.state_ringing == StateRinging.COMMUNAL:
                # 공동현관문 영상 우회 종료
                return self.makePacketCommon(0x60)
            else:
                # 단순 문열기용 (주방 서브폰 활성화)
                return self.makePacketCommon(0xBA)

    def makePacketOpenFrontDoor(self) -> bytearray:
        # 현관 초인종 카메라 영상이 서브폰으로 우회된 상태에서 도어락 해제
        return self.makePacketCommon(0xB4)

    def makePacketOpenCommunalDoor(self) -> bytearray:
        # 공동현관문 호출 후 카메라 영상이 우회된 상태에서 열림 명령
        return self.makePacketCommon(0x61)

    def makePacketSetOuterDoorCamState(self, state: int) -> bytearray:
        if state:
            pass
        else:
            pass


from biostar.accounts.settings import *
from biostar.emailer.settings import *

# Django debug flag.
DEBUG = True

# Private key used to validate external logins
LOGIN_PRIVATE_KEY = SECRET_KEY

RECAPTCHA_PUBLIC_KEY = ""
RECAPTCHA_PRIVATE_KEY = ""

SEARCH_CHAR_MIN = 2

SOCIALACCOUNT_EMAIL_VERIFICATION = None
SOCIALACCOUNT_EMAIL_REQUIRED = False
SOCIALACCOUNT_QUERY_EMAIL = True

# Maximum number of projects allowed
MAX_PROJECTS = 20

# Root directory relative to the job path usd to store logs.
JOB_LOGDIR = 'runlog'

# Stdout filename for each job relative to the log directory.
JOB_STDOUT = os.path.join(JOB_LOGDIR, 'stdout.txt')
JOB_STDERR = os.path.join(JOB_LOGDIR, 'stderr.txt')

# Maximum amount of data allowed
MAX_DATA = 100

# Maximum amount of items per clipboard
MAX_CLIPBOARD = 5

# Name of the clipboard inside of sessions
CLIPBOARD_NAME = "clipboard"

# Maximum amount of total running jobs allowed for non-staff user.
MAX_RUNNING_JOBS = 5

# Maximum amount of cumulative uploaded files a user is allowed, in mega-bytes.
MAX_UPLOAD_SIZE = 10

# Deployment specific parameters.
PROTOCOL = "http"
HTTP_PORT = '8000'
BASE_URL = f"{PROTOCOL}://{SITE_DOMAIN}:{HTTP_PORT}"

FTP_HOST = "localhost"
FTP_PORT = 8021

# Should the site allow signup.
ALLOW_SIGNUP = False

# Allow users to toggle their status moderator
ALLOW_SELF_MODERATE = False

# Maximum size of each file upload in MB
MAX_FILE_SIZE_MB = 300

LOGIN_REDIRECT_URL = "/project/list/private/"
ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTS = True

ENGINE_APPS = [
    'biostar.recipes.apps.EngineConfig',
    'django.contrib.redirects'
]

INSTALLED_APPS = DEFAULT_APPS + ENGINE_APPS + ACCOUNTS_APPS + EMAILER_APP

# Additional middleware.
MIDDLEWARE += [
    'biostar.recipes.middleware.recipes_middleware',
]

# The URL configuration.
ROOT_URLCONF = 'biostar.recipes.urls'

# Add another context processor to first template.
TEMPLATES[0]['OPTIONS']['context_processors'] += [
    'biostar.recipes.context.engine'
]

# Directory where files are extracted to
EXTRACT_TO = join(BASE_DIR, "export", "extracted")
os.makedirs(EXTRACT_TO, exist_ok=True)

# The location of application specific data.
LOCAL_ROOT = join(BASE_DIR, 'export', 'local')

# The location for the table of contents.
TOC_ROOT = join(MEDIA_ROOT, 'tocs')

IMPORT_ROOT_DIR = join(BASE_DIR, 'export', 'local')

# Configure language detection
LANGUAGE_DETECTION = ['en']

# Ensure that the table of directory exists.
os.makedirs(TOC_ROOT, exist_ok=True)

# Sendfile settings go here.
SENDFILE_ROOT = MEDIA_ROOT
SENDFILE_URL = '/protected/'

COUNT_INTERVAL_WEEKS = 10000

SESSION_ENGINE = "django.contrib.sessions.backends.db"

# SENDFILE_BACKEND = "sendfile.backends.nginx"

SENDFILE_BACKEND = "sendfile.backends.development"

STATICFILES_FINDERS += [
    'compressor.finders.CompressorFinder',
]

# Tries to load up secret settings from a predetermined module

try:
    from conf.run.secrets import *
    print(f"Loaded secrets from: conf.run.secrets")
except Exception as exc:
    print(f"Secrets module not imported: {exc}")

# Enable debug toolbar specific functions
if DEBUG_TOOLBAR and 'debug_toolbar' not in INSTALLED_APPS:
    INSTALLED_APPS.extend([
        'debug_toolbar',
    ])
    MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware')

# Add static serving capability
if DEBUG is False:
    print("Whitenoise static serve enabled (pip install whitenoise)")
    MIDDLEWARE.append('whitenoise.middleware.WhiteNoiseMiddleware')


# From the python package
from collections import OrderedDict
import argparse
import os
import numpy as np
import time

# From pytorch package
import torch
import torch.nn as nn
from torch.autograd import Variable

# From the directory
from model import VariousRelationsNet
from data_loader import tensor_data_loader

# Define sub-functions (helpers)
def zero_pad_batch(batch_list, max_length, batch_type=None):  # List of sentences that are in varying size
    zero_padded_tensors = []
    if batch_type == 'story':
        for batch in batch_list:
            padded = []
            for sentence in batch:
                tensor = torch.LongTensor(sentence)
                padded += [
                    torch.cat([tensor, tensor.new(max_length - tensor.size(0), *tensor.size()[1:]).zero_()]).view(1,
                                                                                max_length)]
            zero_padded_tensors += [torch.cat(padded, dim=0)]  # [LongTensor Batch x Max_Sent_Len of the Batch]
        zero_padded_batch = torch.cat(zero_padded_tensors, 0)
    elif batch_type == 'question':
        padded = []
        for question in batch_list:
            tensor = torch.LongTensor(question)
            padded += [torch.cat([tensor, tensor.new(max_length - tensor.size(0), *tensor.size()[1:]).zero_()]).view(1,
                                                                                max_length)]
        zero_padded_batch = torch.cat(padded, dim=0)  # [LongTensor Batch x Max_Sent_Len of the Batch]

    return zero_padded_batch

def validate(vrn, validation_batches, log_file):
    print ("\n\n(PAUSE) Calculating Validation Accuracy")
    num_validation_batches = len(validation_batches)
    total = []
    corrects = []
    accu = []
    for i, batch in enumerate(validation_batches):
        story_batch = Variable(batch[0][0].long())  # [LongTensor (Num_Sent_Combined, Max_Sent_Len)]
        question_batch = Variable(batch[1][0].long())  # [LongTensor (Batch, Max_Question_Len)]
        answer_batch = Variable(batch[2].long())  # [LongTensor (N)]
        story_batch_lengths = batch[3][0].long()

        # Get sentence lengths in the batch.
        story_sent_batch_lengths = [list(map(int, story_length.tolist()[0])) for story_length in batch[4]]  # List of sentences lengths of each batch
        question_batch_lengths = list(map(int, batch[5].tolist()[0]))  # List of question lengths

        if torch.cuda.is_available():
            story_batch = story_batch.cuda()
            question_batch = question_batch.cuda()
            answer_batch = answer_batch.cuda()

        # Get the prediction (output) from the model
        predicted_batch = vrn(story_batch, story_batch_lengths, story_sent_batch_lengths, question_batch,
                              question_batch_lengths)

        # Calculate accuracy
        predicted_batch_np = torch.max(predicted_batch, 1)[1].cpu().data.numpy().squeeze()
        scores = np.array(predicted_batch_np == answer_batch.data.tolist()[0], int)
        total += [len(scores)]
        corrects += [sum(scores)]
        accu += [corrects[i] * 1.0 / total[i]]
        print ('\nVALIDATION Step [%d/%d]: ACCU %.4f' % (i, num_validation_batches, accu[i]))

    validation_avg_accu = sum(accu) / float(len(accu))
    return validation_avg_accu

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-train_data_path', default='../datasets/train_qa1.txt')
    parser.add_argument('-test_data_path', default='../datasets/test_qa1.txt')
    parser.add_argument('-save_path', default='./save')
    parser.add_argument('-save_every_epoch', default=True, action='store_false')
    parser.add_argument('-save_uniquely', default=False, action='store_true')
    parser.add_argument('-save_start_epoch', default=20)
    parser.add_argument('-batch_size', default=32)
    parser.add_argument('-lr_rate', default=0.0001)
    parser.add_argument('-num_epochs', default=100)
    parser.add_argument('-comment', default='None')
    args = parser.parse_args()

    # Load training data and answer vocab
    training_batches, train_answer_vocab = tensor_data_loader(args.train_data_path, int(args.batch_size), shuffle=True)
    validation_batches, validation_answer_vocab = tensor_data_loader(args.test_data_path, int(args.batch_size))
    num_training_batches = len(training_batches)

    # Load vocabs
    w2i = np.load('../datasets/word2idx.npy')
    i2w = np.load('../datasets/idx2word.npy')
    vocab_size = len(w2i.tolist())

    # Hyperparameters
    hidden_size = 1024
    max_num_hiddens = 25
    lstm_n_layers = 2
    lstm_directions = 2

    assert train_answer_vocab == validation_answer_vocab, \
        "Train and validation answer vocab size (%d and %d) do not match." % (len(train_answer_vocab), len(validation_answer_vocab))

    # Define model configurations (i.e. hyperparameters)
    vrn_config = OrderedDict([('input_vocab_size', vocab_size),
                              ('question_vocab_size', vocab_size),
                              ('output_vocab_size', len(validation_answer_vocab)),
                              ('hidden_size', hidden_size),
                              ('max_num_hiddens', max_num_hiddens),
                              ('lstm_n_layers', lstm_n_layers),
                              ('lstm_directions', lstm_directions),
                              ('batch_size', int(args.batch_size))
                              ])

    config_log = ("Train Data Path: %s\n" % args.train_data_path
                  + "Test Data Path: %s\n" % args.test_data_path
                  + "Save Every Epoch: %s\n" % args.save_every_epoch
                  + "Save Unique: %s\n" % args.save_every_epoch
                  + "Save Start Epoch: %d\n" % int(args.save_start_epoch)
                  + "Learning Rate: %f\n" % float(args.lr_rate)
                  + "Num_Epoch: %s\n" % int(args.num_epochs)
                  + "Batch_Size: %d \n" % int(args.batch_size)

                  + "\nInput Vocab Size: %d \n" % vocab_size
                  + "Question Vocab SIze: %d \n" % vocab_size
                  + "Output Vocab Size: %d \n" % len(validation_answer_vocab)
                  + "Hidden_Size: %d \n" % hidden_size
                  + "Max_Num_Hiddens: %d \n" % max_num_hiddens
                  + "LSTM_N_Layers: %d \n" % lstm_n_layers
                  + "LSTM_Directions: %d \n" % lstm_directions

                  + "\nComment: %s" % args.comment
                  )

    # Load model
    vrn = VariousRelationsNet(vrn_config=vrn_config)
    if torch.cuda.is_available():
        print("CUDA enabled.")
        vrn.cuda()

    # Define loss function
    criterion = nn.CrossEntropyLoss()

    # Define optimizer
    optimizer = torch.optim.SGD(
        vrn.parameters(),
        lr=float(args.lr_rate)
    )

    QA_task_number = args.train_data_path.split('/')[-1].split('.')[0].split('_')[1]

    # Prepare a txt file to print training log
    if args.save_uniquely is True:
        args.save_path = '%s/%02d%02d%02d%02d' % (args.save_path, time.localtime()[1], time.localtime()[2],
                                                  time.localtime()[3], time.localtime()[4])

    if not os.path.exists(args.save_path):
        print ("\nCreated a directory (%s) for saving logs and trained models." % args.save_path)
        os.makedirs(args.save_path)

    save_model_path = (args.save_path + '/' + QA_task_number)

    if not os.path.exists(save_model_path):
        print ("\nSaving model in %s for QA Task %s." % (save_model_path, QA_task_number))
        os.makedirs(save_model_path)

    log_file = open('%s/%s_train_log.txt' % (args.save_path, QA_task_number), 'w')

    # Print Log
    print (config_log)
    log_file.write(config_log)
    log_file.flush()

    for epoch in range(int(args.num_epochs)):
        total = []
        corrects = []
        accu = []
        avg_accu = []
        for i, batch in enumerate(training_batches):
            story_batch = Variable(batch[0][0].long())  # [LongTensor (Num_Sent_Combined, Max_Sent_Len)]
            question_batch = Variable(batch[1][0].long())  # [LongTensor (Batch, Max_Question_Len)]
            answer_batch = Variable(batch[2].long())  # [LongTensor (N)]
            story_batch_lengths = batch[3][0].long()

            # Get sentence lengths in the batch.
            story_sent_batch_lengths = [list(map(int, story_length.tolist()[0])) for story_length in batch[4]]  # List of sentences lengths of each batch
            question_batch_lengths = list(map(int, batch[5].tolist()[0]))  # List of question lengths

            contexts = Variable(batch[0][0].long())  # [LongTensor (Batch * Num Sentences for Each Context, Max_Sent_Len)]
            questions = Variable(batch[3][0].long())  # [LongTensor (Batch)]
            answers = Variable(batch[4][0].long())  # [LongTensor (Batch, Max_Question_len)]
            context_num_sent = batch[1][0]  # [FloatTensors (Batch)]
            context_sent_lengths = batch[2]  # List (Batch Size) of [FloatTensors (1, Num_Sent)]

            if torch.cuda.is_available():
                story_batch = story_batch.cuda()
                question_batch = question_batch.cuda()
                answer_batch = answer_batch.cuda()

            optimizer.zero_grad()

            # Get the prediction (output) from the model
            predicted_batch = vrn(story_batch, story_batch_lengths, story_sent_batch_lengths, question_batch, question_batch_lengths)

            # Calculate Loss
            answer_batch = answer_batch.contiguous().view(-1).long()
            loss = criterion(predicted_batch, answer_batch)

            # Perform backprop
            loss.backward()
            optimizer.step()

            # Calculate accuracy
            predicted_batch_np = torch.max(predicted_batch, 1)[1].cpu().data.numpy().squeeze()
            scores = np.array(predicted_batch_np == answer_batch.data.tolist(), int)
            total += [len(scores)]
            corrects += [sum(scores)]
            accu += [corrects[i]*1.0/total[i]]

            # Print log info to both console and file
            if i % int(args.batch_size) == 0:
                print("\n\n#####################################################")
                log = ('\nEpoch [%d/%d], Step [%d/%d], Loss: %.4f, Accu: %.4f'
                       % (epoch, int(args.num_epochs), i, num_training_batches, loss.data[0], accu[i]))
                print(log)
                log_file.write("{}".format(log))

            print ('\nTraining Step %d: LOSS %.4f \t ACCU %.4f' % (i, loss.data.tolist()[0], accu[i]))

        if (epoch >= int(args.save_start_epoch)) or (args.save_every_epoch is True):
            # For each epoch calculate train avg accu
            train_avg_accu = sum(accu) / float(len(accu))
            train_avg_log = ('\nTrain Average Accuracy: %.4f' % train_avg_accu)
            log_file.write(train_avg_log)

            # For each epoch calculate validation avg accu
            validation_avg_accu = validate(vrn, validation_batches, log_file)
            validation_log = ('\n---- Validation Average Accuracy: %.4f\n' % validation_avg_accu)
            print (validation_log)
            log_file.write(validation_log)
            log_file.flush()

            save_model = '%s/%s_epoch_%d_acc_%1.3f.pth' % (save_model_path,
                                                           QA_task_number,
                                                           epoch,
                                                           validation_avg_accu)
            print ("\n\nSaving the model: %s" % save_model)
            torch.save(obj=vrn, f=save_model)

if __name__ == '__main__':
    main()


from socket import *

server_socket = socket()
server_socket.bind(("0.0.0.0",8888))
server_socket.listen(5)
header = ""
while (1):
    (client_socket, client_adress) = server_socket.accept()
    try:
        request = str(client_socket.recv(1000).decode())
        splt = request.split(" ")
        message = "you bad boy \r\n"
        if(splt[0] != "GET"):
            message = "youre messing \r\n"
        elif(splt.__len__() != 2):
            message = "youre the dancing queen \r\n"
        elif(splt[1] in ["\r", "\n", "\r\n", "", " ", None]):
            message = "youre joking \r\n"
        else:
            try:
                    splt[1] = splt[1].split("\r")[0]
                    splt[1] = splt[1].split("\n")[0]
                    splt[1] = splt[1].split("\r\n")[0]
                    file = open(splt[1], "r").read()
                    header = "HTTP/1.0 200 Document Follows\r\nContent-length: "  + str(len(file)) + "\r\n\r\n"
                    message = header + file + " \r\n"
            except FileNotFoundError:
                message = "HTTP/1.0 404 Not Found \r\n"
        message_bytes = bytes(message, "utf-8")
        client_socket.sendall(message_bytes)
        client_socket.close()
    except UnicodeDecodeError:
        client_socket.close()


from fastapi import APIRouter

from city_details.api.api_v1.routes import health_checker_router, ui_router, city_router

__author__ = "Rohit Warke"
__date_created__ = "2021-08-05"
__purpose__ = "The routers are required to add here. Within each router, we add the routes (endpoints)"

router = APIRouter()
router.include_router(city_router.CityRouter.router, tags=["city-details"])
router.include_router(health_checker_router.router, tags=["maintenance"])
router.include_router(ui_router.UIRouter.router, tags=["ui-portal"])


'''
CS5001.38359.202230 - SEC 05 - John Wilder
Final - Gradebook Test
clancy.co@northeastern.edu (002781018)
3 MAY 22

You must implement a testing class that thoroughly tests
the methods described in gradebook.py

You do not need to implement the methods, the description given
in the comments are sufficient for creating tests
'''

from gradebook import Gradebook
import unittest

class GradebookTest(unittest.TestCase):
    '''
    GradebookTest extends the Test the TestCase class to test the
    functionality of the Gradebook test class.
    '''

    def test_init(self):
        '''Test the initiation and attributes of the gradebook class'''

        # instantiate an object
        gradebook = Gradebook("cs5001", 2022, "Spring", "Wilder", ['Connor'])

        # test attributes equals
        self.assertEqual(gradebook.course_name, "cs5001")
        self.assertEqual(gradebook.year, 2022)
        self.assertEqual(gradebook.semester, "Spring")
        self.assertEqual(gradebook.instructor, "Wilder")
        self.assertListEqual(gradebook.students, ["Connor"])

        # test not equals
        self.assertNotEqual(gradebook.course_name, "cs5002")
        self.assertNotEqual(gradebook.year, 2021)
        self.assertNotEqual(gradebook.semester, "Summer")
        self.assertNotEqual(gradebook.instructor, "Clancy")
        self.assertNotEqual(gradebook.students, ["Wilder"])

    def test_init_errors(self):
        '''Test to make sure errors are raised when the should be'''

        # test name string error
        with self.assertRaises(ValueError):
            Gradebook(1, 2022, "Summer", "Wilder", ["Connor"])

        # test year not string
        with self.assertRaises(ValueError):
            Gradebook("cs5001", "2022", "Summer", "Wilder", ["Connor"])

        # test year not float
        with self.assertRaises(ValueError):
            Gradebook("cs5001", 2022.0, "Summer", "Wilder", ["Connor"])

        # test year year before 2000
        with self.assertRaises(ValueError):
            Gradebook("cs5001", 1999, "Summer", "Wilder", ["Connor"])

        # test that semester error catching works
        with self.assertRaises(ValueError):
            Gradebook("cs5001", 2022, "Winter", "Wilder", ['Connor'])

        # test that instructor must be a string
        with self.assertRaises(ValueError):
            Gradebook("cs5001", 2022, "Winter", 1, ['Connor'])

        # test that students must be a list
        with self.assertRaises(ValueError):
            Gradebook("cs5001", 2022, "Winter", "Wilder", 'Connor')

    def test_average_grade(self):
        '''Test to make sure adding and average grade works as expected'''

        # instantiate a gradebook object
        gradebook = Gradebook("cs5001", 2022, "Spring", "Wilder", ['Connor'])

        # create grades to add
        grades1 = {'Connor': 0}
        grades2 = {'Connor': 100}

        # add grades to the object
        gradebook.add_grade(grades1)
        gradebook.add_grade(grades2)

        # test to make sure the average is correct
        self.assertEqual(gradebook.average_grade(), {'Connor': 50.0})

        # test grade letter
        self.assertEqual(gradebook.letter_grade(200), {'Connor': 'F'})
        self.assertEqual(gradebook.letter_grade(100), {'Connor': 'A'})

    def test_add_grade_erros(self):
        '''Tests to make sure add_grade only accepts dictionaries'''

        # instantiate a gradebook object
        gradebook = Gradebook("cs5001", 2022, "Spring", "Wilder", ['Connor'])

        # create grades to add
        grades1 = 100

        # add grades to the object
        with self.assertRaises(ValueError):
            gradebook.add_grade(grades1)

    def test_str(self):
        '''Test to make sure the string functionality works'''

        # instantiate a gradebook object
        gradebook = Gradebook("cs5001", 2022, "Spring", "Wilder", ['Connor'])

        # variables
        divider = '-' * 20
        test_string = ''

        # gradebook metadata
        test_string += 'course name: ' + 'cs5001' + '\n'
        test_string += 'year:        ' + str(2022) + '\n'
        test_string += 'semester:    ' + 'Spring' + '\n'
        test_string += 'instructor:  ' + 'Wilder' + '\n'
        test_string += '\n'
        test_string += 'students'
        test_string += '\n'
        test_string += divider + '\n'
        test_string += '    Connor:[]'+ '\n'

        self.assertEqual(str(gradebook), test_string)

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


# Setup and login
import json
import os
import sys
import signal

class State:
    BEGIN = 0
    PENDING_URLS = 1
    PENDING_ITEMS = 2
    END = 3
    PENDING_URLS_ITEMS = 4

def get_job_template(job_name):
    return {
            "state": State.BEGIN,
            "name": job_name,
            "urls_queued": [],
            "urls_error": [],
            "execution_time": 0
        }

def save_job(file_job, job):
    with open(file_job, 'w') as f:
        json.dump(job, f, ensure_ascii=False, indent=4)

def load_job(file_job):
    with open(file_job) as f:
        return json.load(f)

def close_job_scrapper(is_completed, df):
    """Close the job. Requires global vars

    Args:
        is_completed (bool): _description_
        df (DataFrame): _description_
    """
    global scrapper_reference
    global file_job, job
    print(f"Stopping scraper...Completed: {is_completed} DF type: {type(df)}")
    scrapper_reference.stop = True
    print("Saving job...")

    pending = []
    if not is_completed:
        index_seen = scrapper_reference.cache.get("index_seen",-1)
        pending = scrapper_reference.cache.get("urls",[])
        if len(pending) > 0:
            pending = pending[index_seen+1:]

        df = scrapper_reference.cache.get("df", None)

    job['urls_queued'] = pending

    job['urls_error'] = scrapper_reference.cache.get("urls_error", [])
    save_job(file_job, job)

    if df is not None:
        folder_shards = os.path.join("shards")
        if not os.path.isdir(folder_shards):
            os.mkdir(folder_shards)

        df.to_csv(f"{folder_shards}/db_job-{job['name']}_items-run-{job['execution_time']}.csv", index=False)

    print(f"Closing with: {len(scrapper_reference.cache.get('urls',[])) -len(job['urls_queued'])} completed, {len(job['urls_queued'])} pending urls and {len(job['urls_error'])} error urls")

def signal_handler(sig, frame):
    close_job_scrapper(False, None)

    try:
        sys.exit()
    except:
        print("Problems with sys.exit!")
        sys.exit()

def process_job_scrapper(scrapper, fj, j, timers: dict):
    """Process_job in every item. The scrapper must be logged in if needed. And instance
    should have been open.

    Args:
        scrapper (_type_): _description_
        job (_type_): _description_
        timers (dict): _description_
    """
    global scrapper_reference
    global file_job, job
    file_job = fj
    job = j

    scrapper_reference = scrapper
    signal.signal(signal.SIGINT, signal_handler)

    state = job["state"]
    if state == State.BEGIN or state == State.PENDING_ITEMS:
        scrapper.stop = False
        urls_items = job["urls_queued"]
        urls_items.extend(job["urls_error"])  # try again the errors
        job["urls_error"] = []
        job["execution_time"] += 1

        print(f"Pending work...{len(urls_items)} urls in queue")

        df, completed = scrapper.scrape_items(timers, urls=urls_items)

        print("scrape_items ended")

        if completed and len(scrapper.cache["urls_error"]) == 0:
            job["state"] = State.END

        close_job_scrapper(completed, df)
    elif state == State.END:
        print("Work already completed!")
    else:
        print(f"Can't handle this state: {state}")


"""
@leofansq
https://github.com/leofansq
"""
import cv2

from rgblgray import rgblgray

def main(filename):
    """
    Main function
    """
    # Read image
    print ("Processing {} ...".format(filename))
    img = cv2.imread(filename)

    # BGR 2 RGB
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # BGR 2 RGB

    # Call function: rgblgray
    img_gray_a = rgblgray(img, 'average')
    img_gray_NTSC = rgblgray(img, 'NTSC')

    # Save image
    cv2.imwrite("{}_average.png".format(filename[:-4]), img_gray_a)
    cv2.imwrite("{}_NTSC.png".format(filename[:-4]), img_gray_NTSC)

if __name__ == "__main__":

    main("lena512color.tiff")
    main("mandril_color.tif")


import datetime
from operator import itemgetter

from qgis import core, gui
# import qgis.PyQt.QtWidgets as QtWidgets

from ..plugin_dialogs.line_in_polygon_dialog import LineInPolygonDialog
from ..help_tools.config_reader import ConfigWorker
from ..help_tools.help_func import validate_db_and_project_path, get_geometry_layer_from_db
from ..plugin_modules.line_to_polygon_module.dr_line_to_polygon import LineToPolygon
from ..help_tools.help_func import LineToPolygonData

class LineInPolygonTool:
    """Class for make polygons from lines."""

    def __init__(self, iface, parent_window):
        self.iface: gui.QgisInterface = iface
        self.qgis_project: core.QgsProject = core.QgsProject().instance()
        self.line_in_poly_dlg = LineInPolygonDialog(parent=parent_window)
        self.config = ConfigWorker(header="plugin_configs").config_reader
        self.error_feature_list: list[dict] = []
        self.result_lines: list[dict] = []

    def setup_combobox(self):
        possible_nodes_num = tuple(range(1, 8))
        self.line_in_poly_dlg.NodeNumComboBox.addItems(map(str, possible_nodes_num))
        self.line_in_poly_dlg.buffer_size.setText("0")
        self.line_in_poly_dlg.angle_size.setText("0")

    def make_result_statistic(self):
        result: str = (
            f"В таблице {self.error_feature_list[0]['layer_name']} "
            f"некорректные атрибутивные данные у "
            f"{self.error_feature_list[0]['wrong_feature']} объектов.\n"
            f"Id объектов: {self.error_feature_list[0]['features_id']}.\n"
            f"Количество корректных объектов: {self.error_feature_list[0]['corr_features']}.\n"
            f"В таблице {self.error_feature_list[1]['layer_name']} "
            f"найдено совпадений по атрибутивным данным у "
            f"{self.error_feature_list[1]['feature_count']} объектов.\n"
            "_____________________________\n"
            # f"Объекты с буфером: {self.error_feature_list[2]['f_width']}.\n"
            # f"Объекты без буфера: {self.error_feature_list[2]['nf_width']}.\n"
            # f"Id объектов без буфера: {self.error_feature_list[2]['nb_feature_id']}.\n"
        )
        self.line_in_poly_dlg.tabWidget.setCurrentIndex(1)
        self.line_in_poly_dlg.textResult.insertPlainText(result)

    def _get_geo_quarters_polygon(
            self,
            address_attributes: list[str, ...]
    ) -> dict[tuple[int, ...], core.QgsFeature]:
        geo_quarters_polygon_layer: core.QgsVectorLayer = get_geometry_layer_from_db(
            qgis_project=self.qgis_project,
            db_combobox=self.line_in_poly_dlg.LineInPolyDBcomboBox,
            geom_type=3
        )

        address_attributes: list[str, ...] = address_attributes.copy()
        address_attributes.remove("allotment")

        quarters_features: dict[tuple[int, ...], core.QgsFeature] = {}

        for feature in geo_quarters_polygon_layer.getFeatures():
            feature_key: tuple[int, ...] = tuple(feature[f_attr] for f_attr in address_attributes)
            quarters_features[feature_key] = feature

        return quarters_features

    def _make_prepared_data_allotments_features(
            self,
            address_attributes: list[str, ...]
    ) -> None:
        priority_attribute: str = "ground_category_code"
        data_allotment_layer: core.QgsVectorLayer = get_geometry_layer_from_db(
            qgis_project=self.qgis_project,
            db_combobox=self.line_in_poly_dlg.LineInPolyDBcomboBox,
            layer_name="data_allotments"
        )
        data_allotments_errors: dict = {
            "layer_name": data_allotment_layer.name(),
            "feature_count": 0,
            "features_id": []
        }
        data_allotments_features: dict[tuple, core.QgsFeature] = {}

        for feature in data_allotment_layer.getFeatures():
            feature_key: tuple[int, ...] = tuple(feature[f_attr] for f_attr in address_attributes)
            data_allotments_features[feature_key] = feature

        for feature_data in self.result_lines:
            feature_key: tuple[int, ...] = feature_data["address_attributes"]
            if feature_key in data_allotments_features:
                data_allotment_feature: core.QgsFeature = data_allotments_features[feature_key]
                feature_data["d_m13_allotment_id"] = data_allotment_feature.id()
                feature_data["feature_priority"] = data_allotment_feature[priority_attribute]
                data_allotments_errors["feature_count"] += 1

        self.error_feature_list.append(data_allotments_errors)

    def _make_prepared_data_m13_features(self) -> None:
        data_m13_layer: core.QgsVectorLayer = get_geometry_layer_from_db(
            qgis_project=self.qgis_project,
            db_combobox=self.line_in_poly_dlg.LineInPolyDBcomboBox,
            layer_name="data_m13"
        )
        attributes: tuple[str, ...] = ("allotment_id", "width", "ord")
        f_req = core.QgsFeatureRequest().setFilterExpression('"ord"=0')

        data_m13_features: dict[int, int] = {}

        for feature in data_m13_layer.getFeatures(f_req):
            data_m13_features[feature[attributes[0]]] = feature[attributes[1]] // 10

        for feature_data in self.result_lines:
            line_width: int = data_m13_features[feature_data["d_m13_allotment_id"]]
            feature_data["width"] = line_width

    def _make_geo_allotments_lines_features(
            self,
            address_attributes: list[str, ...]
    ) -> core.QgsVectorLayer:
        geo_allotments_line_layer: core.QgsVectorLayer = get_geometry_layer_from_db(
            qgis_project=self.qgis_project,
            db_combobox=self.line_in_poly_dlg.LineInPolyDBcomboBox,
            geom_type=1
        )
        line_feature_error: dict = {
            "layer_name": geo_allotments_line_layer.name(),
            "wrong_feature": 0,
            "features_id": [],
            "corr_features": 0
        }

        for feature in geo_allotments_line_layer.getFeatures():
            line_feature_data: dict = {}
            feature_key: tuple[int, ...] = tuple(feature[f_attr] for f_attr in address_attributes)
            if core.NULL not in feature_key:
                line_feature_data["address_attributes"] = feature_key
                line_feature_data["line_feature"] = feature
                self.result_lines.append(line_feature_data)
            else:
                line_feature_error["features_id"].append(feature.id())
                line_feature_error["wrong_feature"] += 1

        line_feature_error[
            "corr_features"
        ] = geo_allotments_line_layer.featureCount() - line_feature_error["wrong_feature"]
        self.error_feature_list.append(line_feature_error)

        return geo_allotments_line_layer

    def _sort_lines(self) -> list[dict]:
        lines_features: list[dict] = self.result_lines.copy()
        result_list: list[dict] = []

        for feature_data in lines_features:
            if feature_data["feature_priority"] == 2109:
                result_list.append(feature_data)
                lines_features.remove(feature_data)

        for feature_data in lines_features:
            if feature_data["feature_priority"] == 2110:
                result_list.append(feature_data)
                lines_features.remove(feature_data)
        # result_list.extend(list(sorted(lines_features, key=itemgetter("width"), reverse=True)))

        return result_list

    def get_prepared_data(self) -> dict:
        address_attributes: list[str] = self.config["address_attributes"]

        quarters_layer_features = self._get_geo_quarters_polygon(
            address_attributes=address_attributes
        )
        geo_allotments_lines: core.QgsVectorLayer = self._make_geo_allotments_lines_features(
            address_attributes=address_attributes
        )
        self._make_prepared_data_allotments_features(
            address_attributes=address_attributes
        )
        self._make_prepared_data_m13_features()

        for feature_data in self.result_lines:
            quarter_feature_key: tuple[int, ...] = feature_data["address_attributes"][:-1]
            feature_data["quarter_feature"] = quarters_layer_features[quarter_feature_key]

        result_sorted_lines: list[dict] = self._sort_lines()

        result: dict = {
            "crs": geo_allotments_lines.crs(),
            "field_list": geo_allotments_lines.fields().toList(),
            "data_line_to_polygon": LineToPolygonData(
                line_features=[f["line_feature"] for f in result_sorted_lines],
                buffers_size=[f["width"] for f in result_sorted_lines],
                quarters_features=[f["quarter_feature"] for f in result_sorted_lines]
            )
        }
        self.make_result_statistic()

        return result

    def line_to_polygon(self):
        line_worker = LineToPolygon()

        line_data_to_polygon: dict = self.get_prepared_data()
        current_polygon_features = line_worker.line_to_polygon(
            line_data_to_polygon["data_line_to_polygon"]
        )
        new_layer = core.QgsVectorLayer(
            "Polygon?crs=epsg:4326",
            "Buffered_lines",
            "memory"
        )
        if current_polygon_features.polygon_features:
            new_layer.startEditing()
            new_layer.setCrs(line_data_to_polygon["crs"])

            new_layer_dp = new_layer.dataProvider()
            new_layer_dp.addAttributes(line_data_to_polygon["field_list"])

            new_layer_dp.addFeatures(current_polygon_features.polygon_features)
            # new_layer_dp.reloadData()
            new_layer.commitChanges(stopEditing=True)

            self.qgis_project.addMapLayer(new_layer)
            self.qgis_project.reloadAllLayers()

    def line_in_polygon_run(self):
        if not validate_db_and_project_path(
                qgis_project=self.qgis_project,
                dialog=self.line_in_poly_dlg,
                db_combobox=self.line_in_poly_dlg.LineInPolyDBcomboBox,
                iface=self.iface
        ):
            return

        self.line_in_poly_dlg.progressBar.hide()
        self.line_in_poly_dlg.activateWindow()

        self.setup_combobox()
        self.line_in_poly_dlg.LineInPolyCanceleButton.clicked.connect(
            self.line_in_poly_dlg.close
        )
        self.line_in_poly_dlg.LineInPolyButton.clicked.connect(self.line_to_polygon)
        self.line_in_poly_dlg.exec()


import os
from shutil import copyfile

class Copier():
    def __init__(self, sourcePath, destinationPath, sourceDict, destinationDict, number):
        self.source_path = sourcePath
        self.destination_path = destinationPath
        self.source_dict = sourceDict
        self.destination_dict = destinationDict
        self.total = number

    def copy(self):
        count = 0
        for filename, filepath in self.source_dict.items():
            if not self.destination_dict.get(filename):
                count += 1
                print("Copying {} of {}... ".format(count, self.total), end="")
                copyfile(filepath, os.path.join(self.destination_path, filename))
                self.destination_dict[filename] = self.destination_path
                print("done!")
        if count != self.total:
            print("There was an issue...")
        print("Complete!")
        return count