"""Handlers of callbacks from user"""

from os import remove as os_remove

from aiogram import Dispatcher
from aiogram.dispatcher import FSMContext
from aiogram.types import CallbackQuery, InputFile

from tgbot.misc.states import UserInput
from tgbot.middlewares.localization import i18n
from tgbot.services.database import Database
from tgbot.services.youtube import youtube

_ = i18n.gettext  # Alias for gettext method

async def if_user_clicks_download(call: CallbackQuery, state: FSMContext) -> None:
    """Handles clicks on the Download button in search results"""
    await UserInput.Block.set()  # Block user actions while the download is in progress.

    user_lang_code: str = call.from_user.language_code
    chat_id: int = call.from_user.id

    # If there is data in RAM in state.proxy()
    try:
        async with state.proxy() as data:
            await call.bot.edit_message_text(
                text="⏬ " + _("Downloading, wait a bit...", locale=user_lang_code),
                chat_id=chat_id,
                message_id=data["bot_reply_id"],
            )
            await call.answer(cache_time=1)
            for bot_answer_id in data["bot_answers_ids"]:  # Deleting search results from chat
                await call.bot.delete_message(chat_id=chat_id, message_id=bot_answer_id)

        path_to_audio_file: str | None = await youtube.download_audio(call.data)
        if path_to_audio_file:
            await call.bot.send_audio(
                chat_id=chat_id, audio=InputFile(path_to_audio_file), reply_to_message_id=data["user_message_id"]
            )
            await call.bot.delete_message(chat_id=chat_id, message_id=data["bot_reply_id"])
            os_remove(path_to_audio_file)

            db: Database = call.bot.get("db")
            await db.increase_downloads_counter()

    # If the bot was restarted and now there is no data in RAM in state.proxy()
    except KeyError:
        await call.answer(text="❌ " + _("This link is out of date", locale=user_lang_code), cache_time=1)
        await call.bot.delete_message(chat_id=chat_id, message_id=call.message.message_id)

    await state.reset_state(with_data=True)  # Download completed, unblock user actions.

def register_callbacks(dp: Dispatcher) -> None:
    """Registers callback handlers"""
    dp.register_callback_query_handler(if_user_clicks_download, state=None)


import doctest
import unittest

import zope.component
from zope.component.eventtesting import clearEvents
from zope.component.eventtesting import getEvents
from zope.container import testing
from zope.traversing.api import traverse

from zope.copypastemove import ObjectMover
from zope.copypastemove.interfaces import IObjectMover

class File:
    pass

def test_move_events():
    """
    We need a root folder::

      >>> from zope.container.sample import SampleContainer
      >>> root = SampleContainer()

    Prepare the setup::

      >>> from zope import component
      >>> component.provideAdapter(ObjectMover, (None,), IObjectMover)

    Prepare some objects::

      >>> folder = SampleContainer()
      >>> root['foo'] = File()
      >>> root['folder'] = folder
      >>> list(folder.keys())
      []
      >>> foo = traverse(root, 'foo') # wrap in ContainedProxy

    Now move it::

      >>> clearEvents()
      >>> mover = IObjectMover(foo)
      >>> mover.moveableTo(folder)
      True
      >>> mover.moveTo(folder, 'bar')
      'bar'

    Check that the move has been done::

      >>> list(root.keys())
      ['folder']
      >>> list(folder.keys())
      ['bar']

    Check what events have been sent::

      >>> events = getEvents()
      >>> [event.__class__.__name__ for event in events]
      ['ObjectMovedEvent', 'ContainerModifiedEvent', 'ContainerModifiedEvent']

    Verify that the ObjectMovedEvent includes the correct data::

      >>> events[0].oldName, events[0].newName
      ('foo', 'bar')
      >>> events[0].oldParent is root
      True
      >>> events[0].newParent is folder
      True

    Let's look the other events:

      >>> events[1].object is folder
      True
      >>> events[2].object is root
      True

    """

class ObjectMoverTest(testing.ContainerPlacefulSetup, unittest.TestCase):

    def setUp(self):
        testing.ContainerPlacefulSetup.setUp(self)
        self.buildFolders()
        zope.component.provideAdapter(ObjectMover, (None,), )

    def test_movetosame(self):
        # Should be a noop, because "moving" to same location
        root = self.rootFolder
        container = traverse(root, 'folder1')
        container['file1'] = File()
        file = traverse(root, 'folder1/file1')
        mover = IObjectMover(file)
        mover.moveTo(container, 'file1')
        self.assertTrue('file1' in container)
        self.assertEqual(len(container), 2)

    def test_movetosamewithnewname(self):
        root = self.rootFolder
        container = traverse(root, 'folder1')
        container['file1'] = File()
        file = traverse(root, 'folder1/file1')
        mover = IObjectMover(file)
        mover.moveTo(container, 'file2')
        self.assertFalse('file1' in container)
        self.assertTrue('file2' in container)

    def test_movetoother(self):
        root = self.rootFolder
        container = traverse(root, 'folder1')
        container['file1'] = File()
        target = traverse(root, 'folder2')
        file = traverse(root, 'folder1/file1')
        mover = IObjectMover(file)
        mover.moveTo(target, 'file1')
        self.assertFalse('file1' in container)
        self.assertTrue('file1' in target)

    def test_movetootherwithnewname(self):
        root = self.rootFolder
        container = traverse(root, 'folder1')
        container['file1'] = File()
        target = traverse(root, 'folder2')
        file = traverse(root, 'folder1/file1')
        mover = IObjectMover(file)
        mover.moveTo(target, 'file2')
        self.assertFalse('file1' in container)
        self.assertTrue('file2' in target)

    def test_movetootherwithnamecollision(self):
        root = self.rootFolder
        container = traverse(root, 'folder1')
        container['file1'] = File()
        target = traverse(root, 'folder2')
        target['file1'] = File()
        file = traverse(root, 'folder1/file1')
        mover = IObjectMover(file)
        mover.moveTo(target, 'file1')
        self.assertFalse('file1' in container)
        self.assertTrue('file1' in target)
        self.assertTrue('file1-2' in target)

    def test_moveable(self):
        root = self.rootFolder
        container = traverse(root, 'folder1')
        container['file1'] = File()
        file = traverse(root, 'folder1/file1')
        mover = IObjectMover(file)
        self.assertTrue(mover.moveable())

    def test_moveableTo(self):
        #  A file should be moveable to a folder that has an
        #  object with the same id.
        root = self.rootFolder
        container = traverse(root, 'folder1')
        container['file1'] = File()
        file = traverse(root, 'folder1/file1')
        mover = IObjectMover(file)
        self.assertTrue(mover.moveableTo(container, 'file1'))

    def test_movefoldertosibling(self):
        root = self.rootFolder
        target = traverse(root, '/folder2')
        source = traverse(root, '/folder1/folder1_1')
        mover = IObjectMover(source)
        mover.moveTo(target)
        self.assertTrue('folder1_1' in target)

    def test_movefoldertosame(self):
        # Should be a noop, because "moving" to same location
        root = self.rootFolder
        target = traverse(root, '/folder1')
        source = traverse(root, '/folder1/folder1_1')
        mover = IObjectMover(source)
        mover.moveTo(target)
        self.assertTrue('folder1_1' in target)
        self.assertEqual(len(target), 1)

    def test_movefoldertosame2(self):
        # Should be a noop, because "moving" to same location
        root = self.rootFolder
        target = traverse(root, '/folder1/folder1_1')
        source = traverse(root, '/folder1/folder1_1/folder1_1_1')
        mover = IObjectMover(source)
        mover.moveTo(target)
        self.assertTrue('folder1_1_1' in target)
        self.assertEqual(len(target), 1)

    def test_movefolderfromroot(self):
        root = self.rootFolder
        target = traverse(root, '/folder2')
        source = traverse(root, '/folder1')
        mover = IObjectMover(source)
        mover.moveTo(target)
        self.assertTrue('folder1' in target)

    def test_movefolderfromroot2(self):
        root = self.rootFolder
        target = traverse(root, '/folder2/folder2_1/folder2_1_1')
        source = traverse(root, '/folder1')
        mover = IObjectMover(source)
        mover.moveTo(target)
        self.assertTrue('folder1' in target)

def test_suite():
    return unittest.TestSuite((
        unittest.defaultTestLoader.loadTestsFromTestCase(ObjectMoverTest),
        doctest.DocTestSuite(
            setUp=testing.ContainerPlacefulSetup().setUp,
            tearDown=testing.ContainerPlacefulSetup().tearDown),
    ))


from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
import controlExcel

## 코스닥 top10 종목에서 각 종목의 외국인 매매금액과 코스닥 전체의 외국인 매매금액의 차이를 구하고
## 조건에 따른 메시지를 출력하게 한다(7일간)

def kosdaqTopTen():
    try:
        ## 코스닥 Top15 종목코드

        # 혹시 top10에 순위 변경이 있으면 이 부분만 바꿔주면된다
        # 코스닥의 경우 "셀트리온헬스케어우"는 뺏음 => 그래서 결국 top9임 현재
        # 아래 종목 및 코드를 수정할 때는 종목명을 검색하여 관련 코드도 다 고친다
        # 예: sumsungElec를 고치고 싶다 -> "셀트리온헬스케어" 검색 후 메시지 전부 고치기
        HLBHealth = "091990"
        HLBMedi = "068760"
        seeggene = "096530"
        alteogen = "196170"
        HLB = "028300"
        skMaterial = "036490"
        ecoprobm = "247540"
        pealrbyssGames = "293490"
        pealrbyss = "263750"
        KMW = "032500"
        CJENM = "035760"
        StDragon = "253450"
        hugel = "145020"
        solBrain = "357780"
        medpacto = "235980"

        stockList = []  # 위 종목 코드를 담는 리스트

        stockList.append(HLBHealth)
        stockList.append(HLBMedi)
        stockList.append(seeggene)
        stockList.append(alteogen)
        stockList.append(HLB)
        stockList.append(skMaterial)
        stockList.append(ecoprobm)
        stockList.append(pealrbyssGames)
        stockList.append(pealrbyss)
        stockList.append(KMW)
        stockList.append(CJENM)
        stockList.append(StDragon)
        stockList.append(hugel)
        stockList.append(solBrain)
        stockList.append(medpacto)

        # 주식의 이름을 담는 리스트
        stockNameList = []
        stockNameList.append("셀트리온헬스케어")
        stockNameList.append("셀트리온제약")
        stockNameList.append("씨젠")
        stockNameList.append("알테오젠")
        stockNameList.append("에이치엘비")
        stockNameList.append("sk머터리얼즈")
        stockNameList.append("에코프로비엠")
        stockNameList.append("카카오게임즈")
        stockNameList.append("펄어비스")
        stockNameList.append("케이엠더블유")
        stockNameList.append("CJ ENM")
        stockNameList.append("스튜디오드래곤")
        stockNameList.append("휴젤")
        stockNameList.append("솔브레인")
        stockNameList.append("메드팩토")

        kosdaqForeignBuying_List = []  # 코스닥 외인 순매수/매도 데이터 리스트

        # Kosdaq 외인 크롤링 확인용 카운터
        # kosdaq의 크롤링은 한 번만 하면 되기 때문에 설정한다
        counter = 0

        ## 종목별 반복(위 코드 변수 갯수만큼 반복)
        for i in range(0, len(stockList)):

            options = webdriver.ChromeOptions()
            options.add_argument("headless")
            options.add_argument("disable-gpu")

            driver = webdriver.Chrome("C:/selenium/chromedriver", options=options)
            url1 = "https://finance.daum.net/quotes/A"
            url2 = stockList[i]
            url3 = "#influential_investors/home"
            url = url1 + url2 + url3    # 다음금융_외인/기관_탭

            driver.get(url)

            foreignBuyingAmountList = []    # 외인 순매수량 리스트(7일간)
            stockPriceList = [] # 해당 일 주식 종가 리스트(7일간)
            priceXamountList = []   # 종가 * 순매수/매도량 값의 리스트(7일간)

            ## 날짜별 반복
            # 해당 주식의 외인 순매수량, 해당 날의 종가 정보 가져오기(7일간 기록)
            for j in range(1, 8):

                ## 먼저 해당 주식의 순매수량 가져오기
                xpath1 = "//*[@id='boxContents']/div[4]/div[1]/div[3]/div[2]/div/table/tbody/tr["
                xpath2 = "]/td[4]"

                xpath = xpath1 + str(j) + xpath2

                foreignBuyingAmount = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath))).text

                # 계산을 위해 각 데이터에 있는 ,(콤마) 제거
                foreignBuyingAmount = foreignBuyingAmount.replace(",", "")

                # 제일 앞에 항상 +가 따라오기 때문에 이 +부호를 제거
                foreignBuyingAmount = foreignBuyingAmount.replace("+", "")

                if foreignBuyingAmount[0] == "-":
                    foreignBuyingAmount = foreignBuyingAmount[1:]

                # 계산을 위해 int형으로 변경후 리스트에 추가하기
                foreignBuyingAmountList.append(int(foreignBuyingAmount))

                ## 해당 주식의 해당 날의 종가 가져오기
                xpath2 = "]/td[6]"
                xpath = xpath1 + str(j) + xpath2

                stockPrice = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath))).text

                # 계산을 위해 각 데이터에 있는 ,(콤마) 제거
                stockPrice = stockPrice.replace(",", "")

                # 계산을 위해 int형으로 변경후 리스트에 추가하기
                stockPriceList.append(int(stockPrice))

                ## 해당 주식의 순매도(매수)량 * 종가 (단위는 억 이므로 // 100000000을 해준다)
                # 즉, 각 종목에 대한 외인들의 매매 금액이다
                priceXamountList.append((int(foreignBuyingAmount) * int(stockPrice)) // 100000000)

            # print("priceXamountList: ", end= "")    # (삭제) 확인용
            # print(priceXamountList)     # (삭제) 확인용

            ## 7일간의 코스닥 외인 순매수/매도 데이터를 가져온다
            #(수정하기) - 코스닥 일자별 매매 데이터 시작일과 각 종목의 외국인 매매 데이터 시작일은 다를 수 있음 어케함?
            dateValue_start = 1   # 위의 문제점을 해결하기 위해 넣은 변수
            dateValue_end = 8   # 위의 문제점을 해결하기 위해 넣은 변수
            # 시간을 확인하여 19시 이전에 프로그램을 시작했으면 start = 2 / end = 9
            # 19시 이후에 프로그램 시작했으면 dataValue = 1 / end = 8

            # 9~19시 사이에 실시했다면 start = 2 / end = 9
            if 8 <= datetime.datetime.now().hour < 19:
                dateValue_start = 2
                dateValue_end = 9

            # 주말은 항상 18시 이후로 생각한다
            if datetime.datetime.now().strftime("%a") == "Sat" or datetime.datetime.now().strftime("%a") == "Sun":
                dateValue_start = 1
                dateValue_end = 8

            ## 코스닥에서 외인들의 매매금액을 가져온다(7일간)
            if counter == 0:
                counter += 1
                for k in range(dateValue_start, dateValue_end):
                    options = webdriver.ChromeOptions()
                    options.add_argument("headless")
                    options.add_argument("disable-gpu")

                    driver = webdriver.Chrome("C:/selenium/chromedriver", options=options)
                    url = "https://finance.daum.net/domestic/investors/KOSDAQ"  # 다음금융_코스닥_탭

                    driver.get(url)

                    xpath1 = "//*[@id='boxDays']/div[2]/div/table/tbody/tr["
                    xpath2 = "]/td[3]"
                    xpath = xpath1 + str(k) + xpath2

                    kosdaqForeignBuying = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath))).text

                    # 받은 데이터를 int화 시킨다
                    kosdaqForeignBuying = int(kosdaqForeignBuying.replace(",", ""))

                    kosdaqForeignBuying_List.append(kosdaqForeignBuying)

            # print("kosdaqForeignBuying_List: ", end="")  # (삭제) 확인용
            # print(kosdaqForeignBuying_List)  # (삭제) 확인용

            ## 일일 코스닥 전체 외인 매매금액에 대한 top10 각 종목의 일일 매매금액의 비율을 구한다
            ## 조건에 따른 메시지를 출력한다
            # 아래의 z는 xx일 전 데이터를 의미한다
            for z in range(0, 7):
                # 각 종목의 일별 외인 매매금액(+,- 상관없음) / 코스닥의 외인 매매금액 * 100 (반올림 2자리까지)
                per_result = round(priceXamountList[z] / kosdaqForeignBuying_List[z] * 100, 2)
                if z == 0:
                    # 종목별로 줄바꿈을 실시하기 위해 넣음
                    print("")
                # 위 결과가 3% 넘을 경우 메시지 표기
                if per_result >= 2.0:
                    # 3%를 넘긴하는데 코스닥 & 개별 종목 둘 다 순매도일 때 출력
                    if priceXamountList[z] <= 0 and kosdaqForeignBuying_List[z] <= 0:
                        print(stockNameList[i] + ": " + str(z + 1) + "일 전에 외인들이 코스닥의 " + str(per_result) + "% 만큼 매도함 / ", end="")
                    # 개별 종목도 외인들 순매수 + 코스피도 외인 순매수일 때 출력
                    else:
                        print(stockNameList[i] + ": " + str(z + 1) + "일 전에 외인들이 코스닥의 " + str(per_result) + "% 만큼 '매수'함 / ", end="")
                # 위 결과가 -3%를 넘을 경우 메시지 표기
                if per_result <= -2.0:
                    # 코스닥은 외인 순매도이지만, 개별 종목의 순매수 금액이 지수 매도금액의 3%를 넘으면 "역매수"로 표기하게 한다
                    # 즉, 개별 종목은 순매수(+), 지수의 외인 매매는 순매도로 (-) 이며동시에 연산 결과가 -3% 이하인 애들
                    if priceXamountList[z] >= 0 and kosdaqForeignBuying_List[z] <= 0:
                        print(stockNameList[i] + ": " + str(z + 1) + "일 전에 외인들이 코스닥의 " + str(per_result * -1) + "% 만큼 '역매수'함 / ", end="")
    except TimeoutException:
        driver.quit()
        print("")
        print("에러: 시간 초과됨")


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

# In[1]:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Conv2DTranspose, Concatenate, Input
from tensorflow.keras.models import Model
from tensorflow.keras.applications import ResNet50
import matplotlib.pyplot as plt

# In[2]:

path = 'D:\\.Files\\AI\\data'

# In[2]:

did_train = keras.preprocessing.image_dataset_from_directory(
    'D:\.Files\AI\data\DID-MDN-datasets\DID-MDN-training',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 512),
    shuffle=True,
    interpolation='bilinear')

# In[3]:

def split_x_y(x):
    return (x[:, :, :256, :] / 255., x[:, :, 256:, :] / 255.)

# In[4]:

did_train_splitted = did_train.map(split_x_y)

# In[22]:

plt.figure(figsize=(40, 40))
for X, Y in did_train_splitted.take(10):
    for i in range(4):
        #ax = plt.subplot(3, 3, i + 1)
        plt.imshow(X[i].numpy())
        plt.show()
        plt.imshow(Y[i].numpy())
        plt.show()
        #plt.title(int(labels[i]))
        plt.axis("off")

# In[23]:

rain800 = keras.preprocessing.image_dataset_from_directory(
    'D:\.Files\AI\data\\rain800_idcgan\\train',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 512),
    shuffle=False,
    interpolation='bilinear')

# In[24]:

rain800_splited = rain800.map(split_x_y)

# In[ ]:

# In[25]:

rain1400_X = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\rainy_image_dataset\\training\\rainy_image',
    #labels='inferred',
    label_mode=None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 256),
    shuffle=False,
    interpolation='bilinear')

# from PIL import Image
# import glob
#
#
# images = glob.glob('D:\\.Files\\AI\\data\\rainy_image_dataset\\training\\ground_truth\\ground_truth\\*.jpg')
#
# for image in images:
#     with open(image, 'rb') as file:
#         img = Image.open(file)
#         for i in range(1,15):
#             img.save(image.replace('ground_truth\\ground_truth', 'ground_truth2')[:-4] + '_' + str(i) + '.jpg')

# In[26]:

rain1400_Y = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\rainy_image_dataset\\training\\ground_truth2',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 256),
    shuffle=False,
    interpolation='bilinear')

# In[27]:

rain1400 =  tf.data.Dataset.zip((rain1400_X.map(lambda x: x / 255.), rain1400_Y.map(lambda x: x / 255.)))

# In[28]:

rain1400

# In[29]:

train = did_train_splitted.concatenate(rain1400).concatenate(rain800_splited)

# In[31]:

plt.figure(figsize=(40, 40))
for images, labels in train.take(10):
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy())
        #plt.title(int(labels[i]))
        plt.axis("off")

# In[5]:

def conv_block(input, num_filters):
    x = Conv2D(num_filters, 3, padding="same")(input)
    x = BatchNormalization()(x)
    #x = keras.layers.LeakyReLU()(x)
    #x = Activation('relu')(x)
    x = keras.activations.gelu()(x)

    x = Conv2D(num_filters, 3, padding="same")(x)
    x = BatchNormalization()(x)
    #x = keras.layers.LeakyReLU()(x)
    #x = Activation('relu')(x)
    x = keras.activations.gelu()(x)
    return x

def decoder_block(input, skip_features, num_filters):
    x = Conv2DTranspose(num_filters, (2, 2), strides=2, padding="same")(input)
    x = Concatenate()([x, skip_features])
    x = conv_block(x, num_filters)
    return x

def build_resnet50_unet(input_shape):
    """ Input """
    inputs = Input(input_shape)

    """ Pre-trained ResNet50 Model """
    resnet50 = ResNet50(include_top=False, weights="imagenet", input_tensor=inputs)

    """ Encoder """
    s1 = resnet50.get_layer("input_1").output           ## (512 x 512)
    s2 = resnet50.get_layer("conv1_relu").output        ## (256 x 256)
    s3 = resnet50.get_layer("conv2_block3_out").output  ## (128 x 128)

    """ Bridge """
    b1 = resnet50.get_layer("conv3_block4_out").output  ## (64 x 64)

    """ Decoder """
    d2 = decoder_block(b1, s3, 256)                     ## (128 x 128)
    d3 = decoder_block(d2, s2, 128)                     ## (256 x 256)
    d4 = decoder_block(d3, s1, 64)                      ## (512 x 512)

    """ Output """
    outputs = Conv2D(3, 1, padding="same", activation="relu")(d4)

    model = Model(inputs, outputs, name="ResNet50_U-Net")
    return model

# In[6]:

model = build_resnet50_unet((256,256,3))

# In[32]:

model = tf.keras.models.load_model('dyplom1.h5')

# In[18]:

model.summary()

# In[18]:

@tf.function
def ssim_loss(y_true, y_pred):
    return 1 - tf.image.ssim_multiscale(y_true, y_pred, 1)

# In[38]:

model.save_weights('weights.h5')

# In[41]:

model.compile(optimizer= 'adam', #keras.optimizers.SGD(momentum = 0.01),
              loss= 'mse',
              metrics=['MSE'])

# In[44]:

model.load_weights('weights.h5')

# In[92]:

history1 = model.fit(train, epochs = 1)

# In[93]:

history1 = model.fit(train, epochs = 1)

# In[94]:

history1 = model.fit(train, epochs = 1)

# In[21]:

plt.plot(history.history['loss'])

# In[82]:

model.save('dyplom!.h5')

# In[7]:

model = tf.keras.models.load_model('dyplom!.h5')

# In[154]:

plt.figure(figsize=(40, 40))
for X, Y in rain800_splited_t.take(10):
    for i in range(4):
        ax = plt.subplot(3, 3, i + 1)
        plt.figure(figsize=(40, 40))
        plt.imshow(model.predict(X)[i])
        plt.show()
        #plt.figure(figsize=(40, 40))
        #plt.imshow(Y[i])
        #plt.show()
        plt.figure(figsize=(40, 40))
        plt.imshow(X[i])
        plt.show()
        #plt.title(int(labels[i]))
        plt.axis("off")

# In[54]:

history1 = model.fit(train, epochs = 1)

# In[58]:

rain800_test = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\rain800_idcgan\\test',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 512),
    shuffle=False,
    interpolation='bilinear')

# In[72]:

did_test = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\DID-MDN-datasets\\test',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 512),
    shuffle=True,
    interpolation='bilinear')

# In[170]:

did_test_2 = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\DID-MDN-datasets\\testing_fu',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 512),
    shuffle=True,
    interpolation='bilinear')

# In[ ]:

did_test = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\DID-MDN-datasets\\test',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 512),
    shuffle=True,
    interpolation='bilinear')

# In[191]:

rain1400_t_X = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\rainy_image_dataset\\training\\rainy_image',
    #labels='inferred',
    label_mode=None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 256),
    shuffle=False,
    interpolation='bilinear')

# In[192]:

rain1400_t_Y = keras.preprocessing.image_dataset_from_directory(
    'D:\\.Files\\AI\\data\\rainy_image_dataset\\training\\ground_truth2',
    #labels='inferred',
    label_mode= None,
    class_names=None,
    color_mode='rgb',
    batch_size=32,
    image_size=(256, 256),
    shuffle=False,
    interpolation='bilinear')

# In[75]:

did_splited_t = did_test.map(split_x_y)

# In[171]:

did_test_2_s = did_test_2.map(split_x_y)

# In[76]:

rain800_splited_t = rain800_test.map(split_x_y)

# In[193]:

rain1400_t =  tf.data.Dataset.zip((rain1400_t_X.map(lambda x: x / 255.), rain1400_t_Y.map(lambda x: x / 255.)))

# In[84]:

test = did_splited_t.concatenate(rain800_splited_t)#.concatenate(rain1400_t)

# In[89]:

train_t = did_train_splitted.concatenate(rain800_splited)#.concatenate(rain1400_t)

# In[95]:

def split_x(x):
    return x[:, :, :256, :] / 255.

def split_y(x):
    return x[:, :, 256:, :] / 255.

# In[96]:

rain800_x = rain800_test.map(split_x)
rain800_y = rain800_test.map(split_y)
did_x = did_test.map(split_x)
did_y = did_test.map(split_y)

# In[97]:

test_x = did_x.concatenate(rain800_x)
test_y = did_y.concatenate(rain800_y)

# In[90]:

model.evaluate(train_t)

# In[86]:

model.evaluate(test)

# In[166]:

a = []
for X, Y in rain800_splited_t:
    #pred= model.predict(X)
    for p in range(pred.shape[0]):
        a.append(tf.image.ssim(X[p], Y[p], 1))
print(a)

# In[ ]:

tf.reduce_mean(tf.stack(a))

# In[180]:

a = []
for X, Y in did_test_2_s:
    pred= model.predict(X)
    for p in range(pred.shape[0]):
        a.append(tf.image.ssim(pred[p], Y[p], 1))
print(a)

# In[173]:

tf.reduce_mean(tf.stack(a))

# rain1400 - 0.83481604
# did_mdn - 0.83768666
# did_test_2 - 0.58992624
# rain800 - 0.6411672

# In[8]:

for X, Y in  did_train_splitted:
    #a = model.predict(X)
    plt.figure(figsize=(25, 25))
    plt.imshow(model.predict(X)[30])
    plt.show()
    plt.figure(figsize=(25, 25))
    plt.imshow(model.predict(Y)[30])
    plt.show()
    plt.figure(figsize=(25, 25))
    plt.imshow(X[30])
    plt.figure(figsize=(25, 25))
    plt.show()
    plt.figure(figsize=(25, 25))
    plt.imshow(Y[30])
    plt.show()
    break

# In[175]:

tf.keras.utils.plot_model(model)

# In[ ]:


import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
from torch.autograd import Variable
from tqdm import tqdm, trange
from function import  adaptive_instance_normalization

import torch.optim as optim
import random
import scipy.io as scio

import numpy as np
import time

from data_loader import list_images
import data_loader

from unet import Dense_net

# ------- 1. define loss function --------
from function import perceptual_loss, content_loss
import pytorch_msssim
mse_loss = torch.nn.MSELoss()
ssim_loss = pytorch_msssim.msssim

# ------- 2. set the directory of training dataset --------

# model_name = 'u2netp' #'u2netp'

# data_dir = './train_data/'
con_dir = "/data/Disk_B/MSCOCO2014/train2014/"
sty_dir = "/data/Disk_B/MSCOCO2014/train2014/"

epoch_num = 5
batch_size_train = 4
train_num = 40000

# tra_img_name_list = glob.glob(data_dir + tra_image_dir + '*' + image_ext)
con_list = list_images(con_dir)
sty_list = list_images(sty_dir)

con_list = con_list[:train_num]
sty_list = sty_list[train_num:2*train_num]
random.shuffle(con_list)
random.shuffle(sty_list)

#args
class args():
	HEIGHT = 256
	WIDTH = 256
	log_interval = 5
	save_model_dir = "models"
	save_loss_dir = "models/loss"
	alpha = 0.5 #weight of style
	beta = (1-alpha)*0.5 #weight of content
	gama = (1-alpha)*0.5 #weight of style SSIM
	cuda = 1
	# model_path = "./models/Epoch_0_iters_250_Fri_May_22_15_45_17_2020_.model"
	# resume = "./models/Epoch_0_iters_1000.model"
	resume = None

# salobj_dataloader = DataLoader(salobj_dataset, batch_size=batch_size_train, shuffle=True, num_workers=1)

# ------- 3. define model --------
#
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv2d') != -1:
        torch.nn.init.xavier_normal_(m.weight.data)
        torch.nn.init.constant_(m.bias.data, 0.0)
    elif classname.find('Linear') != -1:
        torch.nn.init.xavier_normal_(m.weight.data)
        torch.nn.init.constant_(m.bias.data, 0.0)
net = Dense_net()
net.apply(weights_init)

if args.resume is not None:
		print('Resuming, initializing using weight from {}.'.format(args.resume))
		net.load_state_dict(torch.load(args.resume))
print(net)

if torch.cuda.is_available():
	net.cuda()

# ------- 4. define optimizer --------
# print("---define optimizer...")
optimizer = optim.Adam(net.parameters(), lr=1e-4, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
# scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=40,
# 														   verbose=True, threshold=0.0001, threshold_mode='rel',
# 														   cooldown=0, min_lr=0, eps=1e-12)

# ------- 5. training process --------
# print("---start training...")

tbar = trange(epoch_num)
print('Start training.....')

# creating save path
temp_path_model = os.path.join(args.save_model_dir)
if os.path.exists(temp_path_model) is False:
		os.mkdir(temp_path_model)

temp_path_loss = os.path.join(args.save_loss_dir)
if os.path.exists(temp_path_loss) is False:
		os.mkdir(temp_path_loss)

Loss_con = []
Loss_sty = []
Loss_ssim = []
Loss_all = []

all_con_loss = 0.
all_ssim_loss = 0
all_sty_loss = 0.

for e in tbar:
	print('Epoch %d.....' % e)
	# load training database
	con_set_ir, batches = data_loader.load_dataset(con_list, batch_size_train)
	sty_set_ir, sty_batches = data_loader.load_dataset(sty_list, batch_size_train)
	net.train()
	count = 0

	for batch in range(batches):
		image_paths = con_set_ir[batch * batch_size_train:(batch * batch_size_train + batch_size_train)]
		img = data_loader.get_train_images_auto(image_paths, height=args.HEIGHT, width=args.WIDTH)
		# style
		sty_image_paths = sty_set_ir[batch * batch_size_train:(batch * batch_size_train + batch_size_train)]
		sty_img = data_loader.get_train_images_auto(sty_image_paths, height=args.HEIGHT, width=args.WIDTH)
		count += 1
		optimizer.zero_grad()

		img = Variable(img, requires_grad=False)
		sty_img = Variable(sty_img, requires_grad=False)

		if args.cuda:
			img = img.cuda()
			sty_img = sty_img.cuda()
		#trans

		outputs = net(img, sty_img)

		# resolution loss
		x = Variable(img.data.clone(), requires_grad=False)
		sty = Variable(sty_img.data.clone(), requires_grad=False)

		con_loss_value = 0.
		ssim_loss_value = 0.
		sty_loss_value = 0.

		con_loss_temp = content_loss(outputs, x)
		ssim_loss_temp = ssim_loss(outputs, x, normalize=True)
		sty_loss_temp = perceptual_loss(outputs, sty)

		ssim_loss_value += (1 - ssim_loss_temp)
		con_loss_value += con_loss_temp
		sty_loss_value += sty_loss_temp

		ssim_loss_value /= len(outputs)
		con_loss_value /= len(outputs) * 3 * 256 * 256
		sty_loss_value /= len(outputs) * 3 * 256 * 256

		total_loss = con_loss_value*args.beta + ssim_loss_value*args.gama + sty_loss_value*args.alpha
		total_loss.backward()
		optimizer.step()
		# scheduler.step(total_loss.item())

		all_ssim_loss += ssim_loss_value.item()*args.gama
		all_con_loss += con_loss_value.item()*args.beta
		all_sty_loss += sty_loss_value.item()*args.alpha

		if (batch + 1) % args.log_interval == 0:
			mesg = "{}\tEpoch {}:\t[{}/{}]\t con loss: {:.6f}\t ssim loss: {:.6f}\t sty loss: {:.6f}\t total loss: {:.6f}\t ".format(
				time.ctime(), e + 1, count, batches,
				# optimizer.param_groups[0]['lr'],
							  all_con_loss / args.log_interval,
							  all_ssim_loss / args.log_interval,
							  all_sty_loss / args.log_interval,
							  (all_con_loss + all_ssim_loss + all_sty_loss ) / args.log_interval, )
			tbar.set_description(mesg)
			Loss_con.append(all_con_loss / args.log_interval)
			Loss_ssim.append(all_ssim_loss / args.log_interval)
			Loss_sty.append(all_sty_loss / args.log_interval)
			Loss_all.append((all_con_loss + all_ssim_loss + all_sty_loss) / args.log_interval)

			all_con_loss = 0.
			all_sty_loss = 0.
			all_ssim_loss = 0

		if (batch + 1) % (1000) == 0:
			# save model
			net.eval()
			net.cpu()
			save_model_filename = "Epoch_" + str(e) + "_iters_" + str(count) + ".model"
			save_model_path = os.path.join(args.save_model_dir, save_model_filename)
			torch.save(net.state_dict(), save_model_path)
			# save loss data
			# con loss
			loss_data_con = np.array(Loss_con)
			loss_filename_path = "loss_con_epoch_" + str(e) + "_iters_" + str(count) + "_" + ".mat"
			save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
			scio.savemat(save_loss_path, {'loss_con': loss_data_con})
			# ssim loss
			loss_data_ssim = np.array(Loss_ssim)
			loss_filename_path = "loss_ssim_epoch_" + str(e) + "_iters_" + str(count) + "_" + ".mat"
			save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
			scio.savemat(save_loss_path, {'loss_ssim': loss_data_ssim})
			# sty loss
			loss_data_sty = np.array(Loss_sty)
			loss_filename_path = "loss_sty_epoch_" + str(e) + "_iters_" + str(count) + "_" + ".mat"
			save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
			scio.savemat(save_loss_path, {'loss_sty': loss_data_sty})
			# all loss
			loss_data_total = np.array(Loss_all)
			loss_filename_path = "loss_total_epoch_" + str(e) + "_iters_" + str(count) + "_" + ".mat"
			save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
			scio.savemat(save_loss_path, {'loss_all': loss_data_total})
			#
			net.train()
			net.cuda()
			tbar.set_description("\nCheckpoint, trained model saved at", save_model_path)

# con loss
loss_data_con = np.array(Loss_con)
loss_filename_path =  "Final_loss_con_epoch_" + str(epoch_num) + "_" + ".mat"
save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
scio.savemat(save_loss_path, {'loss_con': loss_data_con})

# ssim loss
loss_data_ssim = np.array(Loss_ssim)
loss_filename_path =  "Final_loss_ssim_epoch_" + str(epoch_num) + "_" + ".mat"
save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
scio.savemat(save_loss_path, {'loss_ssim': loss_data_ssim})

# sty loss
loss_data_sty = np.array(Loss_sty)
loss_filename_path =  "Final_loss_sty_epoch_" + str(epoch_num) + "_" +  ".mat"
save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
scio.savemat(save_loss_path, {'loss_sty': loss_data_sty})

# all loss
loss_data_total = np.array(Loss_all)
loss_filename_path =  "Final_loss_all_epoch_" + str(epoch_num) + "_" + ".mat"
save_loss_path = os.path.join(args.save_loss_dir, loss_filename_path)
scio.savemat(save_loss_path, {'loss_total': loss_data_total})

# save model
net.eval()
net.cpu()
save_model_filename = "Final_epoch_" + str(epoch_num)  + ".model"
save_model_path = os.path.join(args.save_model_dir, save_model_filename)
torch.save(net.state_dict(), save_model_path)

print("\nDone, trained model saved at", save_model_path)

if __name__ == "__main__":
	main()


import dash
import plotly.graph_objs as go
import pulsecatcher as pc
import functions as fn
import os
import json
import glob
import numpy as np
import sqlite3 as sql
import dash_daq as daq
import audio_spectrum as asp
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
from server import app
from dash.exceptions import PreventUpdate
from datetime import datetime

path = None
n_clicks = None
global_counts = 0
global_cps = 0

data_directory  = os.path.join(os.path.expanduser("~"), "impulse_data")

def show_tab2():

    global global_counts
    global global_cps
    global cps_list

    # Get all filenames in data folder and its subfolders
    files = [os.path.relpath(file, data_directory).replace("\\", "/")
             for file in glob.glob(os.path.join(data_directory, "**", "*.json"), recursive=True)]
    # Add "i/" prefix to subfolder filenames for label and keep the original filename for value
    options = [{'label': "~ " + os.path.basename(file), 'value': file} if "i/" in file and file.endswith(".json")
                else {'label': os.path.basename(file), 'value': file} for file in files]
    # Filter out filenames ending with "-cps"
    options = [opt for opt in options if not opt['value'].endswith("-cps.json")]
    # Filter out filenames ending with "-3d"
    options = [opt for opt in options if not opt['value'].endswith("_3d.json")]
    # Sort options alphabetically by label
    options_sorted = sorted(options, key=lambda x: x['label'])

    for file in options_sorted:
        file['label'] = file['label'].replace('.json', '')
        file['value'] = file['value'].replace('.json', '')

    database = fn.get_path(f'{data_directory}/.data.db')
    conn            = sql.connect(database)
    c               = conn.cursor()
    query           = "SELECT * FROM settings "
    c.execute(query)

    settings        = c.fetchall()[0]

    filename        = settings[1]
    device          = settings[2]
    sample_rate     = settings[3]
    chunk_size      = settings[4]
    threshold       = settings[5]
    tolerance       = settings[6]
    bins            = settings[7]
    bin_size        = settings[8]
    max_counts      = settings[9]
    shapestring     = settings[10]
    sample_length   = settings[11]

    calib_bin_1     = settings[12]
    calib_bin_2     = settings[13]
    calib_bin_3     = settings[14]

    calib_e_1       = settings[15]
    calib_e_2       = settings[16]
    calib_e_3       = settings[17]

    coeff_1         = settings[18]
    coeff_2         = settings[19]
    coeff_3         = settings[20]
    filename2       = settings[21]
    peakfinder      = settings[23]
    sigma           = settings[25]
    max_seconds     = settings[26]
    t_interval      = settings[27]

    if max_counts == 0:
        counts_warning = 'red'
    else:
        counts_warning = 'white'

    if max_seconds == 0:
        seconds_warning = 'red'
    else:
        seconds_warning = 'white'

    html_tab2 = html.Div(id='tab2', children=[
        html.Div(id='polynomial', children=''),
        html.Div(id='bar_chart_div', # Histogram Chart
            children=[
                dcc.Graph(id='bar-chart', figure={},),
                dcc.Interval(id='interval-component', interval=1000, n_intervals=0) # Refresh rate 1s.
            ]),

        html.Div(id='t2_filler_div', children=''),
        #Start button
        html.Div(id='t2_setting_div1', children=[
            html.Button('START', id='start'),
            html.Div(id='start_text', children=''),
            html.Div(id='counts', children= ''),
            html.Div(''),
            html.Div(['Max Counts', dcc.Input(id='max_counts', type='number', step=1000,  readOnly=False, value=max_counts, style={'background-color': counts_warning} )]),
            ]),

        html.Div(id='t2_setting_div2', children=[
            html.Button('STOP', id='stop'),
            html.Div(id='stop_text', children=''),
            html.Div(id='elapsed', children= '' ),
            html.Div(['Max Seconds', dcc.Input(id='max_seconds', type='number', step=60,  readOnly=False, value=max_seconds, style={'background-color': seconds_warning} )]),
            html.Div(id='cps', children=''),
            ]),

        html.Div(id='t2_setting_div3', children=[
            html.Div(['File name:', dcc.Input(id='filename' ,type='text' ,value=filename )]),
            html.Div(['Number of bins:', dcc.Input(id='bins'        ,type='number'  ,value=bins )]),
            html.Div(['bin size      :', dcc.Input(id='bin_size'    ,type='number'  ,value=bin_size )]),
            ]),

        html.Div(id='t2_setting_div4', children=[
            html.Div(['LLD Threshold:', dcc.Input(id='threshold', type='number', value=threshold )]),
            html.Div(['Shape Tolerance:', dcc.Input(id='tolerance', type='number', value=tolerance )]),
            html.Div(['Update Interval(s)', dcc.Input(id='t_interval', type='number', step=1,  readOnly=False, value=t_interval )]),
            ]),

        html.Div(id='t2_setting_div5', children=[
            html.Div('Select Comparison'),
            html.Div(dcc.Dropdown(
                    id='filename2',
                    options=options_sorted,
                    placeholder='Select acomparison',
                    value=filename2,
                    style={'font-family':'Arial', 'height':'32px', 'margin':'0px', 'padding':'0px','border':'None', 'text-align':'left'}
                    )),

            html.Div(['Show Comparison'      , daq.BooleanSwitch(id='compare_switch',on=False, color='purple',)]),
            html.Div(['Subtract Comparison'  , daq.BooleanSwitch(id='difference_switch',on=False, color='purple',)]),

            ]),

        html.Div(id='t2_setting_div6'    , children=[
            html.Div(['Energy by bin'  , daq.BooleanSwitch(id='epb_switch',on=False, color='purple',)]),
            html.Div(['Show log(y)'     , daq.BooleanSwitch(id='log_switch',on=False, color='purple',)]),
            html.Div(['Calibration'    , daq.BooleanSwitch(id='cal_switch',on=False, color='purple',)]),
            ]),

        html.Div(id='t2_setting_div7', children=[
            html.Button('Gaussian sound <)' , id='soundbyte'),
            html.Div(id='audio', children=''),
            html.Button('Update calibration', id='update_calib_button'),
            html.Div(id='update_calib_message', children='')
        ]),

        html.Div(id='t2_setting_div8', children=[
            html.Div('Calibration Bins'),
            html.Div(dcc.Input(id='calib_bin_1', type='number', value=calib_bin_1)),
            html.Div(dcc.Input(id='calib_bin_2', type='number', value=calib_bin_2)),
            html.Div(dcc.Input(id='calib_bin_3', type='number', value=calib_bin_3)),
            html.Div('peakfinder'),
            html.Div(dcc.Slider(id='peakfinder', min=0 ,max=1, step=0.1, value= peakfinder, marks={0:'0', 1:'1'})),
            ]),

        html.Div(id='t2_setting_div9', children=[
            html.Div('Energies'),
            html.Div(dcc.Input(id='calib_e_1', type='number', value=calib_e_1)),
            html.Div(dcc.Input(id='calib_e_2', type='number', value=calib_e_2)),
            html.Div(dcc.Input(id='calib_e_3', type='number', value=calib_e_3)),
            html.Div('Gaussian corr. (sigma)'),
            html.Div(dcc.Slider(id='sigma', min=0 ,max=3, step=0.25, value= sigma, marks={0: '0', 1: '1', 2: '2', 3: '3'})),

            ]),

        html.Div(children=[ html.Img(id='footer', src='https://www.gammaspectacular.com/steven/impulse/footer.gif')]),

        html.Div(id='subfooter', children=[
            ]),

    ]) # End of tab 2 render

    return html_tab2

#------START---------------------------------

@app.callback( Output('start_text'  ,'children'),
                [Input('start'      ,'n_clicks')])

def update_output(n_clicks):
    if n_clicks is None:
        raise PreventUpdate
    else:
        mode = 2
        fn.clear_global_cps_list()
        pc.pulsecatcher(mode)
        return ''
#----STOP------------------------------------------------------------

@app.callback( Output('stop_text'  ,'children'),
                [Input('stop'      ,'n_clicks')])

def update_output(n_clicks):
    if n_clicks is None:
        raise PreventUpdate
    else:
        fn.stop_recording()
        return " "
#-------UPDATE GRAPH---------------------------------------------------------

@app.callback([ Output('bar-chart'          ,'figure'),
                Output('counts'             ,'children'),
                Output('elapsed'            ,'children'),
                Output('cps'                ,'children')],
               [Input('interval-component'  ,'n_intervals'),
                Input('filename'            ,'value'),
                Input('epb_switch'          ,'on'),
                Input('log_switch'          ,'on'),
                Input('cal_switch'          ,'on'),
                Input('filename2'           ,'value'),
                Input('compare_switch'      ,'on'),
                Input('difference_switch'   ,'on'),
                Input('peakfinder'          ,'value'),
                Input('sigma'               ,'value'),
                Input('tabs'                ,'value')
                ])

def update_graph(n, filename, epb_switch, log_switch, cal_switch, filename2, compare_switch, difference_switch, peakfinder, sigma, active_tab):

    if active_tab != 'tab2':  # only update the chart when "tab2" is active
        raise PreventUpdate

    global global_counts
    histogram1 = fn.get_path(f'{data_directory}/{filename}.json')
    histogram2 = fn.get_path(f'{data_directory}/{filename2}.json')

    if os.path.exists(histogram1):
        with open(histogram1, "r") as f:

            data = json.load(f)
            numberOfChannels    = data["resultData"]["energySpectrum"]["numberOfChannels"]
            validPulseCount     = data["resultData"]["energySpectrum"]["validPulseCount"]
            elapsed             = data["resultData"]["energySpectrum"]["measurementTime"]
            polynomialOrder     = data["resultData"]["energySpectrum"]["energyCalibration"]["polynomialOrder"]
            coefficients        = data["resultData"]["energySpectrum"]["energyCalibration"]["coefficients"]
            spectrum            = data["resultData"]["energySpectrum"]["spectrum"]
            coefficients        = coefficients[::-1] # Revese order

            now = datetime.now()
            time = now.strftime("%A %d %B %Y")

            mu = 0
            prominence = 0.5

            if sigma == 0:
                gc = []
            else:
                gc = fn.gaussian_correl(spectrum, sigma)

            if elapsed == 0:
                cps = 0
            else:
                cps = validPulseCount - global_counts
                global_counts = validPulseCount

            x = list(range(numberOfChannels))
            y = spectrum
            max_value = np.max(y)
            max_log_value = np.log10(max_value)

            if cal_switch == True:
                x = np.polyval(np.poly1d(coefficients), x)

            if epb_switch == True:
                y = [i * count for i, count in enumerate(spectrum)]
                gc= [i * count for i, count in enumerate(gc)]

            trace1 = go.Scatter(
                x=x,
                y=y,
                mode='lines+markers',
                fill='tozeroy' ,
                marker={'color': 'darkblue', 'size':3},
                line={'width':1})

  #-------------------annotations-----------------------------------------------
            peaks, fwhm = fn.peakfinder(y, prominence, peakfinder)
            num_peaks   = len(peaks)
            annotations = []
            lines       = []

            for i in range(num_peaks):
                peak_value  = peaks[i]
                counts      = y[peaks[i]]
                x_pos       = peaks[i]
                y_pos       = y[peaks[i]]
                resolution  = (fwhm[i]/peaks[i])*100

                if cal_switch == True:
                    peak_value  = np.polyval(np.poly1d(coefficients), peak_value)
                    x_pos       = peak_value

                if log_switch == True:
                    y_pos = y_pos

                if peakfinder != 0:
                    annotations.append(
                        dict(
                            x= x_pos,
                            y= y_pos + 10,
                            xref='x',
                            yref='y',
                            text=f'cts: {counts}
bin: {peak_value:.1f}
{resolution:.1f}%',
                            showarrow=True,
                            arrowhead=1,
                            ax=0,
                            ay=-40
                        )
                    )

                lines.append(
                    dict(
                        type='line',
                        x0=x_pos,
                        y0=0,
                        x1=x_pos,
                        y1=y_pos,
                        line=dict(
                            color='white',
                            width=1,
                            dash='dot'
                        )
                    )
                )

            title_text = "{}
{}".format(filename, time)

            layout = go.Layout(
                paper_bgcolor = 'white',
                plot_bgcolor = 'white',
                title={
                'text': title_text,
                'x': 0.9,
                'y': 0.9,
                'xanchor': 'center',
                'yanchor': 'top',
                'font': {'family': 'Arial', 'size': 24, 'color': 'black'},
                },
                height  =450,
                margin_t=0,
                margin_b=0,
                margin_l=0,
                margin_r=0,
                autosize=True,
                xaxis=dict(dtick=50, tickangle = 90, range =[0, max(x)]),
                yaxis=dict(autorange=True),
                annotations=annotations,
                shapes=lines,
                uirevision="Don't change",
                )
#---------------Histrogram2 ---------------------------------------------------------------------------

            if os.path.exists(histogram2):
                with open(histogram2, "r") as f:
                    data_2 = json.load(f)
                    numberOfChannels_2    = data_2["resultData"]["energySpectrum"]["numberOfChannels"]
                    elapsed_2             = data_2["resultData"]["energySpectrum"]["measurementTime"]
                    spectrum_2            = data_2["resultData"]["energySpectrum"]["spectrum"]

                    if elapsed > 0:
                        steps = (elapsed/elapsed_2)
                    else:
                        steps = 0.1

                    x2 = list(range(numberOfChannels_2))
                    y2 = [int(n * steps) for n in spectrum_2]

                    if cal_switch == True:
                        x2 = np.polyval(np.poly1d(coefficients), x2)

                    if epb_switch == True:
                        y2 = [i * n * steps for i, n in enumerate(spectrum_2)]

                    trace2 = go.Scatter(
                        x=x2,
                        y=y2,
                        mode='lines+markers',
                        marker={'color': 'red', 'size':1},
                        line={'width':2})

        if sigma == 0:
            trace4 = {}
        else:
            trace4 = go.Scatter(
                x=x,
                y=gc,
                mode='lines+markers',
                marker={'color': 'yellow', 'size':1},
                line={'width':2})

        if compare_switch == False:
            fig = go.Figure(data=[trace1, trace4], layout=layout)

        if compare_switch == True:
            fig = go.Figure(data=[trace1, trace2], layout=layout)

        if difference_switch == True:
            y3 = [a - b for a, b in zip(y, y2)]
            trace3 = go.Scatter(
                            x=x,
                            y=y3,
                            mode='lines+markers',
                            fill='tozeroy',
                            marker={'color': 'green', 'size':3},
                            line={'width':1}
                            )

            fig = go.Figure(data=[trace3], layout=layout)

            fig.update_layout(yaxis=dict(autorange=True, range=[min(y3),max(y3)]))

        if difference_switch == False:
            fig.update_layout(yaxis=dict(autorange=True))

        if log_switch == True:
            fig.update_layout(yaxis=dict(autorange=False, type='log', range=[0, max_log_value+1]))

        return fig, f'{validPulseCount}', f'{elapsed}', f'cps {cps}'

    else:
        layout = go.Layout(
                paper_bgcolor = 'white',
                plot_bgcolor = 'white',
                title={
                'text': filename,
                'x': 0.9,
                'y': 0.9,
                'xanchor': 'center',
                'yanchor': 'top',
                'font': {'family': 'Arial', 'size': 24, 'color': 'black'}
                },
                height  =450,
                autosize=True,
                xaxis=dict(dtick=50, tickangle = 90, range =[0, 100]),
                yaxis=dict(autorange=True),
                uirevision="Don't change",
                )
        return go.Figure(data=[], layout=layout), 0, 0, 0

#--------UPDATE SETTINGS------------------------------------------------------------------------------------------
@app.callback( Output('polynomial'      ,'children'),
                [Input('bins'           ,'value'),
                Input('bin_size'        ,'value'),
                Input('max_counts'      ,'value'),
                Input('max_seconds'     ,'value'),
                Input('filename'        ,'value'),
                Input('filename2'       ,'value'),
                Input('threshold'       ,'value'),
                Input('tolerance'       ,'value'),
                Input('calib_bin_1'     ,'value'),
                Input('calib_bin_2'     ,'value'),
                Input('calib_bin_3'     ,'value'),
                Input('calib_e_1'       ,'value'),
                Input('calib_e_2'       ,'value'),
                Input('calib_e_3'       ,'value'),
                Input('peakfinder'      ,'value'),
                Input('sigma'           ,'value'),
                Input('t_interval'      ,'value')
                ])

def save_settings(bins, bin_size, max_counts, max_seconds, filename, filename2, threshold, tolerance, calib_bin_1, calib_bin_2, calib_bin_3, calib_e_1, calib_e_2, calib_e_3, peakfinder, sigma, t_interval):

    database = fn.get_path(f'{data_directory}/.data.db')

    conn = sql.connect(database)
    c = conn.cursor()

    query = f"""UPDATE settings SET
                    bins={bins},
                    bin_size={bin_size},
                    max_counts={max_counts},
                    name='{filename}',
                    comparison='{filename2}',
                    threshold={threshold},
                    tolerance={tolerance},
                    calib_bin_1={calib_bin_1},
                    calib_bin_2={calib_bin_2},
                    calib_bin_3={calib_bin_3},
                    calib_e_1={calib_e_1},
                    calib_e_2={calib_e_2},
                    calib_e_3={calib_e_3},
                    peakfinder={peakfinder},
                    sigma={sigma},
                    t_interval={t_interval},
                    max_seconds={max_seconds}
                    WHERE id=0;"""

    c.execute(query)
    conn.commit()

    x_bins        = [calib_bin_1, calib_bin_2, calib_bin_3]
    x_energies    = [calib_e_1, calib_e_2, calib_e_3]

    coefficients  = np.polyfit(x_bins, x_energies, 2)
    polynomial_fn = np.poly1d(coefficients)

    conn  = sql.connect(database)
    c     = conn.cursor()

    query = f"""UPDATE settings SET
                    coeff_1={float(coefficients[0])},
                    coeff_2={float(coefficients[1])},
                    coeff_3={float(coefficients[2])}
                    WHERE id=0;"""

    c.execute(query)
    conn.commit()

    return f'Polynomial (ax^2 + bx + c) = ({polynomial_fn})'

#-------PLAY SOUND ---------------------------------------------

@app.callback( Output('audio'       ,'children'),
                [Input('soundbyte'  ,'n_clicks'),
                Input('filename2'   ,'value')])

def play_sound(n_clicks, filename2):

    if n_clicks is None:
        raise PreventUpdate
    else:
        spectrum_2 = []
        histogram2 = fn.get_path(f'{data_directory}/{filename2}.json')

        if os.path.exists(histogram2):
                with open(histogram2, "r") as f:
                    data_2     = json.load(f)
                    spectrum_2 = data_2["resultData"]["energySpectrum"]["spectrum"]

        gc = fn.gaussian_correl(spectrum_2, 1)

        asp.make_wav_file(filename2, gc)

        asp.play_wav_file(filename2)
    return

#------UPDATE CALIBRATION OF EXISTING SPECTRUM-------------------

@app.callback(
    Output('update_calib_message','children'),
    [Input('update_calib_button' ,'n_clicks'),
    Input('filename'         ,'value')
    ])

def update_current_calibration(n_clicks, filename):
    if n_clicks is None:
        raise PreventUpdate
    else:
        settings        = fn.load_settings()
        coeff_1         = round(settings[18],6)
        coeff_2         = round(settings[19],6)
        coeff_3         = round(settings[20],6)

        # Update the calibration coefficients using the specified values
        fn.update_coeff(filename, coeff_1, coeff_2, coeff_3)
        # Return a message indicating that the update was successful
        return f"Update {n_clicks}"


import pytest
from pidgin.app import *
from pidgin.errors import NoCoreMetadataException

def test_translate_dict_to_bibtex():
    input = {"object_id": "object_id_test", "key2": "value2", "key3": "value3"}
    output = translate_dict_to_bibtex(input)
    expected = '@misc {object_id_test, object_id = "object_id_test", key2 = "value2", key3 = "value3"}'
    assert output == expected

def test_flatten_dict():
    input = {"data": {"data_type_test": [{"core_metadata_collections": [{"creator": "creator_test", "description": "description_test"}], "file_name_test": "file_name", "object_id": "object_id_test"}]}}
    output = flatten_dict(input)
    expected = {"creator": "creator_test", "description": "description_test", "file_name_test": "file_name", "object_id": "object_id_test"}
    assert output == expected

def test_flatten_dict_without_core_metadata():
    """
    An exception should be raised if the core_metadata_collections field does not contain any data.
    """
    input1 = {'data': {'data_type_test': [{'core_metadata_collections': [], "file_name_test": "file_name", "object_id": "object_id_test"}]}}
    output = flatten_dict(input1)
    expected = {"file_name_test": "file_name", "object_id": "object_id_test"}
    assert output == expected

    input2 = {'data': {'data_type_test': [{"file_name_test": "file_name", "object_id": "object_id_test"}]}}
    output = flatten_dict(input2)
    assert output == expected

def test_flatten_dict_raises_exception():
    """
    An exception should be raised if a requested field was not found for this file. The details of the error should be in the exception message.
    """
    input = {"data": "null", "errors": ["error_details_test"]}
    with pytest.raises(NoCoreMetadataException) as e:
        flatten_dict(input)
    assert 'error_details_test' in e.value.args[0]


from flask import Flask, render_template

app = Flask(__name__.split('.')[0])

@app.route('/')
def render_lists():
    users = [
        {'first_name' : 'Michael', 'last_name' : 'Choi'},
        {'first_name' : 'John', 'last_name' : 'Supsupin'},
        {'first_name' : 'Mark', 'last_name' : 'Guillen'},
        {'first_name' : 'KB', 'last_name' : 'Tonel'}
    ]
    return render_template('./lists.html', alist = users)

@app.route('/', defaults={'u_path' : ''})
@app.route('/<path:u_path>')
def catch_all(u_path):
    print(repr(u_path))
    return f'Sorry! No response. Try again. But how did you fall into here?<br>{u_path}'

if __name__ == '__main__':
    app.run(debug=True)


from setuptools import setup, find_packages

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

setup(
    name='mineager',
    version='0.1.0',
    author='Prof_Bloodstone',
    description='Simple CLI tool to manage minecraft plugins on your server',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url='https://github.com/Prof-Bloodstone/',
    packages=find_packages(),
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Environment :: Console',
        'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
        'Natural Language :: English',
        'Topic :: Utilities',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
    ],
    python_requires='>=3.6',
    include_package_data=True,
    install_requires=[
        'Click',
        'PyYAML',
        'requests',
    ],
    entry_points='''
        [console_scripts]
        mineager=mineager.main:cli
    ''',
)


import logging
from contextlib import contextmanager
import sqlite3
import psycopg2

@contextmanager
def sqlite_conn_context(db_path: str):
    conn = sqlite3.connect(db_path)
    try:
        yield conn
    finally:
        conn.close()

@contextmanager
def pg_conn_context(**kwargs):
    conn = psycopg2.connect(**kwargs)
    try:
        yield conn
        conn.commit()
    except psycopg2.Error as er:
        logging.error('psycopg2.Error: %s' % (' '.join(er.args)))
        conn.rollback()
    finally:
        conn.close()


import pstats
import sys
from django import http
from django.conf import settings
from common import profile as common_profile
from common import exception

try:
  import cProfile as profile
except ImportError:
  import profile

try:
  import cStringIO as StringIO
except ImportError:
  import StringIO

class ProfileMiddleware(object):
  prof_label = None

  def process_request(self, request):
    if not settings.DEBUG:
      return

  def process_view(self, request, callback, callback_args, callback_kwargs):
    if not settings.DEBUG:
      return

    # hotshot data
    if '_prof_heavy' in request.REQUEST:
      self.profiler = profile.Profile()
      args = (request,) + callback_args
      return self.profiler.runcall(callback, *args, **callback_kwargs)

    # output data for use in the profiling code
    if ('_prof_db' in request.REQUEST
        or request.META.get('HTTP_X_PROFILE', '') == 'db'):
        self.prof_label = common_profile.label(request.path)

    # output data to be included on the page
    if '_prof_quick' in request.REQUEST:
      try:
        common_profile.install_api_profiling()
      except:
        exception.log_exception()

      self.prof_label = common_profile.label(request.path)

  def process_response(self, request, response):
    if not settings.DEBUG:
      return response

    if '_prof_heavy' in request.REQUEST:
      self.profiler.create_stats()

      out = StringIO.StringIO()
      old_stdout = sys.stdout
      sys.stdout = out

      stats = pstats.Stats(self.profiler)
      stats.sort_stats('time', 'calls')

      stats.print_stats()
      sys.stdout = old_stdout

      stats_str = out.getvalue()

      new_response = http.HttpResponse(stats_str)
      new_response['Content-type'] = 'text/plain'
      return new_response

    # NOTE: this will not work in any environment other than the dev server
    #       as this is not shared-state-safe, only one request is allowed
    #       at a time for this data to be accurate
    if ('_prof_db' in request.REQUEST
        or request.META.get('HTTP_X_PROFILE', '') == 'db'):
      self.prof_label.stop()
      csv = common_profile.csv()
      common_profile.clear()
      return http.HttpResponse(csv)

    if '_prof_quick' in request.REQUEST:
      self.prof_label.stop()
      html = common_profile.html()
      common_profile.clear()
      response.write(html)
      return response

    return response


import torch.nn.utils.spectral_norm as spectral_norm
from models.sync_batchnorm import SynchronizedBatchNorm2d
import torch.nn as nn
import torch.nn.functional as F

class SPADE(nn.Module):
    def __init__(self, opt, norm_nc, label_nc):
        super().__init__()
        self.first_norm = get_norm_layer(opt, norm_nc)
        """onnx"""
        ks =3
        nhidden = 128
        pw = ks // 2
        self.mlp_shared = nn.Sequential(
            nn.Conv2d(label_nc, nhidden, kernel_size=ks, padding=pw),
            nn.ReLU()
        )
        self.mlp_gamma = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw)
        self.mlp_beta = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw)

    def forward(self, x, segmap):
        normalized = self.first_norm(x)
        segmap = F.interpolate(segmap, size=x.size()[2:], mode='nearest')
        actv = self.mlp_shared(segmap)
        gamma = self.mlp_gamma(actv)
        beta = self.mlp_beta(actv)
        out = normalized * (1 + gamma) + beta
        return out

"""onnx"""
def get_spectral_norm(opt):
    return spectral_norm

"""onnx"""
def get_norm_layer(opt, norm_nc):
    return SynchronizedBatchNorm2d(norm_nc, affine=False)


import re
from django.db.models import F,Q,Sum
from rest_framework.views import APIView
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin,DestroyModelMixin,ListModelMixin,RetrieveModelMixin
from foods.models import Offer
from util.noitfication import NotificationTypes
from .serializers import *
from rest_framework.exceptions import MethodNotAllowed,ValidationError,PermissionDenied,NotAcceptable
from util.authentection import TokenAuth
from util.permissions import IClient
from django.utils import timezone
from django.shortcuts import get_object_or_404
from firebase_admin import messaging
class CartItemView(GenericAPIView,DestroyModelMixin):
    serializer_class=CartItemWriteSerializer
    queryset=CartItem.objects.all()
    authentication_classes=[TokenAuth]
    permission_classes=[IClient]

    def post(self,request,*args,**kwargs):
        user=request.user.client
        request.data['user']=user.id
        serailizered=CartItemWriteSerializer(data=request.data)
        if serailizered.is_valid(raise_exception=True):
            food =Food.objects.get(pk=request.data['food'])
            if user.points>= (request.data['freeItems']*(food.points or 0)):
                user.points=F('points')-(request.data['freeItems'] * (food.points or 0))
                user.save()
                serailizered.save(usedPoints=request.data['freeItems']*(food.points or 0))

                if 'additions' in request.data:
                    for id in request.data['additions']:
                        serailizered.instance.additions.add(Addition.objects.get(pk=id))
                return Response(serailizered.data,status=201)
            raise NotAcceptable()

    def delete(self,request,*args,**kwargs):
        if not 'pk' in kwargs:
            raise MethodNotAllowed(method="DELETE")
        item=get_object_or_404(CartItem,id=kwargs['pk'])
        if item.user==request.user.client:
            user=request.user.client
            dPoints= item.usedPoints
            item.delete()
            user.points=F('points')+dPoints
            user.save()
            return Response('',status=204)
        raise PermissionDenied("not your item")

class UserCartView(APIView):

    authentication_classes=[TokenAuth]
    permission_classes=[IClient]

    def get(self,request,*args,**kwargs):
        items= CartItem.objects.filter(user=request.user.client)
        for item in items:
            food=item.food
            total=0
            if hasattr(item.food,'offer') and food.offer.start<timezone.now() and food.offer.end>timezone.now():
                offer=food.offer
                if offer.type==Offer.Types.newPrice:
                    total=(offer.value)*(item.count-item.freeItems)
                elif offer.type==Offer.Types.precent:
                    total=(food.price-(food.price*offer.value/100))*(item.count-item.freeItems)

            else:
                total=food.price*(item.count-item.freeItems)
            for addition in item.additions.all():
                total+=addition.price*item.count
            item.total=total
        serializered=CartItemReadSerializer(items,many=True)
        return Response(serializered.data)

    def delete(self,request):
        items= CartItem.objects.filter(user=request.user.client)
        dPoints= 0
        for item in items:
            dPoints+=item.usedPoints
        items.delete()
        user=request.user.client
        user.points=F('points')+dPoints
        user.save()
        return Response('',status=204)
class SendOrederView(APIView):

    authentication_classes=[TokenAuth]
    permission_classes=[IClient]

    def post(self,request):
        serializered=OrderWriteSerilizer(data=request.data)
        if serializered.is_valid(raise_exception=True):
            coupon=None
            if 'promoCode' in request.data and not request.data['promoCode']==None:
                coupon=get_object_or_404(Coupon,key=request.data['promoCode'])
            serializered.save(status=Order.States.inQueue,user=request.user.client,coupon=coupon)
            items= CartItem.objects.filter(user=request.user.client)
            for item in items:
                food=item.food
                total=0
                if hasattr(item.food,'offer') and food.offer.start<timezone.now() and food.offer.end>timezone.now():
                    offer=food.offer
                    if offer.type==Offer.Types.newPrice:
                        total=(offer.value)*(item.count-item.freeItems)
                    elif offer.type==Offer.Types.precent:
                        total=(food.price-(food.price*offer.value/100))*(item.count-item.freeItems)

                else:
                    total=food.price*(item.count-item.freeItems)
                for addition in item.additions.all():
                    total+=addition.price*item.count
                item.total=total
                cartItemSeri=CartItemWriteSerializer2(instance=item)

                orderItemSeri=OrderItemWriteSerializer(data=cartItemSeri.data)
                orderItemSeri.is_valid()
                orderItemSeri.save(order=serializered.instance)
                for addition in item.additions.all():
                    orderItemSeri.instance.additions.add(addition)
            items.delete()
            orderData=OrderReadSerializer(instance=serializered.instance).data
            msg=messaging.Message(
                notification=messaging.Notification(
                    title='New Order',
                    body='there is new order'
                ),
                data={
                    'type':str(NotificationTypes.newOrder),
                    'id':str(orderData['id'])
                },
                topic='admin'

            )
            r=messaging.send(msg)
            return Response(orderData,status=201)

class OrdersView(GenericAPIView,ListModelMixin):
    serializer_class=OrderReadSerializer
    queryset=Order.objects.all().order_by('-time')

    def filter_queryset(self, queryset):
        if 'status' in self.request.GET:
            queryset=queryset.filter(status=self.request.GET['status'])

        if 'payMethod' in self.request.GET:
            queryset=queryset.filter(payMethod=self.request.GET['payMethod'])

        queryset=queryset[20*(self.kwargs['page']-1):20*(self.kwargs['page'])]
        return queryset

    def get(self,request,*args,**kwargs):
        return self.list(request,*args,**kwargs)

class OrderView(GenericAPIView,RetrieveModelMixin):
    serializer_class=OrderReadSerializer
    queryset=Order.objects.all()

    def get(self, request,*args,**kwargs):
        return self.retrieve(request,*args,**kwargs)

@api_view(['PUT'])
def setOrderStatus(request):
    if 'id' in request.data and 'status' in request.data:
        order=get_object_or_404(Order,id=request.data['id'])
        order.status=request.data['status']
        order.save()
        if order.status== Order.States.finshed:
            points=0
            for item in order.items.all():
                points+=(item.food.points or 0)*(item.count-item.freeItems)
            order.user.points=F('points')+int(points*0.1)
            order.user.save()

        if not order.status== Order.States.finshed:
            registration_token = order.user.user.fcmToken

            message = messaging.Message(
                data={
                    'type': str(NotificationTypes.orderStatus),
                    'order_id': str(order.id),
                    'status':order.status
                },
                token=registration_token,
            )

            response = messaging.send(message)

        return Response()
    raise ValidationError()

class CouponView(GenericAPIView,CreateModelMixin,ListModelMixin):
    serializer_class=CouponSerializer
    queryset=Coupon.objects.all()

    def filter_queryset(self, queryset):
        if 'key' in self.request.GET  and not self.request.GET['key']=='':
            queryset=queryset.filter(key__startswith=self.request.GET['key'])
        queryset=queryset[25*(self.kwargs['page']-1):25*(self.kwargs['page'])]
        return queryset

    def get(self,request,*args,**kwargs):
        return self.list(request,*args,**kwargs)

    def post(self,request,*args,**kwargs):
        return self.create(request,*args,**kwargs)

    def put(self,request,pk,*args,**kwargs):
        coupon=get_object_or_404(Coupon,id=pk)
        if 'value' in request.data:
            coupon.enabled=request.data['value']
            coupon.save()
            return Response(CouponSerializer(coupon).data)

class ValidateCouponView(APIView):
    authentication_classes=[TokenAuth]
    permission_classes=[IClient]

    def get(self,request,key):
        coupon=get_object_or_404(Coupon,key=key,enabled=True)
        if not Order.objects.filter(user=request.user.client,coupon=coupon).exists():
            return Response(CouponSerializer(coupon).data)

        return Response('',status=521)

class UserOrdersView(APIView):
    authentication_classes=[TokenAuth]
    permission_classes=[IClient]

    def get(self,request,page):
        historyOrders=Order.objects.filter(Q(user=request.user.client)&Q(Q(status=Order.States.finshed)|Q(status=Order.States.terminate)|Q(status=Order.States.rejected))) if page==1 else []
        currentOrders=(Order.objects.filter(user=request.user.client).exclude(Q(Q(status=Order.States.finshed)|Q(status=Order.States.terminate)|Q(status=Order.States.rejected)))  if page==1 else [] )[25*(page-1):25*page]
        historySeri=OrderReadSerializer(instance=historyOrders,many=True)
        currentSeri=OrderReadSerializer(instance=currentOrders,many=True)
        data={
            'history':historySeri.data,
            "current":currentSeri.data,
        }
        return Response(data)

class CancelOrderView(APIView):
    authentication_classes=[TokenAuth]
    permission_classes=[IClient]

    def put(self,request,pk,*args,**kwargs):
        order=get_object_or_404(Order,id=pk)
        if order.user==request.user.client and order.status ==Order.States.inQueue:
            order.status=Order.States.terminate
            order.save()
            return Response('')
        raise PermissionDenied()

class OrdersProfileView(APIView):

    def get(self,request,pk,page):
        historyOrders=Order.objects.filter(Q(user_id=pk)&Q(Q(status=Order.States.finshed)|Q(status=Order.States.terminate)|Q(status=Order.States.rejected)))
        currentOrders=(Order.objects.filter(user_id=pk).exclude(Q(Q(status=Order.States.finshed)|Q(status=Order.States.terminate)|Q(status=Order.States.rejected)))  if page==1 else [] )[25*(page-1):25*page]
        historySeri=OrderReadSerializer(instance=historyOrders,many=True)
        currentSeri=OrderReadSerializer(instance=currentOrders,many=True)
        data=[*(currentSeri.data),*(historySeri.data)]
        return Response(data)


from flask import Blueprint, request, jsonify
from app.db import db
from app.models import User, User_Game, Note, Game

bp = Blueprint('note', __name__, url_prefix='/note')

def get_game_id_by_name(game):
    result = Game.query.filter_by(name=game).first()
    if result is not None:
        return result.id
    return result

@bp.route('/add', methods=['POST'])
def add_note():
    # if the form does not contain the following keys, the request will fail
    title = request.form["title"]
    description = request.form["description"]
    url = request.form["url"]
    game = request.form["game"]
    banner = request.form["banner"]
    date = request.form["date"]

    error = None

    if error is None:
        game_id = get_game_id_by_name(game)

        if game is None:
            error = ('Game not found', 406)
        else:
            note_data = {
                "title": title,
                "description": description,
                "url": url,
                "game": game_id,
                "banner": banner,
                "date": date,
            }
            try:
                new_note = Note(note_data=note_data)
                db.session.add(new_note)
                db.session.commit()
            except Exception as e:
                error = (str(e), 500)

        if error is None:
            return jsonify( {"success": True } )

    return error

@bp.route('/get', methods=['GET'])
def get_notes_for_game():
    game = request.args.get("game", None)
    error = None

    if not game:
        error = ('Must include game name', 400)

    game_id = get_game_id_by_name(game)
    if game_id is None:
        error = ('Game not found in database', 406)
    else:
        notes = Note.query.filter_by(game_id=game_id).all()

        list_of_notes = []

        for note in notes:
            note = {"id": note.id, "title": note.title, "description": note.description, "url": note.url,
                "game": note.game_id, "banner": note.banner, "date": note.date}
            list_of_notes.append(note)

        response = jsonify({"notes": list_of_notes})
        return response

    return error

@bp.route('/get-latest-and-count', methods=['GET'])
def get_latest():
    game = request.args.get('game', None)
    error = None

    if not game:
        error = ('Must include game id', 400)

    if error is None:
        count = Note.query.filter_by(game_id=game).count()
        if count > 0:
            note = Note.query.filter_by(game_id=game).order_by(Note.date.desc()).first()
            date = note.date
            banner = note.banner
            response = jsonify( {"date": date, "banner": banner, "count": count} )
            return response

    return error


'''
@author: Megamind : Project 5, Applied Artificial Intelligence
'''

#------------------------Import all required Packages-------------------#
import pandas as pd
import lightgbm as lgb
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
import gc

#---------------------------------Data Loading---------------------------#

print('Loading Data.....')
train = pd.read_csv('./train_2016_v2.csv', low_memory=False)
properties = pd.read_csv('./properties_2016.csv', low_memory=False)
test = pd.read_csv('./sample_submission.csv', low_memory=False)

print(" ")
print("Data Loaded successfully.")

#----------------Creating a single matrix of all features------------------------#
train_data = properties
merged_data = train_data.merge(train, how='inner', on='parcelid')

#-----------------Preprocessing the data and keeping only those columns which are un-empty----------------------------#
for o in merged_data.dtypes[merged_data.dtypes==object].index.values:
    merged_data[o] = (merged_data[o]==True)

#----------The number of unreachable objects are retrned-----------------------#
gc.collect()

#----------------Splitting entire data into training and validation data set----------#
train_data, valid_data = train_test_split(merged_data, test_size=0.1, random_state=40)

ytrain = train_data.iloc[:, train_data.shape[1]-2:train_data.shape[1]-1]
xtrain = train_data.iloc[:, 0:train_data.shape[1]-2]

yvalid = valid_data.iloc[:, train_data.shape[1]-2:train_data.shape[1]-1]
xvalid = valid_data.iloc[:, 0:train_data.shape[1]-2]

#-------------------------------------------------------------------------------#
#-------------ESTIMATING OPTIMAL SETTING USING GRIDSEARCH CV----------------------#
#-------------------------------------------------------------------------------#

print(" ")
print("Tuning Hyperparamters using GridSearch CV...")
#------------Its always conventional to tune hyperparamters using development data set---------#

estimator = lgb.LGBMRegressor()

param_grid = {
    'learning_rate': [0.005,0.01,0.1,1],
    'n_estimators': [20, 30, 40],
    'max_bin' : [10],
    'num_leaves' : [50, 100],
    'objective': ['regression']

}

gs=GridSearchCV(estimator, param_grid)

gs.fit(np.array(xvalid), np.array(yvalid.values.ravel()))

print(" ")
print('Best parameters found by grid search are:', gs.best_params_)

lgb_train = lgb.Dataset(xtrain, ytrain['logerror'])

#------Select hyperparamters selected using GRIDSEARCH CV---------#
params={
'objective':gs.best_params_['objective'],
'max_bin':gs.best_params_['max_bin'],
'learning_rate':gs.best_params_['learning_rate'],
'num_leaves':gs.best_params_['num_leaves'],
'n_estimators':gs.best_params_['n_estimators']

}

clf = lgb.train(params,lgb_train)

print(" ")
print("Preparing test data for the prediction ...")

test = pd.read_csv("./sample_submission.csv")
test['parcelid'] = test['ParcelId']
test = test.merge(properties,  how='inner',
                               on='parcelid')

gc.collect()

#-------The num_threads parameter defines the maximum number of worker threads available for scheduler to use.--------#
clf.reset_parameter({"num_threads":4})

gc.collect()

for o in test.dtypes[test.dtypes==object].index.values:
    test[o] = (test[o]==True)

print(" ")
print("Starting prediction ...")
del test['ParcelId']
del test['201610']
del test['201611']
del test['201612']
del test['201710']
del test['201711']
del test['201712']

#---------------Prediction of the test data------------#

p_test = clf.predict(test, num_iteration=clf.best_iteration)

print(p_test)

print(" ")
print("Writing Results to .CSV file...")
#--------------Writing the results to .CSV file----------------------#
sub = pd.read_csv('./sample_submission.csv')
for c in sub.columns[sub.columns != 'ParcelId']:
    sub[c] = p_test

sub.to_csv('Results.csv', index=False, float_format='%.4f')