#!/home/namphuong/Code/news-search-engine/myvenv/bin/python
import logging
import sys
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import crawlers
from crawler.newschannels import NewsChannels

class USATodayCrawler:

    def __init__(self, selected_date):
        self.selected_date = selected_date

    def get_link_articles(self):
        aritcle_url_list = []
        page_url_list = []
        targeted_url = self.generate_targeted_url(self.selected_date)
        html = requests.get(targeted_url)
        html.encoding = 'utf-8'
        sp = BeautifulSoup(html.text, 'lxml')
        pagination = sp.find(
            'ul', {'class': 'sitemap-list sitemap-pagination'})

        for page in pagination.find_all('li', {'class': 'sitemap-list-item'}):
            page_url_list.append(page.find('a').get('href'))

        # get article url in page 1
        aritcle_url_list.extend(self.get_link_article_pagination(None, sp))
        # get article url from page 2
        for i in range(1, len(page_url_list)):
            aritcle_url_list.extend(self.get_link_article_pagination(
                page_url_list[i], None))

        return aritcle_url_list

    def get_link_article_pagination(self, page_url=None, soup=None):
        artile_url_list = []
        if (soup == None):
            html = requests.get(page_url)
            html.encoding = 'utf-8'
            soup = BeautifulSoup(html.text, 'lxml')
        main_content = soup.find('div', {'class': 'sitemap-column-wrapper'})
        article_list = main_content.find('ul', {'class': 'sitemap-list'})
        for article_element in article_list.find_all('li'):
            artile_url_list.append(article_element.find('a').get('href'))

        return artile_url_list

    @staticmethod
    def generate_targeted_url(selected_date, page=None):
        year = selected_date.split('-')[0]
        month_num = selected_date.split('-')[1]
        # Mapping month number to full month name in lowercase(Eg: 09 -> september)
        month_name = datetime(1, int(month_num), 1).strftime("%B")
        # Convert number in string to short style (Eg: 01 to 1)
        day = int(selected_date.split('-')[2])
        if (page == None):
            page = 1
        return "https://www.usatoday.com/sitemap/{}/{}/{}/?page={}".format(year, month_name, day, page)

if __name__ == '__main__':
    selected_date = sys.argv[1]
    selected_date_obj = datetime.strptime(selected_date, '%Y-%m-%d')
    if (selected_date_obj <= datetime.today()):
        usa_today_crawler = USATodayCrawler(selected_date)
        url_list = usa_today_crawler.get_link_articles()
        print(len(url_list))
        crawlers.crawl_articles(url_list, NewsChannels.USATODAY.value, selected_date)
    else:
        logging.error("Selected date is invalid")


import json
textData = []
splitTextData = str()
filename = "no_punctuationText"
with open("tweets.json",encoding="UTF-8") as f:
    data = json.load(f)

for i in range(len(data)):
    textData.append(data[i]["text"])
# print(textData)
for i in textData:
    a = i.split()
    for j in a:
        splitTextData += j + " "
# print(splitTextData)

punc = '''!()-[]{};:"'\,<>./?@#$%^&*_~'''
no_punct = ""
for char in splitTextData:
    if char not in punc:
        no_punct += char
f = open(filename, "r+", encoding="UTF-8")

f.write(no_punct)

def readFile(f):
    fileObj = open(f, "r", encoding="UTF-8")
    w = fileObj.read().splitlines()  # puts the file into an array
    fileObj.close()
    return w

newarray = readFile(filename)
splitarr = []
words = {}

for i in newarray:
    a = i.split()
    for j in a:
        if j != "RT":
            splitarr.append(j)

uniquewords = set(splitarr)
for i in uniquewords:
    words[i] = splitarr.count(i)

sort_order = sorted(words.items(), key=lambda x:x[1], reverse=True)
# print(sort_order)
result = []
for i in sort_order:
    a = str(i[0]) + " = " + str(i[1])
    result.append(a)
#print(result)

for i in range(10):
    print(result[i])

# f.truncate(0) #remore everything
f.close()


# coding: UTF-8
import os
import torch
import torch.nn as nn
from script.classifier.nn_classifier import Classifier
from script.classifier.nn_classifier.text_utils import TextCnnDataset
from script.classifier.setting import DataConfig
import torch.nn.functional as F
from script.pretraining import load_emb_vec
from importlib import import_module
from script.pretraining import Vocabulary

class Config(DataConfig):
    """数据配置参数"""

    def __init__(self, **kwargs):
        DataConfig.__init__(self,
                            dataset=kwargs.get('dataset'),
                            cache_dir=kwargs.get('cache_dir'),
                            model_name=kwargs.get('model_name'),
                            adversarial=kwargs.get('adversarial'),
                            )

        if self.emb_path and os.path.exists(self.emb_path):
            init_embedding, self.word2index = load_emb_vec(self.emb_path)
            self.embedding_pretrained = torch.tensor(init_embedding.astype('float32'))
            self.embed = self.embedding_pretrained.size(1)
        else:
            self.embedding_pretrained = None
            self.embed = 300
            vocabulary = Vocabulary(train_files=[self.train_path, self.test_path, self.dev_path],
                                    emb_path=self.emb_path,
                                    vocab_path=self.vocab_path,
                                    max_vocab_size=10000)
            self.word2index = vocabulary.get_vocab()
        self.dropout = 0.5  # 随机失活
        self.require_improvement = 1000  # 若超过1000batch效果还没提升，则提前结束训练
        self.n_vocab = len(self.word2index)+1  # 词表大小，在运行时赋值
        self.num_epochs = 20  # epoch数
        self.batch_size = 128  # mini-batch大小
        self.pad_size = 32  # 每句话处理成的长度(短填长切)
        self.learning_rate = 1e-3  # 学习率
        self.filter_sizes = (2, 3, 4)  # 卷积核尺寸
        self.num_filters = 256

class Model(nn.Module):
    def __init__(self, config):
        super(Model, self).__init__()
        if config.embedding_pretrained is not None:
            self.embedding = nn.Embedding.from_pretrained(config.embedding_pretrained, freeze=False)
        else:
            self.embedding = nn.Embedding(config.n_vocab, config.embed, padding_idx=config.n_vocab - 1)
        self.convs = nn.ModuleList(
            [nn.Conv2d(1, config.num_filters, (k, config.embed)) for k in config.filter_sizes])
        self.dropout = nn.Dropout(config.dropout)
        self.fc = nn.Linear(config.num_filters * len(config.filter_sizes), config.num_classes)

    def conv_and_pool(self, x, conv):
        x = F.relu(conv(x)).squeeze(3)
        x = F.max_pool1d(x, x.size(2)).squeeze(2)
        return x

    def forward(self, x, labels=None):
        out = self.embedding(x[0])
        out = out.unsqueeze(1)
        out = torch.cat([self.conv_and_pool(out, conv) for conv in self.convs], 1)
        out = self.dropout(out)
        out = self.fc(out)

        if labels is not None:
            # loss_mask = labels.gt(-1)
            loss = F.cross_entropy(out, labels)
            return out, loss

        return out

class TextCNNClassifier(Classifier, Config):
    def __init__(self, **kwargs):
        Config.__init__(self, **kwargs)
        self.model = Model(self).to(self.device)
        self.logger.info(self.model.parameters)
        adversarial = kwargs.get('adversarial', 'base')
        at_model = import_module("script.adversarial.{}_model".format(adversarial))
        self.atModel = at_model.ATModel(self.model)
        self.modelDataset = TextCnnDataset
        self._set_optimizer()
        if kwargs.get('evaluate') and os.path.exists(self.save_model_path):
            self.model.load_state_dict(torch.load(self.save_model_path))
            self.model.eval()
            self.logger.info("load {} success".format(self.model_name))

    def _set_optimizer(self, ):
        self.optimizer = torch.optim.Adam(self.model.parameters(), lr=self.learning_rate)


import requests
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.conf import settings

from apps.main.models import Municipio, Coordenada
from apps.main.utils import get_telefonica
from apps.legacy import  models as legacy_m
from django.core import serializers
from django.contrib.auth.models import User

class EscArea(models.Model):
    area = models.CharField(max_length=20)
    esc_area_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = 'Área'
        verbose_name_plural = 'Áreas'

    def __str__(self):
        return self.area

class EscJornada(models.Model):
    """
    Description: Jornada de la escuela
    """
    jornada = models.CharField(max_length=20)
    esc_jornada_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = 'Jornada'
        verbose_name_plural = 'Jornadas'

    def __str__(self):
        return self.jornada

class EscModalidad(models.Model):
    """
    Description: Modalidad de la escuela
    """
    modalidad = models.CharField(max_length=20)
    esc_modalidad_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = 'Modalidad'
        verbose_name_plural = 'Modalidades'

    def __str__(self):
        return self.modalidad

class EscNivel(models.Model):
    """
    Description: Nivel de la escuela
    """
    nivel = models.CharField(max_length=30)
    esc_nivel_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = 'Nivel'
        verbose_name_plural = 'Niveles'

    def __str__(self):
        return self.nivel

class EscPlan(models.Model):
    """
    Description: Plan de la escuela (diario, fin de semana, etc.)
    """
    plan = models.CharField(max_length=20)
    esc_plan_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = 'Plan'
        verbose_name_plural = 'Planes'

    def __str__(self):
        return self.plan

class EscSector(models.Model):
    """
    Description: Sector de la escuela (oficial, privado, etc.)
    """
    sector = models.CharField(max_length=20)
    esc_sector_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = 'Sector'
        verbose_name_plural = 'Sectores'

    def __str__(self):
        return self.sector

class EscStatus(models.Model):
    """
    Description: Status de la escuela (Abierta, cerrada)
    """
    status = models.CharField(max_length=25)
    esc_status_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = 'Status'
        verbose_name_plural = 'Statues'

    def __str__(self):
        return self.status

class Escuela(models.Model):
    """
    Description: Escuela
    """
    codigo = models.CharField(max_length=15, unique=True)
    distrito = models.CharField(max_length=10, null=True, blank=True)
    municipio = models.ForeignKey(Municipio, on_delete=models.PROTECT, related_name='escuelas')
    nombre = models.CharField(max_length=250)
    direccion = models.TextField(verbose_name='Dirección')
    telefono = models.CharField(max_length=12, null=True, blank=True, verbose_name='Teléfono')
    nivel = models.ForeignKey(EscNivel, on_delete=models.PROTECT)
    sector = models.ForeignKey(EscSector, on_delete=models.PROTECT)
    area = models.ForeignKey(EscArea, on_delete=models.PROTECT, verbose_name='Área')
    status = models.ForeignKey(EscStatus, on_delete=models.PROTECT)
    modalidad = models.ForeignKey(EscModalidad, on_delete=models.PROTECT)
    jornada = models.ForeignKey(EscJornada, on_delete=models.PROTECT)
    plan = models.ForeignKey(EscPlan, on_delete=models.PROTECT)
    mapa = models.ForeignKey(Coordenada, null=True, blank=True, on_delete=models.CASCADE)
    esc_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = "Escuela"
        verbose_name_plural = "Escuelas"

    def __str__(self):
        return self.nombre

    def get_absolute_url(self):
        return reverse('escuela_detail', kwargs={'pk': self.id})

    def get_poblacion(self):
        if self.poblaciones.count() > 0:
            return self.poblaciones.latest('fecha').total_alumno
        else:
            return None
    poblacion = property(get_poblacion)

    def get_maestros(self):
        if self.poblaciones.count() > 0:
            return self.poblaciones.latest('fecha').total_maestro
        else:
            return None
    maestros = property(get_maestros)

    def es_equipada(self):
        return True if self.equipamiento.count() > 0 else False
    equipada = property(es_equipada)

    def get_sedes(self):
        from apps.cyd.models import Sede, Asignacion
        resultado = {'listado':[]}
        sedes = []
        for e in self.participantes.all():
            asignaciones = Asignacion.objects.filter(participante=e, participante__escuela=self)
            for asignacion in asignaciones:
                if asignacion.grupo.sede not in sedes:
                    sedes.append(asignacion.grupo.sede)
        for e in Sede.objects.filter(escuela_beneficiada=self.id, activa=True):
            if e not in sedes:
                sedes.append(e)
        for sede in sedes:
            resultado['listado'].append({'sede': sede})

        return resultado

    def get_ficha_escolar(self):
        return 'https://public.tableau.com/views/1-FichaEscolarDatosGenerales/DatosGenerales?CODUDI={}'.format(
            self.codigo)

    def get_matricula_url(self):
        return 'https://public.tableau.com/views/2-FichaaEscolaraMatriculaHistorica/DetallePromovidosRetirados?CODUDI={}&%3AshowVizHome=no'.format(
            self.codigo)

    def get_rendimiento_url(self):
        return 'https://public.tableau.com/views/6-Fichaescolarpruebasestandarizadas/Pruebasestandarizadas?CODUDI={}&%3AshowVizHome=no'.format(
            self.codigo)

    def get_capacitacion(self):
        """Establece una conexión al servidor del SUNI1 para
        obtener datos de capacitación. Esta función será eliminada en futuras versiones.

        Returns:
            dict: Diccionario con el nombre de la escuela y el listado de participantes
        """
        url = '' #settings.LEGACY_URL['cyd_informe']
        if url is not '':
            params = {'udi': self.codigo}
            try:
                resp = requests.post(url=url, data=params)
            except:
                time.sleep(5)

            return resp.json()
        else:
            return [[], []]

    @property
    def info_capacitacion(self):
        try:
            qs = [] #legacy_m.EscuelaSede.objects.filter(udi=self.codigo)
        except:
            qs = []
        return qs

    @property
    def capacitacion(self):
        respuesta = {'capacitada': True if len(self.participantes.all()) > 0 else False, 'participantes':[]}
        if respuesta['capacitada'] is True:
            personas = []
            respuesta['participantes'].append({'listado': serializers.serialize('json', self.participantes.all())})
        return respuesta

    @property
    def equipada(self):
        return self.equipamiento.count() > 0

    def datos_equipamiento(self):
        return self.equipamiento.last()

class EscContactoRol(models.Model):
    """
    Description: Rol para el contacto de escuela
    """
    rol = models.CharField(max_length=30)
    esc_rol_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = "Rol de contacto"
        verbose_name_plural = "Roles de contacto"

    def __str__(self):
        return self.rol

class EscContacto(models.Model):
    escuela = models.ForeignKey(Escuela, related_name="contacto", on_delete=models.CASCADE)
    nombre = models.CharField(max_length=100)
    apellido = models.CharField(max_length=100)
    rol = models.ForeignKey(EscContactoRol, on_delete=models.PROTECT)
    esc_contacto_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = "Contacto"
        verbose_name_plural = "Contactos de escuela"

    def __str__(self):
        return self.nombre + " " + self.apellido

class EscContactoTelefono(models.Model):
    contacto = models.ForeignKey(EscContacto, related_name="telefono", null=True, on_delete=models.CASCADE)
    telefono = models.IntegerField()
    esc_contacto_telefono_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    def get_empresa(self):
        return get_telefonica(self.telefono)
    empresa = property(get_empresa)

    def __str__(self):
        return str(self.telefono)

class EscContactoMail(models.Model):
    """
    Description: Correo de contacto
    """
    contacto = models.ForeignKey(EscContacto, related_name="mail", null=True, on_delete=models.CASCADE)
    mail = models.EmailField(max_length=125)
    esc_contacto_telefono_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    def __str__(self):
        return self.mail

class EscPoblacion(models.Model):
    escuela = models.ForeignKey(Escuela, related_name="poblaciones", on_delete=models.CASCADE)
    fecha = models.DateField(default=timezone.now)

    alumna = models.PositiveIntegerField(
        default=0, verbose_name='Estudiantes mujeres')
    alumno = models.PositiveIntegerField(
        default=0, verbose_name='Estudiantes varones')
    maestra = models.PositiveIntegerField(
        default=0, verbose_name='Docentes mujeres')
    maestro = models.PositiveIntegerField(
        default=0, verbose_name='Dicentes varones')

    total_alumno = models.PositiveIntegerField(
        null=True,
        blank=True,
        verbose_name='Total de estudiantes')
    total_maestro = models.PositiveIntegerField(
        null=True,
        blank=True,
        verbose_name='Total de docentes')
    esc_poblacion_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)

    class Meta:
        verbose_name = "Población de escuela"
        verbose_name_plural = "Poblaciones de escuela"

    def __str__(self):
        return str(self.escuela)[:15] + " - " + str(self.fecha)

    def save(self,*args, **kwargs):
        """En caso de que no se hubiera ingresado el total, suma las cantidades
        detalladas para establecerlo.
        """

        #print(EscPoblacion.objects.last())
        #self.esc_poblacion_creado_por = EscPoblacion.objects.last().esc_poblacion_creado_por
        if self.total_alumno is None or self.total_alumno == 0:
            self.esc_poblacion_creado_por = EscPoblacion.objects.last().esc_poblacion_creado_por
            self.total_alumno = self.alumna + self.alumno
        if self.total_maestro is None or self.total_maestro == 0:
            self.total_maestro = self.maestra + self.maestro

        super(EscPoblacion, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return self.escuela.get_absolute_url()

class EscMatricula(models.Model):

    """Registro histórico de la matrícula de la escuela.
    Cuántos promovidos, retirados y reprobados.
    """
    escuela = models.ForeignKey(Escuela, related_name='matriculas', on_delete=models.CASCADE)
    ano = models.PositiveIntegerField(verbose_name='Año')
    m_promovido = models.PositiveIntegerField(default=0, verbose_name='Mujeres promovidas')
    m_no_promovido = models.PositiveIntegerField(default=0, verbose_name='Mujeres no promovidas')
    m_retirado = models.PositiveIntegerField(default=0, verbose_name='Mujeres retiradas')
    h_promovido = models.PositiveIntegerField(default=0, verbose_name='Hombres promovidos')
    h_no_promovido = models.PositiveIntegerField(default=0, verbose_name='Hombres no promovidos')
    h_retirado = models.PositiveIntegerField(default=0, verbose_name='Hombres retirados')
    esc_matricula_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = "Matrícula de escuela"
        verbose_name_plural = "Matrículas de escuelas"
        ordering = ['escuela', '-ano']

    def __str__(self):
        return '{escuela} - {ano}'.format(
            escuela=self.escuela,
            ano=self.ano)

    @property
    def t_promovido(self):
        return self.m_promovido + self.h_promovido

    @property
    def t_no_promovido(self):
        return self.m_no_promovido + self.h_no_promovido

    @property
    def t_retirado(self):
        return self.m_retirado + self.h_retirado

    @property
    def m_total(self):
        return self.m_promovido + self.m_no_promovido + self.m_retirado

    @property
    def h_total(self):
        return self.h_promovido + self.h_no_promovido + self.h_retirado

    @property
    def m_promovido_p(self):
        if self.m_total != 0:
            return int(self.m_promovido / self.m_total * 100)
        else:
            return 0

    @property
    def m_no_promovido_p(self):
        if self.m_total != 0:
            return int(self.m_no_promovido / self.m_total * 100)
        else:
            return 0

    @property
    def m_retirado_p(self):
        if self.m_total != 0:
           return int(self.m_retirado / self.m_total * 100)
        else:
            return 0

    @property
    def h_promovido_p(self):
        if self.h_total != 0:
            return int(self.h_promovido / self.h_total * 100)
        else:
            return 0

    @property
    def h_no_promovido_p(self):
        if self.h_no_promovido != 0:
            return int(self.h_no_promovido / self.h_total * 100)
        else:
            return 0

    @property
    def h_retirado_p(self):
        if self.h_total != 0:
            return int(self.h_retirado / self.h_total * 100)
        else:
            return 0

    @property
    def t_promovido_p(self):
        if self.t_promovido != 0:
            return int(self.t_promovido / (self.t_promovido + self.t_retirado + self.t_no_promovido) * 100)
        else:
            return 0

    def get_absolute_url(self):
        return self.escuela.get_absolute_url()

class EscRendimientoMateria(models.Model):

    """Materia a evaluar en una prueba de rendimiento académico.
    """

    materia = models.CharField(max_length=20)
    esc_redimiento_materia_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)
    class Meta:
        verbose_name = "Materia de rendimiento"
        verbose_name_plural = "Materias de rendimiento"

    def __str__(self):
        return self.materia

class EscRendimientoAcademico(models.Model):

    """Pruebas estandarizadas del rendimiento académico de la escuela.
    """

    escuela = models.ForeignKey(Escuela, related_name='rendimientos', on_delete=models.CASCADE)
    materia = models.ForeignKey(EscRendimientoMateria, related_name='registros', on_delete=models.CASCADE)
    ano = models.PositiveIntegerField(verbose_name='Año')
    insatisfactorio = models.DecimalField(max_digits=5, decimal_places=2)
    debe_mejorar = models.DecimalField(max_digits=5, decimal_places=2)
    satisfactorio = models.DecimalField(max_digits=5, decimal_places=2)
    excelente = models.DecimalField(max_digits=5, decimal_places=2)
    no_evaluado = models.DecimalField(max_digits=5, decimal_places=2)
    esc_rendimiento_academico_creado_por =models.ForeignKey(User, on_delete=models.CASCADE,default=User.objects.get(username="Admin").pk)

    class Meta:
        verbose_name = "Rendimiento académico"
        verbose_name_plural = "Rendimientos académicos"
        ordering = ['escuela', '-ano']

    def __str__(self):
        return '{escuela} - {ano}'.format(
            escuela=self.escuela,
            ano=self.ano)

    def get_absolute_url(self):
        return self.escuela.get_absolute_url()

    @property
    def porcentaje_logro(self):
        return self.excelente + self.satisfactorio


import cv2
import numpy as np
from scipy.stats import linregress
import serial
import time

# 웹캠 해상도 설정
FRAME_WIDTH = 640
FRAME_HEIGHT = 480

# 파이 시리얼 포트 설정 및 연결 대기
arduino = serial.Serial('COM8', 9600)
time.sleep(2)

#파이 시리얼 통신 코드
def py_serial(line_angle_input, width_input):

    # 음수 값이면 '-'를 붙여서 전송
    if float(line_angle_input) < 0:
        line_angle_input = "-" + line_angle_input
    if float(width_input) < 0:
        width_input = "-" + width_input
    # 숫자 두개를 하나의 문자열로 변환하여 통신(오버헤드 줄이려면 한 문자열로 하는게 좋음)
    message = f"{line_angle_input},{width_input}\n"
    arduino.write(message.encode())
    time.sleep(0.05)

    # 아두이노로부터 입력 받은 응답을 버퍼를 비우기 위해 읽어옴
    while arduino.in_waiting > 0:
        response = arduino.readline().decode().strip()
        print(response)

def py_serial(line_angle_input, width_input):

    # 음수 값이면 '-'를 붙여서 전송
    if float(line_angle_input) < 0:
        line_angle_input = "-" + line_angle_input
    if float(width_input) < 0:
        width_input = "-" + width_input

    message = f"{line_angle_input},{width_input}\n"
    arduino.write(message.encode())
    time.sleep(0.05)

    # 아두이노로부터 입력 받은 응답을 버퍼를 비우기 위해 읽어옴
    while arduino.in_waiting > 0:
        response = arduino.readline().decode().strip()
        print(response)

def undistort_frame(frame):
    # 카메라 캘리브레이션 매개 변수
    camera_matrix = np.array([[1.0, 0, FRAME_WIDTH / 2],
                              [0, 1.0, FRAME_HEIGHT / 2],
                              [0, 0, 1.0]], dtype=np.float32)
    distortion_coefficients = np.zeros((4, 1), dtype=np.float32)

    # 왜곡 보정
    undistorted_frame = cv2.undistort(frame, camera_matrix, distortion_coefficients)
    return undistorted_frame

def detect_dotted_line():
    # 웹캠 열기
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, FRAME_WIDTH)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT)

    # 웹캠 영상 읽기
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        # 영상 보정
        frame = undistort_frame(frame)

        # 영상 크기
        height, width = frame.shape[:2]

        # 원의 중심 좌표와 반지름 설정
        center_x = int(width / 2)
        center_y = int(height / 2)
        radius = int(min(center_x, center_y) * 0.6)

        # ROI (원 내부 영역) 생성
        mask = np.zeros((height, width), dtype=np.uint8)
        cv2.circle(mask, (center_x, center_y), radius, (255, 255, 255), -1)
        roi = cv2.bitwise_and(frame, frame, mask=mask)

        # 이미지를 그레이스케일로 변환
        gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)

        # 이미지 이진화
        _, binary = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY)

        # 추세선을 위한 점 리스트 초기화
        dotted_points = []
        solid_points = []
        solid_rect_lines = []  # 실선의 긴 변을 저장하는 리스트

        # 점선과 실선 검출을 위한 컨투어 찾기
        contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        # 점선과 실선 그리기
        for contour in contours:
            area = cv2.contourArea(contour)
            if 300 < area < 1000:  # 점선으로 인식할 면적 조건 설정
                # 직사각형의 꼭지점 좌표 구하기
                rect = cv2.minAreaRect(contour)
                box = cv2.boxPoints(rect)
                box = np.intp(box)

                # 점선 그리기
                cv2.drawContours(frame, [box], 0, (0, 0, 255), 2)

                # 중심점 좌표 수집
                cx = int(rect[0][0])
                cy = int(rect[0][1])
                dotted_points.append((cx, cy))
            elif area >= 1000:  # 실선으로 인식할 면적 조건 설정
                # 직사각형의 꼭지점 좌표 구하기
                rect = cv2.minAreaRect(contour)
                box = cv2.boxPoints(rect)
                box = np.intp(box)

                # 실선 그리기
                cv2.drawContours(frame, [box], 0, (255, 255, 0), 2)

                # 중심점 좌표 수집
                cx = int(rect[0][0])
                cy = int(rect[0][1])
                solid_points.append((cx, cy))

                # 가장 긴 변의 직선 그리기
                rect_lines = cv2.boxPoints(rect)
                rect_lines = np.intp(rect_lines)
                longest_line = np.argmax(np.linalg.norm(rect_lines[:-1] - rect_lines[1:], axis=1))

                # 가장 긴 변의 두 점
                p1 = rect_lines[longest_line]
                p2 = rect_lines[(longest_line + 1) % 4]

                # 직선의 방정식 계산
                dx = p2[0] - p1[0]
                dy = p2[1] - p1[1]
                if dx != 0:
                    slope = dy / dx
                else:
                    slope = np.inf

                # 직선을 화면 밖까지 그리기
                x1 = p1[0] - int((p1[1] - 0) / slope) if slope != 0 else p1[0]
                y1 = 0
                x2 = p2[0] + int((height - p2[1]) / slope) if slope != 0 else p2[0]
                y2 = height - 1

                cv2.line(frame, (x1, y1), (x2, y2), (255, 0, 255), 2)
                solid_rect_lines.append(rect_lines[longest_line])

        # 점선 추세선 그리기
        if len(dotted_points) > 1:
            xs, ys = zip(*dotted_points)
            unique_xs = np.unique(xs)
            if len(unique_xs) == 1:
                continue
            slope, intercept, _, _, _ = linregress(xs, ys)
            if np.isnan(slope) or np.isnan(intercept):
                continue

            # 점선 추세선 그리기
            x1 = 0
            x2 = width
            y1 = int(slope * x1 + intercept)
            y2 = int(slope * x2 + intercept)

            cv2.line(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)

            # 가장 긴 변의 직선과 점선 추세선의 교점 계산
            dx = x2 - x1
            dy = y2 - y1
            if dx != 0:
                line_slope = dy / dx
                line_intercept = y1 - line_slope * x1
                if line_slope != slope:
                    intersection_x = (intercept - line_intercept) / (line_slope - slope)
                    intersection_y = slope * intersection_x + intercept

                    # 화면에 교점 표시
                    if np.isfinite(intersection_x) and np.isfinite(intersection_y) and 0 <= intersection_x < width:
                        cv2.circle(frame, (int(intersection_x), int(intersection_y)), 5, (0, 255, 255), -1)

                        # 교점의 x 좌표에 따라 동작 결정 (아예 여유를 안줬는데 straight로 갔으면 하는 정도의 여유줘야함)
                        if intersection_x > center_x:
                            cv2.putText(frame, 'Turn right', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
                        elif intersection_x < center_x:
                            cv2.putText(frame, 'Turn left', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
                        else:
                            cv2.putText(frame, 'Go straight', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
                else:
                    print("No intersection")
            else:
                print("No intersection")

        # 원의 영역 표시
        cv2.circle(frame, (center_x, center_y), radius, (255, 0, 0), 2)

        # Calculate the angle between the dotted line and the horizontal axis
        angle = None
        if len(dotted_points) > 1:
            xs, ys = zip(*dotted_points)
            unique_xs = np.unique(xs)
            if len(unique_xs) == 1:
                continue
            slope, intercept, _, _, _ = linregress(xs, ys)
            if np.isnan(slope) or np.isnan(intercept):
                continue

            x1 = 0
            x2 = width
            y1 = int(slope * x1 + intercept)
            y2 = int(slope * x2 + intercept)

            vec1 = np.array([1, 0])  # Vector along the horizontal axis
            vec2 = np.array([x2 - x1, y2 - y1])  # Vector along the dotted line
            vec2_normalized = vec2 / np.linalg.norm(vec2)  # Normalize the dotted line vector
            angle = np.degrees(np.arccos(np.dot(vec1, vec2_normalized)))

        # Calculate the midpoint of the right solid line
        midpoint_x = None
        distance_to_right_line = None
        if len(solid_points) > 0:
            p1 = rect_lines[longest_line]
            p2 = rect_lines[(longest_line + 1) % 4]
            midpoint_x = int((p1[0] + p2[0]) / 2)

            # 차의 위치 좌표 (예시로 가운데 좌표 사용)
            cx = int(width / 2)
            cy = int(height / 2)

            # 오른쪽 실선과 차의 거리 계산
            dx = p2[0] - p1[0]
            dy = p2[1] - p1[1]
            if dx != 0:
                line_slope = dy / dx
                line_intercept = p1[1] - line_slope * p1[0]
                distance_to_right_line = abs(line_slope * cx - cy + line_intercept) / np.sqrt(line_slope ** 2 + 1)

        # Display the angle, midpoint x-coordinate, and distance to the right line on the frame
        if angle is not None:
            cv2.putText(frame, f"Angle: {angle:.2f} degrees", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        if midpoint_x is not None:
            cv2.putText(frame, f"Midpoint X: {midpoint_x}", (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        if distance_to_right_line is not None:
            cv2.putText(frame, f"Distance to Right Line: {distance_to_right_line:.2f} units", (10, 120),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

        # 파이시리얼 통신 (현장에서 angle, distance_to_right_line 변수 확인 및 튜닝 필요)
        py_serial(angle, distance_to_right_line)

        # 결과 프레임 출력
        cv2.imshow('Detected Lines', frame)

        # 'q' 키를 누르면 종료
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        # 파이 시리얼 통신

    # 웹캠 종료
    cap.release()
    cv2.destroyAllWindows()

# 웹캠으로 특정 색상 점선 및 흰색 실선 검출 및 추세선, 가장 긴 변 표시 함수 호출
detect_dotted_line()


from models import Person,Article,Category
from flask import render_template,request,session,url_for,redirect
from blog import app,db
from forms import SigninForm,SignupForm,ArticleCreateForm,ArticleUpdateForm,CategoryCreateForm

@app.route('/')
def index():
    articles = Article.all()
    return render_template('index.html',articles=articles)

@app.route('/signup',methods=['POST','GET'])
def signup():
    form = SignupForm()
    if request.method == 'POST':
        if not form.validate():
            return render_template('signup.html',form=form)
        else:
            newperson = Person(form.firstname.data,form.lastname.data,form.email.data,form.password.data)
            db.session.add(newperson)
            db.session.commit()
            session['email']=newperson.email
            return redirect(url_for('profile'))
    elif request.method=='GET':
        return render_template('signup.html',form=form)

@app.route('/profile',methods=['POST','GET'])
def profile():
    if 'email' not in session:
        return redirect(url_for('signin'))
    person = Person.query.filter_by(email=session['email']).first()
    if person:
        article = Article()
        form = ArticleCreateForm()
        if request.method=='POST' and form.validate_on_submit():
            form.populate_obj(article)
            db.session.add(article)
            db.session.commit()
            return redirect(url_for('index'))
        else:
            return render_template('create.html',form=form)
    return render_template('profile.html')

@app.route('/signin',methods=['POST','GET'])
def signin():
    if 'email' in session:
        return redirect(url_for('index'))
    form = SigninForm()
    if request.method=='POST':
        if form.validate():
            session['email']=form.email.data
            return redirect(url_for('profile'))
        else:
            return render_template('signin.html',form=form)
    elif request.method=='GET':
        return render_template('signin.html',form=form)

@app.route('/signout')
def signout():
    if 'email' not in session:
        return redirect(url_for('signin'))
    session.pop('email',None)
    return redirect(url_for('index'))

@app.route('/article/<int:id>/<slug>')
def show_article(id,slug):
    article = Article.find_by_id(id)
    return render_template('show_article.html',article=article)

@app.route('/article/<int:id>/<slug>/edit',methods=['POST','GET'])
def update_article(id,slug):
    article = Article.find_by_id(id)
    if article is None:
        return HTTPNotFound(404)
    form = ArticleUpdateForm(request.form,article)
    if form.validate_on_submit():
        form.populate_obj(article)
        db.session.add(article)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('create.html',form=form)

@app.errorhandler(404)
def HTTPNotFound(e):
    return render_template('error.html'),404

@app.route('/category/create',methods=['GET','POST'])
def category_create():
    form = CategoryCreateForm()
    category = Category()
    if form.validate_on_submit():
        form.populate_obj(category)
        db.session.add(category)
        db.session.commit()
        return redirect(url_for('profile'))
    return render_template('category.html',form=form)


from django import forms
from . import models

class purchasehead_forms(forms.ModelForm):
    purchaseinvoice = forms.CharField(
        required=True,
        label='purchaseinvoice',
        max_length=5,
        widget=forms.TextInput(attrs={'pattern': '[0-9 ]+', 'title': 'enter digits only'})
    )

    class Meta:
        model = models.Purchasehead
        fields = ['purchaseinvoice', 'purchasedate', 'agency_id']


import json
import numpy as np
from django.shortcuts import render
from django.views import View
from RepBenchWeb.views.config import *
from RepBenchWeb.views import data_loader
import RepBenchWeb.models.datasetsConfig as datasetsConfig
from RepBenchWeb.models import DataSet, InjectedContainer

class DatasetView(View):
    default_nbr_of_ts_to_display = 5
    template = DISPLAY_DATASET_TEMPLATE
    # encoder = NpEncoder
    data_fetch_url_name = "get_data"
    load_data_container = data_loader.load_data_container

    def data_set_info_context(self, setname):
        dataSet = DataSet.objects.get(title=setname)

        # generatate correlation data for highcharts
        df = dataSet.df
        # corr = df.corr().round(3)
        # corr_data = []
        # for i, row in enumerate(corr.values):
        #     for j, v in enumerate(row):
        #         corr_data.append([i, j, v])
        # columns = df.columns.tolist()

        context = {}
        context["data_info"] = dataSet.get_info()
        # context["columns"] = columns
        # context["corr_data"] = corr_data
        # context.update(dataSet.get_catch_22_features())
        return context

    def data_set_default_context(self, request, setname=datasetsConfig.default_set):
        data_container = data_loader.load_data_container(setname)
        df = data_container.original_data
        context = {"setname": setname}
        context["RepBenchWeb"] = int(request.GET.get("RepBenchWeb", self.default_nbr_of_ts_to_display))
        context["data_fetch_url_name"] = self.data_fetch_url_name
        return context, df

    def get(self, request, setname=datasetsConfig.default_set):
        context, df = self.data_set_default_context(request, setname)
        context.update(self.data_set_info_context(setname))
        return render(request, self.template, context=context)

# def sliders_view(request, setname="BAFU"):
#     from RepBenchWeb.catch22.features import features
#     dataSet = DataSet.objects.get(title=setname)
#     context = {} #dataSet.get_catch_22_features()
#     ## add abr and description to the features
#     for ts  , feature_dict  in context["catch22"].items():
#         for feature_name , feature_valued_dict in feature_dict.items():
#             feature_valued_dict['abr'] = features[feature_name]["abr"]
#             feature_valued_dict['description'] = features[feature_name]["description"]
#     return RepBenchJsonRespone(context)

def display_datasets(request=None,option="Display" , synthetic=False):
    context = {"datasets": {dataSet.title: dataSet.get_info()
                            for dataSet in DataSet.objects.all()},
               "syntheticDatasets": {dataSet.title: dataSet.get_info()
                                     for dataSet in InjectedContainer.objects.all() if
                                     dataSet.title is not None and dataSet.title != ""},

                "option" : option}

    if synthetic:
        return render(request, DISPLAY_DATASETS_SYNTHETIC_TEMPLATE, context=context)

    return render(request, DISPLAY_DATASETS_TEMPLATE, context=context)

def delete_dataset(request,setname):
    print("delete_dataset")
    print(setname)
    try:
        dataSet = DataSet.objects.get(title=setname)
        dataSet.delete()
        response = f"{setname} deleted"
        response = f"could not delete {setname}"
        raise Exception
    except:
        try:
            dataSet = InjectedContainer.objects.get(title=setname)
            print(dataSet,"EEEEEEEE")
            dataSet.delete()
            response = f"{setname} (synthetic) deleted"
        except Exception as e:
            print(f"could not delete {setname} in {DataSet.objects.all()} or {InjectedContainer.objects.all()}" )
            raise e
            response = f"could not delete {setname}"

    return display_datasets(request)


import torch.nn as nn
from transformers import BertModel
import copy
import numpy as np

def deleteLayers(model, layers_to_remove):
    oldModuleList = model.encoder.layer
    newModuleList = nn.ModuleList()

    # loop through 12 layers
    for i in range(0, 12):
        # if layer i+1 is in remove list, then remove it
        if i+1 in layers_to_remove:
            continue
        else:
            newModuleList.append(oldModuleList[i])

    copyModel = copy.deepcopy(model)
    copyModel.encoder.layer = newModuleList
    return copyModel

class BertClassifier(nn.Module):
    def __init__(self, freeze_bert=False, D_out=3):
        super(BertClassifier, self).__init__()

        D_in, H = 768, 50
        # Bert layer
        self.bert = deleteLayers(BertModel.from_pretrained('bert-base-uncased'), [12])
        # Linear layer with ReLU
        self.classifier = nn.Sequential(
            nn.Linear(D_in, H),
            nn.ReLU(),
            nn.Linear(H, D_out)
        )

        if freeze_bert:
            for param in self.bert.parameters():
                param.requires_grad = False

    def forward(self, input_ids, attention_mask):
        outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
        first_hidden_state_cls = outputs.last_hidden_state[:, 0, :]
        logits = self.classifier(first_hidden_state_cls)
        return logits


import sys
import itertools as it
sys.stdin=open("input.txt","r")

# 이 문제는 결국 부분집합을 만들어서 검증하는 것이다.
# 주어진 n개의 숫자 중에 m개의 숫자를 뽑아서 사용하는 것이다.
# 그 m개의 원소들의 합을 받아서 6의 배수인지 판별하면 된다.

# a라는 리스트를 크기 n으로 만들어서 다 저장해두고 그 값을 인덱싱 하여
# 넣어보자
# sum + a[i] 이렇게 누적해서 넘긴다.
# i는 S에 for문을 돌리면서 하면 된다.

# 방법 2

n,k = map(int, input().split())
a = list(map(int, input().split()))
m = int(input())
cnt = 0

# 조합은 combinations()
for x in it.combinations(a,k):
    if sum(x) % m == 0:
        cnt += 1

print(cnt)

# 방법 1

# def DFS(L,S,sum):
#     global cnt
#     if L==k:
#         if sum%m == 0:
#             cnt +=1
#     else: # 자료가 리스트에 들어가 있는데 이 인덱스를 받아야한다.
#         # 처음에는 a[0]부터 a[n-1]까지 돌아야하고
#         # 이후 시작점을 S를 받아서
#         # 돌아야한다.
#         for i in  range(S,n):
#             # i+1를 넘겨야한다. 인덱스를 넘긴다.
#             # 지금 넣은
#             DFS(L+1,i+1,sum+a[i])

# if __name__ == "__main__":
#     n,k=map(int, input().split())
#     a = list(map(int, input().split()))
#     m=int(input())
#     cnt=0
#     DFS(0,0,0)
#     print(cnt)


from datetime import datetime, timedelta
from datetime import date
import csv
import calendar
import os
import time

__all__ = ['find_holidays','is_holiday','last_day_of_month','getNextWeeklyExpiry',
            'getMonthlyExpiry','get_max_date']
dir_name = os.path.dirname(__file__)
holiday_file = os.path.join(dir_name,'./holidays.csv')

holidaydates: date = []

def get_max_date()->date:
    "Latest date of bhav copy"
    latest_date = date.today()
    while is_holiday(latest_date):
        latest_date = latest_date - timedelta(days=1)

    if latest_date < date.today():
        return latest_date

    localtime = time.localtime(time.time())
    hour = localtime.tm_hour

    if hour < 19 :
        return latest_date - timedelta(days=1)
    else:
        return latest_date

def find_holidays()->list:
    if holidaydates:
        return holidaydates
    else:
        with open(holiday_file) as f:
            csv_reader = csv.reader(f, delimiter=',')
            for entry in csv_reader:
                holidaydates.append(datetime.strptime(entry[2], "%d-%b-%y").date())

            return holidaydates

def is_holiday(dt: date) -> bool:
    "finds if NSE is closed for the given date "
    holiday_list = find_holidays()
    if dt in holiday_list or dt.weekday() == 5 or dt.weekday() == 6:
        return True
    else:
        return False

def last_day_of_month(d: datetime.date) -> date:
    """Calculate current month last date"""
    return d.replace(day=calendar.monthrange(d.year, d.month)[1])

def getNextWeeklyExpiry(fromdate: date) -> date:
    """get nearest weekly expiry from the given date"""
    # assume given date is expiry
    expDate = fromdate
    # time delta
    tomorrow = timedelta(days=+1)
    yesterday = timedelta(days=-1)
    # increment from given date until next thursday(3) is found.
    for i in range(7):
        if expDate.weekday() == 3:
            if is_holiday(expDate):  # if thurnsday is holiday
                if expDate != fromdate:  # if given date not holiday thursday
                    expDate = expDate + yesterday  # expiry is one day before
                    print(f"next weekly expiry date:{expDate.strftime('%d-%m-%Y')}")
                    return (expDate)
                else:  # if given date is  holiday thursday
                    fromdate = fromdate + tomorrow
                    return getNextWeeklyExpiry(fromdate)  # get expiry of next week
            else:
                print(f"next weekly expiry date:{expDate.strftime('%d-%m-%Y')}")
                return (expDate)
        # go to next day
        expDate = expDate + tomorrow

def getMonthlyExpiry(fromDate: date):
    # time delta
    tomorrow = timedelta(days=+1)
    yesterday = timedelta(days=-1)
    # assume given last day of month is expiry
    lastDay = last_day_of_month(fromDate)
    expDate = lastDay
    # find closest thursday(3) to end of the month
    for i in range(7):
        if expDate.weekday() == 3:  # on thursday
            if is_holiday(expDate):  # if last thurday is holiday
                expDate = expDate + yesterday  # wedensday is the expiry
                break  # exit from loop as expiry is found

            else:  # not holiday
                break  # exit from loop as expiry is found
        # go to next day
        else:
            expDate = expDate + yesterday

    if expDate >= fromDate:  # if month expiry is after given date
        print(f"Monthly expiry date:{expDate.strftime('%d-%m-%Y')}")
        return (expDate)

    else:  # given date is past the month expiry
        return getMonthlyExpiry(lastDay + tomorrow)  # get next month expiry

if __name__ == '__main__':
    curDate = date.today()
    print(f"current date:{curDate.strftime('%d-%m-%Y')}")
    getNextWeeklyExpiry(curDate)
    print(last_day_of_month(curDate))


import folium
from folium import IFrame
import base64

map = folium.Map(location=[10.796019890314097,
                 76.82275843702458], zoom_start=16)

html = ''.format

picture_ab = base64.b64encode(open('./acad.jpg', 'rb').read()).decode()
iframe_ab = IFrame(html(picture_ab), width=327+20, height=245+20)
popup_ab = folium.Popup(iframe_ab, max_width=650)
ricon_ab = folium.features.CustomIcon('lib.png', icon_size=(40, 60))
folium.Marker(location=[10.8017955754564, 76.81849606364607],
              tooltip="Academic Block", popup=popup_ab, icon=ricon_ab).add_to(map)

map.save("acad.html")


# -*- coding: utf-8 -*-
# arg1 : file of filenames : csv files with positions of breaks (absolute path)
# arg2 : shustring_file.out or csv files with repeat data
# arg3 : type of repeat file : shustring or csv

################################ IMPORT ################################################################################################

#from sequana.lazy import pylab
from sequana.lazy import pandas as pd
#from sequana.lazy import numpy as np
#from random import shuffle
import sys
import os
from scipy.stats import chisquare

################################ PARAMETERS ##############################################################################################

fof_breaks_csv = str(sys.argv[1])
file_shustring = str(sys.argv[2])
type_repeat_file = str(sys.argv[3])

if type_repeat_file not in ["shustring","csv"]:
	print("arg 3 error : type of file not available : possible values : shustring csv")

threshold = 1000

################################ FUNCTIONS ##############################################################################################

def is_non_zero_file(fpath):
    return os.path.isfile(fpath) and os.path.getsize(fpath) > 0

def step_repeat_len(df_shustring, threshold):
	"""
	Convert repetitivity curve into step curve with repeat len
	returns begining and end as list of tuples
	"""

	nb_row = df_shustring.shape[0]
	i = 0
	step_repeat_seq = []
	be_repeats = []
	e = 0

	# use list because much faster
	list_len_shus = list(df_shustring.iloc[:,1])

	while(i < nb_row):
		# begining of repeat
		if (list_len_shus[i] > threshold):
			b = i
			#print(b)
			# add 0 for non repeat area
			step_repeat_seq = step_repeat_seq + [0 for j in range(e,b,1)]
			# compute new end of repeat
			len_repeat = list_len_shus[i]
			e = b + len_repeat
			# add len of repeat for all repeat area
			step_repeat_seq = step_repeat_seq + [len_repeat for j in range(b,e,1)]
			# save (b,e)
			be_repeats.append((b,e))
			# update i
			i = e-1
		i +=1

	step_repeat_seq = step_repeat_seq + [0 for j in range(e,i,1)]

	# merge repeats that are fragmented
	prev_tup = be_repeats[0]
	b = prev_tup[0]
	be_repeats_concat = []
	for tup in be_repeats[1:len(be_repeats)]:
		if tup[0] == prev_tup[1]:
			# concat
			e = tup[1]
			prev_tup = tup
		else:
			# real end of repeat : append result and update b, e
			e = prev_tup[1]
			be_repeats_concat.append((b,e))
			prev_tup = tup
			b = prev_tup[0]

	return step_repeat_seq, be_repeats_concat

def rotate_breaks(df):
	# rotate the break if needed
	# rotate whole break
	ind_rotate = df[((df["begin"] % len_genome) != df["begin"]) & ((df["end"] % len_genome) != df["end"])].index.tolist()
	for i in ind_rotate:
		df.loc[i,("begin")] = df.loc[i,("begin")] % len_genome
		df.loc[i,("end")] = df.loc[i,("end")] % len_genome

	# if break bypass the start : cut in two parts
	ind_rotate = df[((df["begin"] % len_genome) == df["begin"]) & ((df["end"] % len_genome) != df["end"])].index.tolist()
	for i in ind_rotate:
		df.loc[i,("begin")] = df.loc[i,("begin")] % len_genome
		df.loc[i,("end")] = len_genome
		df.append(df.iloc[i,],ingnore_index=True)
		df.loc[df.shape[0],("begin")] = 0
		df.loc[df.shape[0],("end")] = df[i,("end")] % len_genome

	# sort by position and update index
	df.sort_values(by="begin",inplace=True)
	df.index = range(df.shape[0])

def find_overlap(start_break, end_break, start_rep, end_rep):
	start_overlap, end_overlap = max(start_break, start_rep), min(end_break, end_rep)
	if start_overlap > end_overlap:
		start_overlap, end_overlap = None, None
	return start_overlap, end_overlap

def find_nb_overlap_repeats(start_break, end_break, df_repeats):
	"""
	df_repeats must be sorted by begin position !!
	"""
	rep_i = 0
	nb_repeat_overlap = 0
	while rep_i < df_repeats.shape[0]:
		start_rep = df_repeats.loc[rep_i,"begin"]
		end_rep = df_repeats.loc[rep_i,"end"]

		# check if repeat can overlap the break
		if not (start_break > end_rep) :
			# check if start inside repeat
			# print("repeat : %d , %d" %(start_rep, end_rep))
			start_break_inside = (start_break >=  start_rep) & (start_break <= end_rep)
			end_break_inside = (end_break >=  start_rep) & (end_break <= end_rep)
			start_rep_inside = (start_rep >=  start_break) & (start_rep <= end_break)
			end_rep_inside = (end_rep >=  start_break) & (end_rep <= end_break)
			if start_break_inside | end_break_inside :
				# find overlap
				start_overlap, end_overlap = find_overlap(start_break, end_break, start_rep, end_rep)
				# print("overlap : %d , %d" %(start_overlap, end_overlap))
				nb_repeat_overlap += (end_overlap - start_overlap)
			elif start_rep_inside | end_rep_inside :
				# find overlap
				start_overlap, end_overlap = find_overlap(start_break, end_break, start_rep, end_rep)
				# print("overlap : %d , %d" %(start_overlap, end_overlap))
				nb_repeat_overlap += (end_overlap - start_overlap)
		# all repeats after this cannot overlap
		if end_break < start_rep:
			break
		rep_i +=1

	return nb_repeat_overlap

################################ INPUT DATA ##############################################################################################

print("Breakpoints files")

# list of all files
f = open(fof_breaks_csv, 'r')
list_files_breaks = []
list_df_breaks_files = []
for path in f.readlines():
	f_path = path.split('\n')[0]
	list_files_breaks.append(f_path)

	# save short name (for plots)
	short_name = f_path.split("/")[-1]
	list_df_breaks_files.append(short_name.replace(".csv",""))
f.close()

# load all breaks files and set header
list_df_breaks = []
list_df_breaks_names = []
for i in range(len(list_files_breaks)):
	f_path = list_files_breaks[i]
	if is_non_zero_file(f_path):
		# import df
		df_breaks = pd.read_csv(f_path,header=None,sep=",")
		df_breaks.columns = ["begin","end"]
		# add filename in df
		df_breaks["filename"] = [list_df_breaks_files[i]]*df_breaks.shape[0]
		# save in list
		list_df_breaks.append(df_breaks)
		list_df_breaks_names.append(list_df_breaks_files[i])
	else:
		print("%s : empty file, not imported" % list_df_breaks_files[i])

if type_repeat_file == "shustring":
	print("Shustring file")
	# load shustring file with pandas, and re-read to set header and find genome length
	df_shustring = pd.read_csv(file_shustring,comment='#',header=None,sep="\s+")

	f = open(file_shustring, 'r')
	l = f.readline()
	# first line contains genome length
	len_genome = int(l.split("\t")[1])
	# find header
	while(l):
		if (l[0] == '#') & (l[1] != '>'):
			header_df = l.replace('\n','').replace('#','').split('\t')
			break
		l = f.readline()
	f.close()
	df_shustring.columns = header_df

	print("Find repeats location and save as csv file")
	step_repeat_seq, be_repeats_concat = step_repeat_len(df_shustring, threshold)
	df_repeats = pd.DataFrame(be_repeats_concat)
	df_repeats.columns = ["begin","end"]
	path_outfile = file_shustring + "_df_begin_end.csv"
	# if csv file doesn't exist, save it
	if not os.path.isfile(path_outfile):
		f = open(path_outfile, 'a')
		f.write("# %d\n" %len_genome)
		df_repeats.to_csv(f,mode = 'a', index=None)
		f.close()

elif type_repeat_file == "csv":
	print("Repeat csv file")
	df_repeats = pd.read_csv(file_shustring,comment='#')

	f = open(file_shustring, 'r')
	l = f.readline()
	# first line contains genome length
	len_genome = int(l.replace("\n","").split(" ")[1])
	f.close()

################################ EXECUTE ##############################################################################################

# expected frequency of repeats in genome
freq_exp_inside_repeat = 0
for rep_i in range(df_repeats.shape[0]):
	freq_exp_inside_repeat += (df_repeats.loc[rep_i,"end"] - df_repeats.loc[rep_i,"begin"])
freq_exp_outside_repeat = len_genome - freq_exp_inside_repeat
# normalisation
freq_exp_inside_repeat = freq_exp_inside_repeat / float(len_genome)
freq_exp_outside_repeat = freq_exp_outside_repeat / float(len_genome)

# observed frequency of repeats between contigs (breaks)
for df_i in range(len(list_df_breaks)):
	df = list_df_breaks[df_i]
	df["nb_outside_repeat"] = [0]*df.shape[0]
	df["nb_inside_repeat"] = [0]*df.shape[0]

	# rotate breaks if needed
	rotate_breaks(df)

	for break_i in range(df.shape[0]):
		start_break = df.loc[break_i,"begin"]
		end_break = df.loc[break_i,"end"]

		nb_repeat_overlap = find_nb_overlap_repeats(start_break, end_break, df_repeats)

		df.loc[break_i,"nb_inside_repeat"] = nb_repeat_overlap
		df.loc[break_i,"nb_outside_repeat"] = (end_break - start_break) - nb_repeat_overlap

	# results repeats
	nb_obs_inside_repeat = df["nb_inside_repeat"].sum()
	nb_obs_outside_repeat = df["nb_outside_repeat"].sum()
	total = nb_obs_inside_repeat + nb_obs_outside_repeat
	# normalisation
	# nb_obs_inside_repeat = nb_obs_inside_repeat / float(total)
	# nb_obs_outside_repeat = nb_obs_outside_repeat / float(total)

	result_chi = chisquare(f_obs=[nb_obs_inside_repeat,nb_obs_outside_repeat],f_exp=[round(freq_exp_inside_repeat*total), round(freq_exp_outside_repeat*total)])
	print("\n" + list_df_breaks_names[df_i])
	print("Expected (repeat / non repeat) : ", [round(freq_exp_inside_repeat*total), round(freq_exp_outside_repeat*total)])
	print("Observed (repeat / non repeat) : ", [nb_obs_inside_repeat,nb_obs_outside_repeat])
	print(result_chi)


from datetime import timedelta, datetime
from time import strptime
from unittest import TestCase, mock

from parameterized import parameterized
from requests.cookies import RequestsCookieJar, create_cookie

from src.chain_smoker.config import Cookie
from src.chain_smoker.test_methods import ExpectedTest, ExpectedStatusCodeTest, ContainsTest, ContainsCookiesTest

class ExpectedTestTestCase(TestCase):
    @parameterized.expand([
        (1, 1, True),
        (1, 2, False),
        (1, '1', False),
        ('foo', 'foo', True),
        ('foo', 'bar', False),
        ({'id': 1}, {'id': 1}, True),
        ({'id': 1}, {'id': 2}, False),
    ])
    def test_run_test(self, input_value, other_value, expected_result):
        self.assertEqual(ExpectedTest(input_value, '', '').test(other_value), expected_result)

class ExpectedStatusCodeTestTestCase(TestCase):
    @parameterized.expand([
        (200, mock.Mock(status_code=200), True),
        (200, mock.Mock(status_code=400), False),
    ])
    def test_run_test(self, input_value, other_value, expected_result):
        self.assertEqual(ExpectedStatusCodeTest(input_value, '', '').test(other_value), expected_result)

    @parameterized.expand([
        (200, mock.Mock(status_code=200), False),
        (200, mock.Mock(status_code=400), True),
    ])
    def test_run_test_inverse(self, input_value, other_value, expected_result):
        self.assertEqual(ExpectedStatusCodeTest(input_value, '', '', inverse=True).test(other_value), expected_result)

class ContainsTestTestCase(TestCase):
    @parameterized.expand([
        ('Foo bar', 'The story of Foo bar is extremely important to remember', True),
        ('Baz', 'The story of Foo bar is extremely important to remember', False),
        ({'text': 'Foo bar'}, {'text': 'Foo bar'}, True),
        ({'text': 'Foo bar'}, {'id': 1, 'text': 'Foo bar'}, True),
        ({'id': 1, 'text': 'Foo bar'}, {'text': 'Foo bar'}, False),
        ({'foo': 'bar'}, {'foo': 'baz'}, False),
        ({'foo': 'bar'}, {'id': 1, 'text': 'Foo bar'}, False),
        ({'nested_obj': {'id': 1, 'field': 'name'}}, {'nested_obj': {'id': 1, 'field': 'name'}}, True),
        ({'nested_obj': {'id': 1, 'field': 'name'}}, {'nested_obj': {'id': 1, 'field': 'name-2'}}, False),
        ({'nested_obj': {'id': 1, 'field': 'name'}}, {'nested_obj': {'address': 'name'}}, False),
        ({'baz': {'id': 1, 'field': 'name'}}, {'nested_obj': {'id': 1, 'field': 'name'}}, False),
        ({'id': 1}, [{'id': 1}], True),
        ({'id': 1}, [{'id': 2}], False),
        (1, [1, 2, 3], True),
        (4, [1, 2, 3], False),
        ({'payload': 1}, {'payload': [1, 2, 3]}, True),
        ({'payload': 4}, {'payload': [1, 2, 3]}, False),
        (['foo', 'bar'], {'foo': 1, 'bar': 2, 'baz': 3}, True),
        (['foo'], {'foo': 1, 'bar': 2}, True),
        (['baz'], {'foo': 1, 'bar': 2}, False),
    ])
    def test_run_test(self, input_value, other_value, expected_result):
        self.assertEqual(ContainsTest(input_value, '', '').test(other_value), expected_result)

    @parameterized.expand([
        ('Foo bar', 'The story of Foo bar is extremely important to remember', False),
        ('Baz', 'The story of Foo bar is extremely important to remember', True),
        ({'text': 'Foo bar'}, {'text': 'Foo bar'}, False),
        ({'text': 'Foo bar'}, {'id': 1, 'text': 'Foo bar'}, False),
        ({'id': 1, 'text': 'Foo bar'}, {'text': 'Foo bar'}, False),
        ({'foo': 'bar'}, {'foo': 'baz'}, True),
        ({'foo': 'bar'}, {'id': 1, 'text': 'Foo bar'}, True),
        ({'nested_obj': {'id': 1, 'field': 'name'}}, {'nested_obj': {'id': 1, 'field': 'name'}}, False),
        ({'nested_obj': {'id': 1, 'field': 'name'}}, {'nested_obj': {'address': 'name'}}, True),
        ({'id': 1}, [{'id': 1}], False),
        ({'id': 1}, [{'id': 2}], True),
        (1, [1, 2, 3], False),
        (4, [1, 2, 3], True),
        ({'payload': 1}, {'payload': [1, 2, 3]}, False),
        ({'payload': 4}, {'payload': [1, 2, 3]}, True),
        (['foo', 'bar'], {'foo': 1, 'bar': 2, 'baz': 3}, False),
        (['foo'], {'foo': 1, 'bar': 2}, False),
        (['baz'], {'foo': 1, 'bar': 2}, True),
    ])
    def test_run_test_inverse(self, input_value, other_value, expected_result):
        self.assertEqual(ContainsTest(input_value, '', '', inverse=True).test(other_value), expected_result)

class ContainsCookiesTestTesCase(TestCase):
    @parameterized.expand([
        (dict(key='foo', domain='example.com'), dict(name='foo', domain='example.com', value='2'), True),
        (dict(key='foo', domain='example.com', value='2'), dict(name='foo', domain='example.com', value='2'), True),
        (dict(key='foo', domain='example.com', value='3'), dict(name='foo', domain='example.com', value='2'), False),
        (dict(key='foo', domain='foo.com'), dict(name='foo', domain='example.com', value='2'), False),
        (dict(key='foo', domain='example.com'), dict(name='foo', domain='foo.com', value='2'), False),
        (dict(key='foo', domain='example.com'), dict(name='bar', domain='example.com', value='2'), False),
        (
            dict(key='foo', domain='example.com', value='2', max_age='5m'),
            dict(
                name='foo',
                value='2',
                domain='example.com',
                expires=(datetime.now() + timedelta(minutes=5)).timestamp()
            ),
            True
        ),
        (
            dict(key='foo', domain='example.com', value='2', max_age='5m'),
            dict(
                name='foo',
                value='2',
                domain='example.com',
                expires=(datetime.now() + timedelta(minutes=5)).timestamp()
            ),
            True
        ),
        (
            dict(key='foo', domain='example.com', value='2', max_age='5m'),
            dict(
                name='foo',
                value='2',
                domain='example.com',
                expires=(datetime.now() + timedelta(minutes=6)).timestamp()
            ),
            False
        ),
        (
            dict(key='foo', domain='example.com', value='2', max_age='session'),
            dict(
                name='foo',
                value='2',
                domain='example.com',
                expires=(datetime.now() + timedelta(minutes=5)).timestamp()
            ),
            False
        ),
        (
            dict(key='foo', domain='example.com', value='2', max_age='5m'),
            dict(name='foo', value='2', domain='example.com', expires=None),
            False
        ),
        (
            dict(key='foo', domain='example.com', value='2', max_age='Session'),
            dict(name='foo', value='2', domain='example.com', expires=None),
            True
        ),
        (
            dict(key='foo', domain='example.com', value='2', max_age='session'),
            dict(name='foo', value='2', domain='example.com', expires=None),
            True
        ),
    ])
    def test_run_test(self, test_cookie_conf, response_cookie_conf, expected_result):
        test = ContainsCookiesTest([Cookie(**test_cookie_conf)], '', '')
        jar = RequestsCookieJar()
        if response_cookie_conf is not None:
            jar.set_cookie(
                create_cookie(**response_cookie_conf)
            )
        self.assertEqual(test.test(jar), expected_result)

    def test_run_test_inverse(self):
        test = ContainsCookiesTest([Cookie(key='foo', domain='example.com', value='2')], '', '', inverse=True)
        with self.assertRaises(NotImplementedError):
            test.test({})

    @parameterized.expand([
        ('Session', 'session'),
        ('session', 'session'),
        ('5m', timedelta(minutes=5)),
        ('5W', timedelta(weeks=5)),
        ('5M', timedelta(weeks=4*5)),
        ('5D', timedelta(days=5)),
        ('5d', timedelta(days=5)),
    ])
    def test_get_max_age(self, input_value, expected_output_value):
        now = datetime.now()
        cookie = Cookie(key='foo', domain='example.com', max_age=input_value)

        if not isinstance(expected_output_value, str):
            expected_output_value = now + expected_output_value

        with mock.patch('src.chain_smoker.test_methods.datetime', create=True) as now_mock:
            now_mock.timedelta = timedelta
            now_mock.datetime.strptime = strptime
            now_mock.datetime.now.return_value = now
            self.assertEqual(ContainsCookiesTest([cookie], '', '')._get_max_age(cookie), expected_output_value)


# -*- coding: utf-8 -*-
import io
import os

import requests

class ImageDownloader:
    def __init__(self, image_folder):
        self.image_folder = image_folder

        if not os.path.exists(image_folder):
            os.makedirs(image_folder)

    def download_image(self, image_url, image_name):
        image_path = os.path.join(self.image_folder, image_name)

        if not os.path.exists(image_path):
            response = requests.get(image_url)

            if (response.status_code == 200) or (response.status_code == 206):
                with open(image_path, 'wb') as f:
                    f.write(response.content)

    def get_image(self, image_url, image_name):
        image_path = os.path.join(self.image_folder, image_name)

        if not os.path.exists(image_path):
            self.download_image(image_url, image_name)

        image_stream = None

        with open(image_path, 'rb') as f:
            image_stream = io.BytesIO(f.read())

        return image_stream


# _author: Coke
# _date: 2022/4/12 11:11

from application import socketio, utils
from flask import request
from application.ws import online_server, session_maps

import logging

@socketio.on('connect')
def connect():
    """ 挂载钩子 """

    header = request.headers

    # 获取启动的标记符并将其存储在内存中
    device_token = header.get('token')

    if not device_token:
        logging.info('socket no token')
        return False

    payload = utils.analytic_token(device_token)
    if not payload.get('results'):
        return False

    user_info = payload.get('info')
    user_id, username = user_info.get('user_id'), user_info.get('username')

    if user_id not in online_server:
        online_server.append(user_id)

    # 将 session id 更新
    session_maps[user_id] = getattr(request, 'sid', None)

    logging.info(f'{username} online')

    if isinstance(user_id, str):
        master = utils.get_master_info(user_id)
        if master:
            socketio.emit('masterOnline', {'id': master.id, 'online': True})

@socketio.on('disconnect')
def test_disconnect():
    """ 销毁钩子 """

    header = request.headers
    device_token = header.get('token')

    payload = utils.analytic_token(device_token)

    user_info = payload.get('info')
    user_id, username = user_info.get('user_id'), user_info.get('username')

    if user_id in online_server:
        online_server.remove(user_id)

    if user_id in session_maps:
        del session_maps[user_id]

    logging.info(f'{username} offline')

    if isinstance(user_id, str):
        master = utils.get_master_info(user_id)
        if master:
            socketio.emit('masterOnline', {'id': master.id, 'online': False})


import json, logging
from pytz import timezone
from enum import Enum

from django.db.models import Q
from django.shortcuts import redirect, get_object_or_404
from django.shortcuts import render
from django.views import View
from django.views.generic import DetailView
from django.views.generic.list import ListView

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import JsonResponse
from django.template.defaultfilters import slugify

from .models import *
from .business import *

# Number of star which is reviewing score of the store (dispensary)
NUM_START = 5

class MenuBar(Enum):
    HOME = "HOME"
    ABOUT_US = "ABOUT US"
    PRODUCTS = "PRODUCTS"
    SERVICES = "SERVICES"
    NEWS = "NEWS"
    CONTACT_US = "CONTACT US"

class HomeView(View):
    def get(self, request, *args, **kwargs):
        products = Products.objects.all().order_by('-id')[:3]
        news_list = News.objects.all().order_by('-id')[:3]
        product_caterory_list = ProductCategory.objects.filter(is_parent_category=True).order_by('name')
        voucher_list = Voucher.objects.all().order_by('-id')
        context = {
            "products": products,
            "news_list": news_list,
            "product_caterory_list": product_caterory_list,
            "page_name": MenuBar.HOME.value,
            "voucher_list": voucher_list
        }
        return render(request, './apps/home.html', context)

class AboutView(View):
    def get(self, request, *args, **kwargs):
        employee_obj_list = EmployeeProfile.objects.all().order_by('-id')
        employee_list = []
        i = 0
        for employee_obj in employee_obj_list:
            employee_list.append(employee_obj)
            i = i + 1
        if i < 4:
            for employee_obj in employee_obj_list:
                i = i + 1
                if i > 4:
                    break
                employee_list.append(employee_obj)
        about_us = AboutUs.objects.last()
        context = {
            "employee_list": employee_list,
            "page_name": MenuBar.ABOUT_US.value,
            'about_us': about_us
        }
        return render(request, './apps/about.html', context)

class DataView(View):
    def get(self, request, *args, **kwargs):
        news_list = News.objects.filter(slug__isnull=True)
        for news in news_list:
            value = slugify(news.title)
            print(value)
            News.objects.filter(id=news.id).update(slug=value)
        product_list = Products.objects.filter(slug__isnull=True)
        for product in product_list:
            value = slugify(product.name)
            Products.objects.filter(id=product.id).update(slug=value)
        return JsonResponse({})

class ContactView(View):
    def get(self, request, *args, **kwargs):
        context = {
            "page_name": MenuBar.CONTACT_US.value
        }
        return render(request, './apps/contact-us.html', context)

    def post(self, request, *args, **kwargs):
        return JsonResponse({})

class ProductListView(ListView):
    model = Products
    context_object_name = 'products'
    paginate_by = 25  # if pagination is desired
    template_name = './apps/products.html'

    def get_queryset(self):
        object_list = Products.objects.all().order_by('-id')
        product_category = self.request.GET.get('product_category', None)
        if product_category:
            object_list = Products.objects.filter(product_category__name=product_category).order_by('-id')
        return object_list

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        product_caterory_list = ProductCategory.objects.filter(is_parent_category=True).order_by('name')
        context["product_caterory_list"] = product_caterory_list
        context["page_name"] = MenuBar.PRODUCTS.value
        context["product_category"] = self.request.GET.get('product_category', '')
        return context

class ProductDetailView(DetailView):
    model = Products
    context_object_name = 'product'
    template_name = './apps/product_detail.html'

    def get_context_data(self, **kwargs):
        current_product = self.get_object()
        context = super().get_context_data(**kwargs)
        products = Products.objects.all().order_by('-id')[:25]
        product_reviews = ProductReviews.objects.filter(product_id=current_product.id)
        context["products"] = products
        context["product_reviews"] = product_reviews
        context['meta'] = self.get_object().as_meta(self.request)
        context["page_name"] = MenuBar.PRODUCTS.value
        return context

class NewsListView(ListView):
    model = News
    context_object_name = 'news_list'
    paginate_by = 25  # if pagination is desired
    template_name = './apps/news.html'

    def get_queryset(self):
        object_list = News.objects.all().order_by('-id')
        news_category = self.request.GET.get('news_category', None)
        # if news_category:
        #     object_list = News.objects.filter(news_category__name=news_category).order_by('-id')
        return object_list

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        news_category_list = NewsCategory.objects.all().order_by('name')
        context["news_category_list"] = news_category_list
        context["page_name"] = MenuBar.NEWS.value
        context["current_category"] = self.request.GET.get('news_category', '')
        return context

class NewsDetailView(DetailView):
    model = News
    context_object_name = 'news'
    template_name = './apps/news_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        news_list = News.objects.all().order_by('-id')[:25]
        news_category_list = NewsCategory.objects.all().order_by('name')
        context["news_list"] = news_list
        context["news_category_list"] = news_category_list
        context['meta'] = self.get_object().as_meta(self.request)
        context["page_name"] = MenuBar.NEWS.value
        return context

class ServiceView(View):
    def get(self, request, *args, **kwargs):
        context = {
            "page_name": MenuBar.SERVICES.value
        }
        return render(request, './apps/services.html', context)


from scipy import signal, interpolate
import numpy as np
import statsmodels.api as sm

class MatchEQ:
    def __init__(self, sample_rate=48000, smoothing = 0.0375, fft_size=4096, min_freq=100, max_freq=8000, mix=1):
        self.sample_rate = sample_rate
        self.fft_size = fft_size
        self.lowess_frac = smoothing
        self.lowess_it = 0
        self.lowess_delta = 0.001
        self.lin_log_oversampling = 4
        self.min_value = 1e-6
        self.min_freq = min_freq
        self.max_freq = max_freq
        self.mix = mix

    def create_fft(self, audio):
        *_, specs = signal.stft(
            audio,
            fs = self.sample_rate,
            window = 'boxcar',
            nperseg = self.fft_size,
            noverlap = 0,
            boundary = None,
            padded = False
            )
        return specs

    def get_ifft(self,fft):
        t, x = signal.istft(fft,
            fs = self.sample_rate,
            window = 'boxcar',
            nperseg = self.fft_size, #None,
            noverlap = 0,
            nfft = None,
            input_onesided = True,
            boundary = None,#True,
            time_axis = -1,
            freq_axis = -2
            )
        return x

    def average_fft(self, specs):
        return np.abs(specs).mean(axis=1)

    def smooth_lowess(self, array: np.ndarray) -> np.ndarray:
        result = sm.nonparametric.lowess(
            endog = array,
            exog = np.linspace(0, 1, len(array)),
            frac = self.lowess_frac,
            it = self.lowess_it,
            delta = self.lowess_delta
            )[:, 1]
        return result

    def smooth_exponentially(self, matching_fft: np.ndarray) -> np.ndarray:

        grid_linear = self.sample_rate * 0.5 * np.linspace(
            start = 0,
            stop = 1,
            num = (self.fft_size//2+1)
            )

        grid_logarithmic = self.sample_rate * 0.5 * np.logspace(
            start = np.log10(4/self.fft_size),
            stop = 0,
            num = (self.fft_size//2) * self.lin_log_oversampling + 1
            )

        interpolator = interpolate.interp1d(
            x = grid_linear,
            y = matching_fft,
            kind = 'cubic'
            )

        matching_fft_log = interpolator(grid_logarithmic)
        matching_fft_log_filtered = self.smooth_lowess(array=matching_fft_log)

        interpolator = interpolate.interp1d(
            x = grid_logarithmic,
            y = matching_fft_log_filtered,
            kind = 'cubic',
            fill_value = 'extrapolate'
            )

        matching_fft_filtered = interpolator(grid_linear)
        matching_fft_filtered[0] = 0
        matching_fft_filtered[1] = matching_fft[1]

        return matching_fft_filtered

    def get_fir(self, fft):
        fir = np.fft.irfft(fft)
        fir = np.fft.ifftshift(fir) * signal.windows.hann(len(fir))
        return fir

    def convolve(self, source, target):
        result = signal.fftconvolve(target, source, 'same')
        return result

    def __call__(self, source, target, min_freq=100):
        self.source = source
        self.target = target

        self.create_source_from_signal()

        if isinstance(target, str):
            self.get_target_from_savefile()
        else:
            self.create_target_from_signal()

        #self.limit_target()
        self.adjust_to_mean()

        self.get_match_fir()

        result = signal.fftconvolve(in1 = self.source, in2 = self.match_fir, mode='same')

        return result

    def create_source_from_signal(self):
        source_fft = self.create_fft(self.source)
        self.source_average_fft = self.average_fft(source_fft)

    def create_target_from_signal(self):
        self.target_fft = self.create_fft(self.target)
        self.target_average_fft = self.average_fft(self.target_fft)

    def get_target_from_savefile(self):
        self.target_average_fft = np.load(self.target)

    def adjust_to_mean(self):
        source_average = np.mean(self.source_average_fft)
        target_average = np.mean(self.target_average_fft)
        #print(np.min(self.target_average_fft), np.max(self.target_average_fft))
        print(np.mean(self.source_average_fft), np.mean(self.target_average_fft))
        self.target_average_fft = self.target_average_fft * (source_average/target_average)
        self.target_average_fft[self.target_average_fft <= 0] = self.min_value
        #idxs = np.where(self.target_average_fft <= 0, self.target_average_fft)
        #print(idxs)
        #print(source_average, target_average)
        #print(np.min(self.source_average_fft), np.max(self.source_average_fft))
        #print(np.min(self.target_average_fft), np.max(self.target_average_fft))
        print(np.mean(self.source_average_fft), np.mean(self.target_average_fft))
        #quit()

    #def limit_target(self):
        #np.maximum(self.min_value, self.target_average_fft, out=self.target_average_fft)

    def get_match_fir(self):
        self.match_fft = self.target_average_fft / self.source_average_fft
        self.match_fft = self.smooth_exponentially(self.match_fft)

        self.apply_mix()
        self.ignore_extremes()
        self.match_fir = self.get_fir(self.match_fft)

    def apply_mix(self):
        self.match_fft = np.log10(self.match_fft)
        self.match_fft = self.mix*self.match_fft
        self.match_fft = 10**self.match_fft

    def ignore_extremes(self):
        self.freqs = np.linspace(start=0, stop=self.sample_rate//2, num=self.fft_size//2)

        min_bin = next(i for i, f in enumerate(self.freqs) if f >= self.min_freq)
        min_bin_half = next(i for i, f in enumerate(self.freqs) if f >= self.min_freq/2)

        max_bin = next(i for i, f in reversed(list(enumerate(self.freqs))) if f <= self.max_freq)
        max_bin_double = next(i for i, f in reversed(list(enumerate(self.freqs))) if f <= self.max_freq*2)

        #double_hi = hi *2
        #if double_hi > self.sample_rate /2:
         #   double_hi = self.freqs[-1]

        minfade = np.linspace(
            start = 1,
            stop = self.match_fft[min_bin],
            num = min_bin - min_bin_half,
            )

        maxfade = np.linspace(
            start = self.match_fft[max_bin],
            stop = 1,
            num = max_bin_double - max_bin,
            )

        self.match_fft[min_bin_half : min_bin] = minfade
        self.match_fft[max_bin : max_bin_double] = maxfade
        self.match_fft[:min_bin_half] = 1
        self.match_fft[max_bin_double:] = 1

        #print(self.match_fft.min(), self.match_fft.max(), self.match_fft.mean())
        #quit()


import numpy as np
import cv2 as cv
img1 = cv.imread('C:/Users/Administrator/Desktop/cv_hwk/image_swiching/image/re-photos_4157/before.jpg')
img2 = cv.imread('C:/Users/Administrator/Desktop/cv_hwk/image_swiching/image/re-photos_4157/after.jpg')
gray1= cv.cvtColor(img1,cv.COLOR_BGR2GRAY)
sift1 = cv.xfeatures2d.SIFT_create()
kp1 = sift1.detect(gray1,None)
img1=cv.drawKeypoints(img1,kp1,img1)

gray2= cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
sift2 = cv.xfeatures2d.SIFT_create()
kp2 = sift2.detect(gray2,None)
img2=cv.drawKeypoints(img2,kp2,img2)
cv.namedWindow('sift_keypoints1',cv.WINDOW_NORMAL  )
cv.imshow('sift_keypoints1',img1)
cv.namedWindow('sift_keypoints2',cv.WINDOW_NORMAL  )
cv.imshow('sift_keypoints2',img2)

cv.waitKey(0)
cv.destroyAllWindows()


import sys
print('importing ...', file=sys.stderr)
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria

class Model:
    def __init__(
        self,
        name = "TheBloke/CodeLlama-13B-Instruct-GPTQ",
        revision = None,
        prompt_template='''[INST] Write code to solve the following coding problem that obeys the constraints and passes the example test cases. Please wrap your code answer using ```:
{prompt}
[/INST]

''',
        callback = None
    ):
        if revision is None:
            revision = "main"
            #revision = "gptq-8bit-128g-actorder_True"
        self.name = name
        self.revision = revision
        self.prompt_template = prompt_template
        self.model = None
        self.callback = callback

    class _StreamingOutput(StoppingCriteria):
        def __init__(self, prompt_length, tokenizer, callback):
            self.prompt_length = prompt_length
            self.text = ''
            self.tokenizer = tokenizer
            self.callback = callback
        def __call__(self, input_ids, scores, **kwparams):
            text_length = len(self.text)
            self.text = self.tokenizer.decode(input_ids[0,self.prompt_length:])
            self.callback(self.text[text_length:])
            return False

    def load(self, **kwparams):
        for key, val in kwparams.items():
            if val is not None and val != getattr(self, key, None):
                setattr(self, key, val)
                if key in ('name', 'revision'):
                    self.model = None
        if self.model is None:
            print(f'loading {self.name}@{self.revision} ...', file=sys.stderr)
            self.model = AutoModelForCausalLM.from_pretrained(self.name,
                                             torch_dtype=torch.float16,
                                             device_map="auto",
                                             revision=self.revision)
            self.tokenizer = AutoTokenizer.from_pretrained(self.name, use_fast=True)

    def forward(self, prompt, append = False, **kwparams):
        self.load(**kwparams)

        input_ids = self.tokenizer(self.prompt_template.format(prompt=prompt), return_tensors='pt').input_ids.to(self.model.device)

        if append:
            input_ids = torch.cat([self.last_output_ids, input_ids], dim=-1)

        criteria = []
        if self.callback:
            criteria.append(self._StreamingOutput(
                input_ids.shape[-1],
                self.tokenizer,
                self.callback,
            ))

        output = self.model.generate(inputs=input_ids, do_sample=False, max_new_tokens=512, stopping_criteria=criteria)
        self.last_output_ids = output
        self.last_output = self.tokenizer.decode(self.last_output_ids[0][...,input_ids.shape[-1]:])
        return self.last_output

    def forward_more(self, prompt, **kwparams):
        return self.forward(prompt, append=True, **kwparams)

    def output(self, prompt, **kwparams):
        print(prompt)
        result = forward(prompt, **kwparams)
        print(result)
        return result

if __name__ == '__main__':
    import sys
    prompt = sys.argv[1:].join(' ')
    print(Model().forward(prompt))


""" Python Package Imports """
import random, string, unicodedata

""" Django Package Imports"""
from django.template.loader import get_template
from django.template import Context, RequestContext
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, render

""" Internal Package Imports """
from Control.choice_lists import GENRE_TYPES
from Data_Base.models import (Album, Artist, Song)
from Data_Base.forms import (AlbumForm, ArtistForm, SongForm)

"""

 Data_Base/views.py

 Author:      Matthew J Swann;
 Version:     1.0
 Last Update: 2013-04-17
 Update By:   Matthew J Swann

 Code for the website forms and inherent functionality.

 """

"""
 {
  ADMIN BLOCK
 }
 """
def homepage(request):
    return render_to_response('homepage.html')

"""
 {
  COMMIT BLOCK
 }
 """
def commit_album(request):
    if request.method == 'POST':
        form = AlbumForm(request.POST)
        if form.is_valid():
            pointer = Album.objects.create(
                artist    = form.cleaned_data['artist'],
                name      = form.cleaned_data['name'],
                genre     = form.cleaned_data['genre'],
                year      = form.cleaned_data['year'],
                price     = form.cleaned_data['price'],
                available = form.cleaned_data['available'],
                        )
            return HttpResponseRedirect('/album/%s' % (pointer.id))
    else:
        form = AlbumForm()
    return render(request, 'add_album.html', RequestContext(request, {
                                                        'form': form,
                                                        }))

def commit_artist(request):
    if request.method == 'POST':
        form = ArtistForm(request.POST)
        if form.is_valid():
            pointer = Artist.objects.create(
                first_name = form.cleaned_data['first_name'],
                last_name  = form.cleaned_data['last_name'],
                        )
            return HttpResponseRedirect('/artist/%s' % (pointer.id))
    else:
        form = ArtistForm()
    return render(request, 'add_artist.html', RequestContext(request, {
                                                        'form': form,
                                                        }))

def commit_song(request):
    if request.method == 'POST':
        form = SongForm(request.POST)
        if form.is_valid():
            pointer = Song.objects.create(
                album = form.cleaned_data['album'],
                name  = form.cleaned_data['name'],
                        )
            return HttpResponseRedirect('/song/%s' % (pointer.id))
    else:
        form = SongForm()
    return render(request, 'add_song.html', RequestContext(request, {
                                                    'form': form,
                                                    }))

"""
 {
  SEARCH BLOCK
 }
 """
def ajax_album_home(request):
    return render_to_response('search_album.html')

def ajax_album_search(request, searchString):
    found = True
    results = Album.objects.filter(name__startswith=searchString)
    if not results:
        results = Album.objects.all()
        found = False
    return render_to_response('render_album.html', {
                                "results": results,
                                "found": found,
                                })

def ajax_artist_home(request):
    return render_to_response('search_artist.html')

def ajax_artist_search(request, searchString):
    found = True
    results = Artist.objects.filter(last_name__startswith=searchString)
    if not results:
        results = Artist.objects.all()
        found = False
    return render_to_response('render_artist.html', {
                                "results": results,
                                "found": found,
                                })

def ajax_song_home(request):
    return render_to_response('search_song.html')

def ajax_song_search(request, searchString):
    found = True
    results = Song.objects.filter(name__startswith=searchString)
    if not results:
        results = Song.objects.all()
        found = False
    return render_to_response('render_song.html', {
                                "results": results,
                                "found": found,
                                })

"""
 {
  DISPLAY BLOCK
 }
 """
def specific_artist(request, offset):
    offset = int(offset)
    artist = Artist.objects.get(pk=offset)
    albums = Album.objects.filter(artist=artist)
    return render_to_response('specific_artist.html', locals())

def specific_album(request, offset):
    offset = int(offset)
    album  = Album.objects.get(pk=offset)
    songs  = Song.objects.filter(album=album)
    return render_to_response('specific_album.html', locals())

def specific_song(request, offset):
    offset = int(offset)
    song   = Song.objects.get(pk=offset)
    return render_to_response('specific_song.html', locals())


import asyncio
import datetime
import re
from datetime import timedelta

import asynctest
from django.utils import timezone

from feed_fetcher.tests.utils import get_cursor, get_session
from feed_fetcher.workers import fill_queue, pull_data

class FillQueueTestCase(asynctest.TestCase):
    async def setUp(self):
        self.test_feeds = [
            (1, 'https://www.dachi.me/rss/news', False, timezone.now(), 60, 1)
        ]
        self.cur = get_cursor(self.test_feeds)
        self.data = asyncio.Queue()

    async def test_tasks_are_added_to_the_queue(self):
        await fill_queue(self.data, self.cur)
        self.assertEqual(self.data.qsize(), 1)
        data = await self.data.get()
        self.cur.execute.assert_awaited()
        self.cur.fetchall.assert_awaited()
        self.assertEqual(data[1], 'https://www.dachi.me/rss/news')

    async def test_tasks_are_being_scheduled_correctly(self):
        await fill_queue(self.data, self.cur)
        self.assertEqual(self.data.qsize(), 1)
        data = await self.data.get()
        self.cur.execute.assert_awaited()
        self.cur.fetchall.assert_awaited()
        scheduled_date = data[3] + timedelta(seconds=data[4])
        scheduled_date_str = datetime.datetime.strftime(
            scheduled_date, "%Y-%m-%d %H:%M:%S.%f")
        query = (f"UPDATE feed SET scan_after = '{scheduled_date_str}' "
                 f"WHERE id = 1")
        cur_execute_args_list = self.cur.execute.await_args_list
        self.assertEqual(len(cur_execute_args_list), 2)
        executed_query = self.cur.execute.await_args_list[1][0][0]
        executed_query = re.sub(r"\+\d{2}\:\d{2}'", r"'", executed_query)
        self.assertEqual(query, executed_query)

class PullData(asynctest.TestCase):
    async def setUp(self):
        self.test_feeds = [
            (1, 'https://www.dachi.me/rss/news', False, timezone.now(), 60, 1)
        ]
        self.data = asyncio.Queue()
        await self.data.put(self.test_feeds[0])

    @asynctest.patch('feed_fetcher.workers.terminate_feed')
    @asynctest.patch('feed_fetcher.workers.push_to_db')
    @asynctest.patch('feed_fetcher.workers.get_feed_as_dict')
    @asynctest.patch('feed_fetcher.workers.aiohttp.ClientSession')
    async def test_success_request_leads_to_correct_calls(
            self, mock_client_session, mock_parser, mock_db_insert,
            mock_feed_termination
    ):
        client, response = get_session("Dummy content", 200)
        mock_client_session.return_value.__aenter__.side_effect = client
        conn = asynctest.CoroutineMock()
        await pull_data(self.data, conn)
        self.assertEqual(self.data.qsize(), 0)
        response.assert_called_once()
        mock_parser.assert_awaited()
        mock_db_insert.assert_awaited()
        mock_feed_termination.assert_not_awaited()

    @asynctest.patch('feed_fetcher.workers.terminate_feed')
    @asynctest.patch('feed_fetcher.workers.push_to_db')
    @asynctest.patch('feed_fetcher.workers.get_feed_as_dict')
    @asynctest.patch('feed_fetcher.workers.aiohttp.ClientSession')
    async def test_back_off_mechanism(
            self, mock_client_session, mock_parser, mock_db_insert,
            mock_feed_termination
    ):
        client, response = get_session("Dummy content", 400)
        mock_client_session.return_value.__aenter__.side_effect = client
        conn = asynctest.CoroutineMock()
        await pull_data(self.data, conn)
        self.assertEqual(self.data.qsize(), 0)
        response.assert_called()
        self.assertEqual(response.call_count, 5)
        mock_parser.assert_not_awaited()
        mock_db_insert.assert_not_awaited()
        mock_feed_termination.assert_awaited()