from django.urls import reverse
from django.db import models
from product.models import Product
from invoice.models import Invoice, InvoiceLine
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from main.models import NamingSeries as NS

ENTRY_STATUS = [
    ('draft', 'Draft'),
    ('submitted', 'Submitted'),
    ('cancelled', 'Cancelled'),
]
ENTRY_TYPE = [
    ('transfer', 'Transfer'),
    ('issue', 'Issue'),
    ('delivery', 'Delivery'),
    ('receipt', 'Receipt'),
]

class StockEntry(models.Model):
    code = models.CharField(max_length=10, null=True)
    date = models.DateField(null=True)
    type = models.CharField(choices=ENTRY_TYPE, default='', null=True, max_length=10)
    status = models.CharField(choices=ENTRY_STATUS, default='draft', null=True, max_length=10)
    total = models.DecimalField(default=0.0, decimal_places=2, max_digits=12)
    parent = models.ForeignKey(Invoice, related_name='ste_parent', on_delete=models.CASCADE, null=True)

    class Meta:
        ordering = ['-code']

    def __str__(self):
        return self.code

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if not self.id:
            if self.type == 'transfer':
                self.code = NS.get_series(serie='TSTE')
            if self.type == 'issue':
                self.code = NS.get_series(serie='ISTE')
            if self.type == 'delivery':
                self.code = NS.get_series(serie='DSTE')
            if self.type == 'receipt':
                self.code = NS.get_series(serie='RSTE')

        # FIXME dont calculate total fom post_save
        # when ste create from invoice only from save method

        else:
            total = 0
            lines = StockEntryLine.objects.filter(parent=self.id)
            for line in lines:
                total += line.total
            self.total = total
        return super().save(force_insert, force_update, using, update_fields)

    @classmethod
    def submit_stock_entry(cls, id=None):
        if id:
            ste = cls.objects.get(pk=id)
            if StockEntryLine.submit_stock_lines(ste):
                ste.status = 'submitted'
                ste.save()

    @classmethod
    def cancel_stock_entry(cls, id=None):
        if id:
            ste = cls.objects.get(pk=id)
            ste.status = 'cancelled'
            ste.save()

    @classmethod
    def calculate_total(cls, id=None):
        if id:
            total = 0
            lines = StockEntryLine.objects.filter(parent=id)
            for line in lines:
                total += line.total
            ste = cls.objects.get(pk=id)
            ste.total = total
            ste.save()

class StockEntryLine(models.Model):
    item = models.ForeignKey(Product, related_name='str_line_item', on_delete=models.SET_NULL, null=True)
    parent = models.ForeignKey(StockEntry, related_name='ste_line_parent', on_delete=models.SET_NULL, null=True)
    qty = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
    price = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
    status = models.CharField(choices=ENTRY_STATUS, default='draft', null=True, max_length=10)
    total = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
    invo_line = models.ForeignKey(InvoiceLine, related_name='invo_line_parent', on_delete=models.SET_NULL, null=True, blank=True)

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        self.total = self.qty * self.price
        return super().save(force_insert, force_update, using, update_fields)

    @classmethod
    def submit_stock_lines(cls, parent=None):
        lines = cls.objects.all().filter(parent=parent.id)
        if lines:
            for line in lines:
                if line.item.type == 'stockable':
                    if line.invo_line:
                        line.invo_line.qty_stock = line.invo_line.qty_stock - line.qty
                        line.invo_line.save()
                        line.invo_line.update_qty_status(line.invo_line)
                    if parent.type == 'receipt':
                        if line.item.update_product_stock(line, type='in'):
                            line.status = 'submitted'
                            line.save()
                    if parent.type == 'delivery':
                        if line.item.update_product_stock(line, type='out'):
                            line.status = 'submitted'
                            line.save()
            return True
        else:
            return False

    @classmethod
    def cancel_stock_lines(cls, parent=None):
        lines = cls.objects.all().filter(parent=parent)
        if lines:
            for line in lines:
                line.status = 'cancelled'
                line.save()
            return True
        else:
            return False

@receiver(post_save, sender=StockEntryLine)
def save_calculate_ste_lines(sender, instance, **kwargs):
    # FIXME dont calculate when create from invoice
    instance.parent.calculate_total(instance.parent.id)

@receiver(post_delete, sender=StockEntryLine)
def delete_calculate_ste_lines(sender, instance, **kwargs):
    instance.parent.calculate_total(instance.parent.id)


import pandas as pd
import numpy as np
conv = 219474.63
sos_exp = {2 : pd.Series([15.287, 16.41671, 8.713, 158.265, 404.141]),
        3 : pd.Series([112.061,77.112,15.61,396.055,882.35]),
        4 : pd.Series([826.19,557.1341,322.2,1989.497,3685.24]),
        5 : pd.Series([2212.598,1691.806,1341.893,4750.712,7603.15])}
sos_exp = pd.DataFrame(sos_exp)

line_form = 'atom: {:2} method: {:7} bs: {} ncore:{:4} {}'
line_form_sos = 'atom: {} method: {:7} bs: {} ncore:{} {:5.6E} hartree {:8.4f} cm^-1'

def error(period,group,basisset,method,frozencore,data):
    idx = pd.IndexSlice
    data_abs_error = data.copy()
    data_perc_error = data.copy()

    for a in period:
        for b in group:
            data_abs_error.loc[:,idx[a,b]] -= sos_exp[a][b]
            for bs in basisset:
                for met in method:
                    for fc in frozencore:
                        if data_perc_error.loc[idx[met,bs,fc],idx[a,b]] != 0:
                            data_perc_error.loc[idx[met,bs,fc],idx[a,b]] = 100*((data_perc_error.loc[idx[met,bs,fc],idx[a,b]]- sos_exp[a][b])/sos_exp[a][b])
                        else:
                            data_perc_error.loc[idx[met,bs,fc],idx[a,b]] = np.nan
                        if data_abs_error.loc[idx[met,bs,fc],idx[a,b]] == -sos_exp[a][b]:
                            data_abs_error.loc[idx[met,bs,fc],idx[a,b]] = np.nan
    return data_abs_error, data_perc_error

def get_sos(energy,a,g):
    sos = 0
    relenergy = []
    for i,e in enumerate(energy):
        relenergy.append((float(energy[i])-float(energy[0]))*conv)

    if len(relenergy)== 0 :
        sos = 0
    else:

        if a == 0:
            sos = relenergy[2]
        elif a == 1:
            sos = relenergy[1]
        elif a == 4:
            sos = relenergy[4]
        elif a ==2:
            sos = relenergy[8]-relenergy[4]
            if g==2:
                sos = relenergy[10]-relenergy[4]
        elif a == 3:
            sos = relenergy[5]

#                      elif a == 1:
#                        sos = relenergy[4]
#                      elif a ==2:
#                        sos = relenergy[10]-relenergy[4]
#                      elif a == 3:
    return relenergy, sos

def append_dat(title,mode,atom,method,basisset,n,energy,relenergy,sos):

    with open('data/energy'+title+'.dat', mode) as f:
        f.write(line_form.format(atom, method, basisset, n, energy))
        f.write('\n')

    with open('data/relenergy' + title + '.dat', mode) as f:
        f.write(line_form.format(atom, method, basisset, n, relenergy))
        f.write('\n')

    with open('data/sos' + title + '.dat', mode) as f:
        f.write(line_form_sos.format(atom, method, basisset, n, sos/conv, sos))
        f.write('\n')

def write_dat(title,mode):

    with open('data/energy'+title+'.dat', mode) as f:
        f.write('\n')
    with open('data/relenergy' + title + '.dat', mode) as f:
        f.write('\n')
    with open('data/sos' + title + '.dat', mode) as f:
        f.write('\n')

def get_var(g):

    if g == 2:
        nclosed = 1
        basissets = ['dz','tz','qz','fz']
        atoms = ['B','C','N','O','F']
        ncore = [0,1]
    elif g == 3:
        nclosed = 5
        basissets = ['dz','tz','qz']
        atoms = ['Al','Si','P','S','Cl']
        ncore = [5]
    elif g == 4:
        nclosed = 14
        basissets = ['dz','tz','qz']
        atoms = ['Ga','Ge','As','Se','Br']
        ncore = [14]
    elif g == 5:
        nclosed = 23
        basissets = ['tz','qz']
        atoms = ['In','Sn','Sb','Te','I']
        ncore = [23]

    return nclosed,basissets,atoms,ncore


# -- encoding:utf-8 --
import os
import argparse

import cv2
import numpy as np
from keras import backend as K
from keras.layers import Input, Activation, Conv2D, BatchNormalization, Lambda, MaxPooling2D,concatenate
from keras.models import Model
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint, TensorBoard
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF

def build_model(width, height, num_channels, classes):
    input_tensor = Input(name='the_input', shape=(width, height, num_channels), dtype='float32')
    x = input_tensor
    base_conv = 32

    for i in range(3):
        x = Conv2D(base_conv * (2 ** i), (3, 3), padding="same")(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(256, (5, 5))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    conx1 = Conv2D(256, (7, 1), padding='same')(x)
    conx1 = BatchNormalization()(conx1)
    conx2 = Conv2D(256, (5, 1), padding='same')(x)
    conx2 = BatchNormalization()(conx2)
    conx3 = Conv2D(256, (3, 1), padding='same')(x)
    conx3 = BatchNormalization()(conx3)
    conx4 = Conv2D(256, (1, 1), padding='same')(x)
    conx4 = BatchNormalization()(conx4)
    x=concatenate([conx1,conx2,conx3,conx4],axis=-1)
    x = Conv2D(1024, (1, 1))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(classes, (1, 1))(x)
    x = Activation('softmax')(x)
    model = Model(input_tensor, x, name='miniggnet')
    # y_pred = x
    # return input_tensor, y_pred
    return model


from gcp import GCPFetcher
from model import Metric
from tests.helpers import filter_metrics

def test_gcp():
    f = GCPFetcher()
    metrics = f.get("google-cloud-spanner")
    assert [
        Metric(
            "instance/cpu/utilization",
            "gauge",
            "CPU utilization",
            "Utilization of provisioned CPU, between 0 and 1.",
        )
    ] == filter_metrics(metrics, {"instance/cpu/utilization"})


"""
Implements Tokenizer() class for preprocessing and tokenizing raw-text and
Language() class for storing metrics about the language used.
"""

import re
import pickle
from tqdm import tqdm
from numpy import log, mean
from unidecode import unidecode
from collections import Counter
from flashtext import KeywordProcessor

import utils as utils

class Tokenizer(object):
    """ Stores all methods for working with text """
    # base methods
    def __init__(self, lower=True):
        assert isinstance(lower, bool), ('lower expected type bool, but found '
                                        f'type {type(lower)}.')
        self.lower = lower
        # low level attributes to be abstracted
        self.vocabSize  =   0
        self.freqDict   =   None
        self.idx        =   None
        self.reverseIdx =   None
        self.tokenizer  =   KeywordProcessor()
        # high level attribute to indicate initialization
        self.initialized = False
        # matches non-alphanumeric, space, or sentence-ending punctuation
        self.STRIP = re.compile(r'[^0-9a-zA-Z\t\n\s_.?!:;/<>*&^%$#@()"~`+-]')
        # matches sequence of tabs, newlines, spaces, underscores, and dashes
        self.SPACE = re.compile(r'[\t\n\s_.?!:;/<>*&^%$#@()"~`+-]+')

    def __str__(self):
        return (f'<Tokenizer Object: VOCAB_SIZE={self.vocabSize} | ' \
                f'LOWER={self.lower}>')

    # save/load methods
    def save(self, path):
        """ Saves Tokenizer() to folder at path """
        assert isinstance(path, str), ('path expected type str, but found '\
                                        f'type {type(path)}.')
        assert self.initialized, 'Tokenizer must be initialized before saving.'
        utils.safe_make_folder(path)
        # save neccessary attributes for lossless reconstruction
        utils.save_obj(self.freqDict, f'{path}/freqDict')
        utils.save_obj(self.idx, f'{path}/idx')
        utils.save_obj(self.tokenizer, f'{path}/tokenizer')
        with open(f'{path}/lower.sav', 'w+') as lowerFile:
            lowerStr = 't' if self.lower else 'f'
            lowerFile.write(lowerStr)
        return True

    def load(self, path):
        """ Loads Tokenizer() from folder at path """
        utils.path_exists(path)
        assert not self.initialized, ("Tokenizer file can't be loaded into an "\
                                        "initialized Tokenizer.")

        # load pickled objects
        self.freqDict = utils.load_obj(f'{path}/freqDict')
        self.idx = utils.load_obj(f'{path}/idx')
        self.tokenizer = utils.load_obj(f'{path}/tokenizer')
        # load lower bool
        with open(f'{path}/lower.sav', 'r') as lowerFile:
            lowerStr = lowerFile.read()
        self.lower = True if (lowerStr=='t') else False
        # extrapolate from loaded objects
        self.build_reverse_idx()
        self.vocabSize = len(self.freqDict)
        self.initialized = True
        return True

    # common file iterators
    def wiki_iterator(self, path='data/inData/wikiArticles.csv'):
        """ Iterates over wiki csv, yielding raw article text """
        with open(path, 'r') as wikiFile:
            for line in wikiFile:
                commaLoc = line.find(',')
                articleText = line[commaLoc+3:-3]
                yield articleText

    # methods for preprocessing text
    def to_lower(self, rawString):
        """ Lowercases string """
        return rawString.lower()

    def clean(self, rawString):
        """
        Cleans rawString by replacing spaceMatcher and tagMatcher with a single
        space, removing non-alpha chars, and lowercasing alpha chars
        """
        unicodedString = unidecode(rawString)
        # replace STRIP Matcher with ""
        cleanedString = re.sub(self.STRIP, "", rawString)
        # replace SPACE Matcher with " " and strip surround whitespace
        spacedString = re.sub(self.SPACE, " ", cleanedString)
        # lowercase the alpha chars that remain
        if self.lower:
            return self.to_lower(spacedString)
        else:
            return spacedString

    # methods for gathering language data
    def calc_tf_idf(self, termFreq, docFreq):
        """ Calcs scalar token freq score from tf and idf """
        # DEBUG: TF IDF SEEMS OFF
        return 1 + log(termFreq * docFreq)

    def freq_dict_from_file_iterator(self, iterator):
        """ Builds freq dict from file iterator. Updates vocabSize """
        # initialize counter to map tokens to raw number of occurences
        tokenCounts = Counter()
        # initialize counter to map tokens to number of docs they appear in
        tokenAppearances = Counter()
        # initialize variable to count total number of words used
        totalLength = 0
        # get base count of article num for tqdm
        articleCount = len([None for _ in iterator()])
        # iterate over wiki file
        for i, text in enumerate(tqdm(iterator(), total=articleCount)):
            # find tokens in text
            cleanText = self.clean(text)
            tokenList = cleanText.split()
            # count number of times each token appears
            currentCounts = Counter(tokenList)
            # get total length of text
            wordCount = len(tokenList)
            # add tokens counts to tokenCounts counter
            tokenCounts.update(currentCounts)
            # add single appearance for each token found
            tokenAppearances.update(set(currentCounts))
            # add number of words in current file to totalLength
            totalLength += wordCount
        # lambdas for calculating termFreq and docFreq
        calc_termFreq = lambda tokenCount : tokenCount / totalLength
        calc_docFreq = lambda tokenAppearance : log(float(i) / tokenAppearance)
        # use total num to norm tokenCounts and find frequency for each token
        freqDict = {token : self.calc_tf_idf(calc_termFreq(rawCount),
                            calc_docFreq(tokenAppearances[token]))
                    for token, rawCount in tokenCounts.items()}
        meanScore = mean([elt for elt in freqDict.values()])
        self.freqDict = freqDict
        self.vocabSize = len(freqDict)
        return True

    def filter_freq_dict(self, minFreq=0, maxFreq=1, tokenNum=50000):
        """
        Filters freq dict to tokenNum tokens between min and maxFreq. Updates
        vocab size in conjunction.
        """
        assert (tokenNum > 0), f'Filtering to tokenNum={tokenNum} would result'\
                                'in empty freqDict.'
        qualifies = lambda freq : (maxFreq > freq > minFreq)
        filteredFreqDict = {token : freq
                            for i, (token, freq)
                            in enumerate(self.freqDict.items())
                            if (qualifies(freq) and (i < tokenNum))}
        assert (filteredFreqDict != dict()), ('Filtering removed all elements '\
                                            'from freqDict. Try chaning min '\
                                            'or max frequency parameters.')
        self.freqDict = filteredFreqDict
        self.vocabSize = len(filteredFreqDict)
        return True

    # methods for tokenizer modification
    def build_tokenizer(self):
        """ Builds flashtext tokenizer from freq dict """
        assert (self.freqDict), f'freqDict must be built before tokenizer.'
        self.tokenizer.add_keywords_from_list(list(self.freqDict.keys()))
        return True

    # methods for idx modification
    def build_idx(self):
        assert (self.freqDict), f'freqDict must be built before idx.'
        self.idx = {word : i for i, word in enumerate(self.freqDict)}

    def build_reverse_idx(self):
        assert (self.idx), f'idx must be built before reverse idx.'
        self.reverseIdx = {i : word for word, i in self.idx.items()}

    # higher level initialization methods
    def language_from_wiki_file(self, minFreq, maxFreq, tokenNum):
        """
        Builds freqDict, vocabSize, tokenizer, idx, and reverse idx from wiki
        file. Takes tokenNum tokens between minFreq and maxFreq.
        """
        self.freq_dict_from_file_iterator(iterator=self.wiki_iterator)
        self.filter_freq_dict(minFreq, maxFreq, tokenNum)
        self.build_tokenizer()
        self.build_idx()
        self.build_reverse_idx()
        self.initialized = True
        return True

    # tokenization methods
    def clean_and_tokenize(self, text):
        """ Returns Counter() of recognized tokens in raw text """
        return Counter(self.tokenizer.extract_keywords(self.clean(text)))

    # mechanical token ranking in text
    def score_single_token(self, token, observedFreq):
        """ Scores a single token in text according to observed tf """
        freqDiff = self.calc_tf_idf(observedFreq, 1) - self.freqDict[token]
        if freqDiff <= 0:
            return None
        return round(freqDiff, 4)

    def single_mechanically_score_tokens(self, text):
        """
        Ranks token scores in text using freqDict and assuming no subtokens
        and converts token names to idx number
        """
        tokenCounts = self.clean_and_tokenize(text)
        wordNum = len(text.split())
        tokenScores =  {token : self.score_single_token(token,
                                                        (count/wordNum))
                        for token, count in tokenCounts.items()}
        return {self.idx[token] : score for token, score in tokenScores.items()
                if ((score != None) and (token in self.idx))}

    # def KNOWLEDGE_mechanically_score_tokens(self, text, maxChunkSize=5):
    #     """ Ranks tokenes according to freqDict and observed freq in text """
    #     # find counter of greedy tokens in text
    #     greedyTokens = self.clean_and_tokenize(test)
    #     subTokens = Counter()
    #     # iterate over greedy counter
    #     for greedyToken, greedyCount in greedyTokens.items():
    #         greedyWords = greedyToken.split()
    #         wordNum = len(greedyWords)
    #         # if multiple words in cur topToken, recursively look for sub tokens
    #         if wordNum > 1:
    #             # init chunk is 1 smaller than wordNum but capped at maxChunkSize
    #             chunkSize = min(maxChunkSize, (wordNum - 1))
    #             # iterate over greedy tokens, analyzing smaller chunks at a time
    #             while chunkSize > 0:
    #                 for i in range(wordNum):
    #                     chunkWords = greedyWords[i : i+chunkSize]
    #                     textChunk = ' '.join(chunkWords)
    #                     # if the chunk is a token in the tokenizer,
    #                     # add its count tokenCounts after norming by fraction
    #                     # of larger token and multiplying by larger token's count
    #                     # (becuase you're iterating over a set)
    #                     if textChunk in knowledgeProcessor:
    #                         subTokens.update({textChunk :
    #                                             (greedyCount * (len(chunkWords)
    #                                                             / wordNum))})
    #                 chunkSize -= 1
    #     greedyTokens.update(subTokens)
    #     return greedyTokens


import pandas as pd
CD_data=pd.read_csv('E:\python\CD_DLC.csv')
#pd.set_option("display.max.columns", None)
CD_data.head()
frames_CD=CD_data["coords"]
nosebrx=CD_data["nb_x"]
nosebry=CD_data["nb_y"]
midlinex=CD_data["ad_x"]
midliney=CD_data["ad_y"]
distx=midlinex.subtract(nosebrx)
disty=midliney.subtract(nosebry)
import numpy as np
nosebrxr=np.where(nosebrx<0,0,nosebrx)
area_nbx=print('areanosebridgeright=', np.trapz(nosebrxr))
nosebrxl=np.where(nosebrx>0,0,nosebrx)
area_nbx=print('areanosebridgeleft=', np.trapz(nosebrxl))
move_nbx=print('movement_nosebridge_horizontal=', np.std(nosebrx))
import matplotlib.pyplot as plt
plt.plot((frames_CD/30), distx)
plt.plot((frames_CD/30), disty)
plt.plot((frames_CD/30), nosebry)
plt.plot((frames_CD/30), nosebrx)
plt.ylim(-50,250)
plt.axhline(0,linestyle='dotted')
plt.show()


#!/usr/bin/env python
# encoding: utf-8
'''
@author: mj
@contact: major3428@foxmail.com
@software: pycharm
@file: paint500v2_U_THDu.py
@time: 2018-12-26 上午 9:33
@desc:
'''

import matplotlib as mpl
import pandas as pd
from pandas import DataFrame
import numpy as np
from fbprophet import Prophet
import matplotlib.pyplot as plt
from sklearn import preprocessing
import matplotlib.mlab as mlab#拟合模块
from scipy import stats
from scipy import optimize

#读取
df = pd.read_csv('./data/huabang2_Nov.csv',index_col=0)
#index_col=0去掉索引
#更名
df.rename(columns={'采集时间': 'ds', 'A相电流': 'Ia', 'B相电流': 'Ib', 'C相电流': 'Ic',
                   'A相电流THD': 'Ia_THD', 'B相电流THD': 'Ib_THD', 'C相电流THD': 'Ic_THD',
                   'A相电压': 'Ua', 'B相电压': 'Ub', 'C相电压': 'Uc',
                   'A相电压THD': 'Ua_THD', 'B相电压THD': 'Ub_THD', 'C相电压THD': 'Uc_THD',
                   '总功率因数': 'PF', '三相无功功率': 'Q', '三相有功功率': 'P',
                   'A相电流H3': 'Ia_H3', 'A相电流H5': 'Ia_H5', 'A相电流H7': 'Ia_H7',
                   'A相电流H9': 'Ia_H9', 'A相电流H11': 'Ia_H11', 'A相电流H13': 'Ia_H13',
                   'A相电流H15': 'Ia_H15', 'A相电流H17': 'Ia_H17', 'A相电流H19': 'Ia_H19',
                   'A相电流H21': 'Ia_H21',
                   'B相电流H3': 'Ib_H3', 'B相电流H5': 'Ib_H5', 'B相电流H7': 'Ib_H7',
                   'B相电流H9': 'Ib_H9', 'B相电流H11': 'Ib_H11', 'B相电流H13': 'Ib_H13',
                   'B相电流H15': 'Ib_H15', 'B相电流H17': 'Ib_H17', 'B相电流H19': 'Ib_H19',
                   'B相电流H21': 'Ib_H21',
                   'C相电流H3': 'Ic_H3', 'C相电流H5': 'Ic_H5', 'C相电流H7': 'Ic_H7',
                   'C相电流H9': 'Ic_H9', 'C相电流H11': 'Ic_H11', 'C相电流H13': 'Ic_H13',
                   'C相电流H15': 'Ic_H15', 'C相电流H17': 'Ic_H17', 'C相电流H19': 'Ic_H19',
                   'C相电流H21': 'Ic_H21'
                   },inplace = True)

print(np.max([df.Ua_THD, df.Ub_THD, df.Uc_THD], axis=1))
mu = np.mean([df.Ua, df.Ub, df.Uc,
              df.Ua_THD, df.Ub_THD, df.Uc_THD], axis=1)
#mu = np.mean(df.Ua)
sigma = np.std([df.Ua, df.Ub, df.Uc,
                df.Ua_THD, df.Ub_THD, df.Uc_THD], axis=1)
freq = len(df.ds) #总样本频次
#print(freq)

#设置字体
mpl.rcParams['font.sans-serif'] = ['Microsoft Yahei']
mpl.rcParams['font.family']='sans-serif'#解决负号'-'显示为方块的问题
mpl.rcParams['axes.unicode_minus'] = False

#配置参数
gama = 0.90
beta = 0.85
QR = np.array([len(df.query('Ua_THD < 5')),len(df.query('Ub_THD < 5')),len(df.query('Uc_THD < 5'))])/freq
UR = 1-QR
qur = np.array([len(df.query('Ua < 235')), len(df.query('Ub < 235')), len(df.query('Uc < 235'))])/freq
unr = 1 - qur
values = [[qur[0], unr[0]], [qur[1], unr[1]], [qur[2], unr[2]]]
UTHDQR = np.array([len(df.query('Ua_THD < 5')), len(df.query('Ub_THD < 5')), len(df.query('Uc_THD < 5'))])/freq
UTHDUR = 1 - UTHDQR
vals = [[UTHDQR[0], UTHDUR[0]], [UTHDQR[1], UTHDUR[1]], [UTHDQR[2], UTHDUR[2]]]

#配置颜色
color_kinds = [['orange', 'goldenrod', ['khaki', 'goldenrod'], 'darkorange'],
               ['mediumseagreen', 'green', ['mediumseagreen', 'green'], 'lightseagreen'],
               ['orangered', 'red', ['lightcoral', 'orangered']], 'lightcoral']

thd_color = [['orange', 'darkorange', ['khaki', 'goldenrod']],
             ['mediumseagreen', 'lightseagreen', ['mediumseagreen', 'green']],
             ['orangered', 'lightcoral', ['lightcoral', 'orangered']]]

#面向对象
class Paint:
    def __init__(self):
        self.pos = 'upper left'

    def paintU(self, electricity, phase, num):
        '''
        画电压
        :param electricity: 电压数组
        :param phase: 相
        :param num: A:0 B:1 C:2
        :return:
        '''
        # 画布大小 长 宽 dpi分辨率 facecolor背景颜色
        fig1 = plt.figure(figsize=[10.5, 3.55], dpi=150, facecolor='gainsboro')
        plt.subplot2grid((1, 3), (0, 0), colspan=2)
        n_u, bins_u, patches_u = plt.hist(electricity, bins=17, alpha=0.6, rwidth=0.9, color=color_kinds[num][0],
                                          label='概率', density=False)

        # 显示数字标签 ha代表对齐方式
        for a, b in zip(bins_u, n_u):
            plt.text(a, b + 2, '%.1f%%' % (b / freq * 100), ha='left', va='bottom', fontsize=6)
        # 拟合U
        y_u = stats.norm.pdf(bins_u, mu[num], sigma[num])

        alpha = np.max(n_u, axis=0) / np.max(y_u, axis=0) * beta

        norm_u = plt.plot(bins_u, y_u * alpha, color=color_kinds[num][1], linestyle='--', label='正态曲线')
        # 画参考线
        if np.max(electricity, axis=0) > 235:
            plt.vlines(235, 0, np.max(n_u, axis=0) * gama, colors=color_kinds[num][3], linestyles='-.', label='国标上限 235V')

        plt.legend(fontsize=6, loc=self.pos)
        plt.xlabel(phase+'相电压(单位:V)', fontsize=9)
        plt.ylabel('发生次数', fontsize=9)
        #plt.title(phase+'相电压', fontsize=10)

        plt.subplot2grid((1, 3), (0, 2))
        wedges1, texts1, autotexts1 = plt.pie(values[num], autopct="%.1f%%", radius=1, pctdistance=0.85,
                                              colors=color_kinds[num][2], textprops={'color': 'w'},
                                              startangle=90, wedgeprops={'width': 0.3, 'edgecolor': 'w'},
                                              )
        plt.legend(wedges1, labels=['合格率 %.1f%%' % (qur[num] * 100), '不合格率 %.1f%%' % (unr[num] * 100)], fontsize=7,
                   title=phase+'相电压', loc='center')
        fig1.set_tight_layout(True)
        plt.show()

    def paintTHDu(self, electricity, phase, num):
        fig2 = plt.figure(figsize=[10.5, 3.55], dpi=150, facecolor='gainsboro')
        plt.subplot2grid((1, 3), (0, 0), colspan=2)
        n_thdu, bins_thdu, patches_thdu = plt.hist(electricity, bins=17, alpha=0.6, rwidth=0.9, color=thd_color[num][0],
                                                      label='概率', density=False)
        print(n_thdu)  # 检查

        # 显示数字标签 ha代表对齐方式
        for a, b in zip(bins_thdu, n_thdu):
            plt.text(a, b + 2, '%.1f%%' % (b / freq * 100), ha='left', va='bottom', fontsize=6)

        if np.max(electricity, axis=0) > 5:
            plt.vlines(5, 0, np.max(n_thdu, axis=0), colors=thd_color[num][1], linestyles='-.', label='上限 5%')

        plt.legend(fontsize=6, loc=self.pos)
        plt.xlabel(phase+'相电压THD(单位:%)', fontsize=9)
        plt.ylabel('发生次数', fontsize=9)

        plt.subplot2grid((1, 3), (0, 2))
        wedges2, texts2, autotexts2 = plt.pie(vals[num], autopct="%.1f%%", radius=1, pctdistance=0.85,
                                              colors=thd_color[num][2], textprops={'color': 'w'},
                                              startangle=90, wedgeprops={'width': 0.3, 'edgecolor': 'w'}
                                              )
        plt.legend(wedges2, labels=['合格率 %.1f%%' % (UTHDQR[num] * 100), '不合格率 %.1f%%' % (UTHDUR[num] * 100)], fontsize=7,
                   title=phase+'相电压谐波含有率', loc='center')
        #plt.title(phase+'相谐波电压含有率合格率', fontsize=9)
        fig2.set_tight_layout(True)
        plt.show()
p = Paint()
#p.paintU(df.Ub, 'B', 1)
#p.paintTHDu(df.Ub_THD, 'B', 1)


import pika
import sys
import os
import Object_Storage as Obj
import Image_Tagging as Tag
import database
import Email

class RabbitMQ_Receive():
    def __init__(self):
        self.AMQP_URL = "amqps://wqzfdbgv:H0k0rWOPf6vTBaFCgJ35vZgWn657nAQZ@albatross.rmq.cloudamqp.com/wqzfdbgv"
        self.connection = pika.BlockingConnection(
            pika.URLParameters(self.AMQP_URL))
        self.channel = self.connection.channel()
        self.queue = 'hello'
        print('start')
        self.db = database.Database()

    def receive(self):
        '''Receive a message from the RabbitMQ server.'''
        self.channel.queue_declare(queue=self.queue)

        def callback(ch, method, properties, body):
            print(" [x] Received %r" % body)
            body = body.decode('utf-8')
            id = int(os.path.splitext(body)[0])
            extention = os.path.splitext(body)[1]
            file_name = body
            file_name = Obj.S3().download_file(id, extention)
            tag, is_vehicle = Tag.ImageTagging().get_tags(file_name)
            os.remove(file_name)
            if is_vehicle:
                state = 2
                self.db.update_data(id=id, state=state, category=tag)
                email = self.db.get_email(id)
                text = f'Your Advertisement with id: {id} is approved and your category is {tag}'
                subject = 'Approve Advertisment'
                Email.SendEmail().send_message(email, subject, text)

            if is_vehicle is False:
                state = 1
                self.db.update_data(id=id, state=state, category=tag)
                email = self.db.get_email(id)
                text = f'Your Advertisement with id: {id} is rejected because it is not a vehicle'
                subject = 'Reject Advertisment'
                Email.SendEmail().send_message(email, subject, text)

            print('Email Sent!')

            # print(" [x] Received %r" % body)

        self.channel.basic_consume(
            queue=self.queue, on_message_callback=callback, auto_ack=True)
        print(' [*] Waiting for messages. To exit press CTRL+C')
        self.channel.start_consuming()

RabbitMQ_Receive().receive()


from gpiozero import DistanceSensor
from time import sleep

sensor1 = DistanceSensor(echo=22, trigger=11)
sensor = DistanceSensor(echo=24, trigger=25)
#18 and 23
#17and 4
#22 and 11
#24 and 25
while True:
    x = sensor.distance
    y = sensor1.distance

    print("Right")
    print (x)
    print("lLeft")
    print (y)
    print("\n")

    sleep(1)


# -*- encoding: utf-8 -*-

##
from matplotlib import pyplot as plt
import matplotlib.style as mplstyle
import os

# 设置快速样式,加快绘图速度.并确保fast在最后
# https://blog.csdn.net/jeffery0207/article/details/81317000
mplstyle.use(['seaborn-deep', 'fast'])
# matplotlib绘图风格,参考下面链接. seaborn-deep效果不错
# https://blog.csdn.net/justisme/article/details/100543869

# 解决标签无法显示中文的问题
plt.rcParams['font.sans-serif'] = ['Helvetica']         # matlab字体
# plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']   # 微软雅黑,可显示中文
plt.rcParams['axes.unicode_minus'] = False              # 用来正常显示负号

# matplotlib 查看所有可设置字体
# import matplotlib
# a = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
# print(a)

container = []     # 容器, 用于放置axes

STAGE = 'test'     # 'test' 'show' 'save', 默认'test'

HOLD = False       # holdon 全局设置, 仅使用一次

FIGwidth, FIGheight, FIGdpi = 8, 5.8, 300

# 全局设置
def stage(stage: str, figwidth=FIGwidth, figheight=FIGheight, figdpi=FIGdpi):
    # 设置阶段
    global STAGE, FIGwidth, FIGheight, FIGdpi
    if stage.lower() in ['test', 'show', 'save']:
        STAGE = stage
    else:
        raise ValueError('wrong value of `stage`')
    # 设置图像参数
    FIGwidth, FIGheight, FIGdpi = figwidth, figheight, figdpi
    # 清空容器
    container.clear()

def subplot(*args, stick=True):
    global HOLD; HOLD = True
    plt.subplot(*args, frameon=stick)
    if STAGE.lower() in ['show','save']:
        plt.gcf().set_figwidth(FIGwidth)
        plt.gcf().set_figheight(FIGheight)

def stem(*args, label=None, linewidth=0.5, line='-', marker=' ', holdon=False):
    # holdon和HOLD有一个为True就不新建图窗
    if not (holdon or HOLD): new()
    # 绘图
    if label is None or label == '':
        ax = plt.stem(*args, linefmt=line, markerfmt=marker,  basefmt=' ')
    else:
        ax = plt.stem(*args, linefmt=line, markerfmt=marker,  basefmt=' ', label=label)
        leg = plt.legend(loc='best', frameon=False, ncol=2)
        leg.legendHandles[0].set_linewidth(linewidth)       # 设置legend的线宽
        ax[1].set_linewidth(linewidth)    # 竖线 matplotlib.collections.LineCollection
        # ax[0].set_markersize(linewidth) # 标记 matplotlib.lines.Line2D
    container.append(ax)                  # stem的axes包含3个元素
    # 绘制格网
    plt.grid(b=True, linestyle='-.', linewidth=0.5)

def plot(*args, label=None, linewidth=0.75, holdon=False):
    # holdon和HOLD有一个为True就不新建图窗
    if not (holdon or HOLD): new()
    # 绘图
    if label is None or label == '':
        ax = plt.plot(*args, linewidth=linewidth)
    else:
        ax = plt.plot(*args, linewidth=linewidth, label=label)
        plt.legend(loc='best', frameon=False, ncol=2)
    container.extend(ax)
    # 绘制格网
    plt.grid(b=True, linestyle='-.', linewidth=0.5)

def set(xlim=None, ylim=None,
        title=None, titlesize=11.5, label=None,
        xlabel=None, ylabel=None, labelsize=11.5,
        show=False):
    # 如果仅仅用于测试, 则将字体调小
    # 若用于Word文档, 12号的字体很合适
    if STAGE.lower() == 'test':
        titlesize, labelsize = 10, 10
    # limit
    if   xlim is not None: plt.xlim(xlim[0], xlim[1])
    if   ylim is not None: plt.ylim(ylim[0], ylim[1])
    # title
    if  title is not None: plt.title(title,  fontsize=titlesize)
    # legend of current axes
    if  label is not None:
        container[-1].set_label(label)
        plt.gcf().axes[-1].legend(loc='best', frameon=False, ncol=2)
    # x/y label
    if xlabel is not None: plt.xlabel(xlabel, fontsize=labelsize)
    if ylabel is not None: plt.ylabel(ylabel, fontsize=labelsize)
    # show
    if show: showing()

# 仅设置axes.lines里的legend
def legend_lines(labels=None, show=False):
    lines_gca = plt.gca().lines
    for i, l in enumerate(lines_gca):
        if labels[i] == '': continue
        l.set_label(labels[i])
    plt.legend(loc='best', frameon=False, ncol=2)
    # show
    if show: showing()

# 设置container里每一个axes的Legend
def legend(labels=None, show=False):
    # 如果lables是str类型,将其转为list
    if isinstance(labels, str): labels = [labels]
    # 逐一设置label
    for i, c in enumerate(container):
        if labels[i] == '': continue
        c.set_label(labels[i])
    # 对于每个子图，都要调用.legend()方法
    for ax in plt.gcf().axes:
        ax.legend(loc='best', frameon=False, ncol=2)
    # show
    if show: showing()

def new():  # 清空容器, 创建新fig
    global HOLD; HOLD = False
    container.clear()
    # 创建图窗
    plt.figure()
    if STAGE.lower() in ['show','save']:
        plt.gcf().set_figwidth(FIGwidth)
        plt.gcf().set_figheight(FIGheight)
        if STAGE.lower() == 'save':
            plt.gcf().set_figdpi(FIGdpi)

def save(filename='tmp.svg', path=None):
    # 获取存储路径
    if path is None: path = os.getcwd() + '/'
    # 判断路径的末尾是否存在'/'或'\\',若不存在则添加'/'
    if path[-1] != '/' and path[-2:] != '\\':
        path = path + '/'
    # save 背景是透明的
    if STAGE.lower() == 'save':
        mplstyle.use(['seaborn-deep'])            # 取消fast
        plt.savefig( path + filename, transparent=True, dpi=FIGdpi)
        mplstyle.use(['seaborn-deep', 'fast'])    # fast仅用于展示
    else:
        plt.savefig( path + filename, transparent=True)

def showing():  # 清空容器, 展示fig
    container.clear()
    # if backen is not None:
    #     plt.switch_backend(backen)
    # else:
    #     backen = 'TkAgg'
    # # 判断启用的是否是webagg
    # if backen.lower() == 'webagg':
    #     import nest_asyncio
    #     nest_asyncio.apply()
    # show完不需关闭，程序自动向下执行
    plt.ion()
    # show完不需关闭，程序自动向下执行
    plt.show()


import matplotlib.pyplot as plt
import sys
import csv

STOCK_DIR = 'stock'

#x = [1, 2, 3, 4]
#y = [1.2, 2.4, 3.0, 5.8]

#plt.plot(x, y, color="r", linestyle="--", marker="*", linewidth=1.0)

#plt.show()
ZHEN_FU_LIST = []

def openfile(code):

    try:
        f = open(STOCK_DIR+'/'+code+'.csv', 'r')
        f_csv = csv.reader(f)
    except Exception as e:
        print(e)

    return f, f_csv

def closefile(file):
    file.close()

def readzhenfu(f_csv):
    title = next(f_csv)

    for row in f_csv:
        ZHEN_FU_LIST.append(row)

def main(code):
    f, f_csv = openfile(code)
    readzhenfu(f_csv)

    closefile(f)

if __name__ == "__main__":
#    if len(sys.argv) == 2:
#        main(sys.argv[1])
#    else:
#        print('give a code...')
    main('300082')


#!/usr/bin/python3
def divisible_by_2(my_list=[]):
    if my_list is None:
        return (None)
    divs = []
    for a in my_list:
        if a % 2 == 0:
            divs.append(True)
        else:
            divs.append(False)
    return divs


import pathlib
from setuptools import setup

# The directory containing this file
HERE = pathlib.Path(__file__).parent

# The text of the README file
README = (HERE / "README.md").read_text()

# This call to setup() does all the work
setup(
    name="pviewer",
    version="1.0",
    description="pviewer is a tool which will be used to find the conflicting policies for a AWS IAM user",
    long_description=README,
    long_description_content_type="text/markdown",
    url="https://github.com/ankitsaini2609/PViewer",
    author="Ankit Saini",
    author_email="ankitsaini2609@gmail.com",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
    ],
    packages=["pviewer"],
    include_package_data=True,
    install_requires=["boto3", "graphviz", "pytest"],
    entry_points={
        "console_scripts": [
            "pviewer=pviewer.__main__:main",
        ]
    },
)


from ..runner import Sequential
from ..utils import Registry, build_from_cfg

def build_model_from_cfg(cfg, registry, default_args=None):
    """Build a PyTorch model from config dict(s). Different from
    ``build_from_cfg``, if cfg is a list, a ``nn.Sequential`` will be built.

    Args:
        cfg (dict, list[dict]): The config of modules, is is either a config
            dict or a list of config dicts. If cfg is a list, a
            the built modules will be wrapped with ``nn.Sequential``.
        registry (:obj:`Registry`): A registry the module belongs to.
        default_args (dict, optional): Default arguments to build the module.
            Defaults to None.

    Returns:
        nn.Module: A built nn module.
    """
    if isinstance(cfg, list):
        modules = [
            build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg
        ]
        return Sequential(*modules)
    else:
        return build_from_cfg(cfg, registry, default_args)

MODELS = Registry('model', build_func=build_model_from_cfg)


'''get a word from input. if the first letter in the word is consonant -> more it to the end of the word and add "ay" to it
   if the word beging with vowel  = just add "way to the end of it."
'''
def pig_latin_game():
    print("Welcome to the silly application called 'Pig Latin'. It messes up the words you input in some manner. \n")
    print(" Give it a try by inputing a word.\n")
    print("Say 'EXIT' to quit. \n")
    while True:
    #Determine user input
        words = []
        entries = input("Say the word! ")
        words = entries.split()
        new_words = []
        for word in words:
            if word[0] in ("a", "e", "o", 'u', 'i',):
                new_word = word + "way"
                new_words.append(new_word)
                #most probably you want to use REGEX here :)
            elif word[0] in ("q", "w", 'r', 't', 'y', 'p', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'):
                new_word = word[1:]+word[0]+"ay"
                new_words.append(new_word)
            else:
                print("This doesn't actually look like a word to me. Try again. ")

        if entries == "EXIT":
                break

        print("\n")
        print(" ".join(new_words))
        print("\n")

pig_latin_game()
print("See you later")

'''this can be improved by accepting cap letters as well, also REGEX. + this is not getting inputs in format like
"Hello! This is me, Mario!"'''


import requests

# ------------ CONS ------------- #

MY_LAT = 54.372158
MY_LONG = 18.638306

# ------------ API connection --------------- #

parameters = {
    "lat": MY_LAT,
    "lng": MY_LONG,
}

response = requests.get(url="https://api.sunrise-sunset.org/json", params=parameters, verify=False)
response.raise_for_status()
data = response.json()

# ------------- Show Results -------------- #

print(f"Sunrise: {data['results']['sunrise']}")
print(f"Sunset: {data['results']['sunset']}")
print(f"Day Length: {data['results']['day_length']}")


#! /usr/bin/env python3

import argparse
import csv
import glob
import json
import os

def parseArguments(args):
    parser = argparse.ArgumentParser(description="Gathers stats after a run")
    parser.add_argument(
        "-o",
        "--outdir",
        default="cases",
        help="Directory to generate the test suite in.",
    )
    parser.add_argument(
        "-f",
        "--file",
        type=argparse.FileType("w"),
        default="stats.csv",
        help="The resulting CSV file containing all stats.",
    )
    return parser.parse_args(args)

def statsFromTimings(dir):
    stats = {}
    assert os.path.isdir(dir)
    assert (
        os.system("command -v precice-profiling > /dev/null") == 0
    ), 'Could not find the profiling tool "precice-profiling", which is part of the preCICE installation.'
    event_dir = os.path.join(dir, "precice-profiling")
    json_file = os.path.join(dir, "profiling.json")
    timings_file = os.path.join(dir, "timings.csv")
    os.system("precice-profiling merge --output {} {}".format(json_file, event_dir))
    os.system(
        "precice-profiling analyze --output {} B {}".format(timings_file, json_file)
    )
    file = timings_file
    if os.path.isfile(file):
        try:
            timings = {}
            with open(file, "r") as csvfile:
                timings = csv.reader(csvfile)
                for row in timings:
                    if row[0] == "_GLOBAL":
                        stats["globalTime"] = row[-1]
                    if row[0] == "initialize":
                        stats["initializeTime"] = row[-1]
                    if row[0].startswith("initialize/map") and row[0].endswith(
                        "computeMapping.FromA-MeshToB-Mesh"
                    ):
                        computeMappingName = row[0]
                        stats["computeMappingTime"] = row[-1]
                    if row[0].startswith("advance/map") and row[0].endswith(
                        "mapData.FromA-MeshToB-Mesh"
                    ):
                        mapDataName = row[0]
                        stats["mapDataTime"] = row[-1]
        except BaseException:
            pass
    return stats

def memoryStats(dir):
    stats = {}
    assert os.path.isdir(dir)
    for P in "A", "B":
        memfile = os.path.join(dir, f"memory-{P}.log")
        total = 0
        if os.path.isfile(memfile):
            try:
                with open(memfile, "r") as file:
                    total = sum([float(e) / 1024.0 for e in file.readlines()])
            except BaseException:
                pass
        stats[f"peakMem{P}"] = total

    return stats

def main(argv):
    args = parseArguments(argv[1:])

    globber = os.path.join(args.outdir, "**", "*.stats.json")
    statFiles = [
        os.path.relpath(path, args.outdir)
        for path in glob.iglob(globber, recursive=True)
    ]
    allstats = []
    fields = []
    for file in statFiles:
        print("Found: " + file)
        casedir = os.path.join(args.outdir, os.path.dirname(file))
        parts = os.path.normpath(file).split(os.sep)
        assert len(parts) >= 5
        mapping, constraint, meshes, ranks, _ = parts[-5:]
        meshA, meshB = meshes.split("-")
        ranksA, ranksB = ranks.split("-")

        with open(os.path.join(args.outdir, file), "r") as jsonfile:
            stats = json.load(jsonfile)
            stats["mapping"] = mapping
            stats["constraint"] = constraint
            stats["mesh A"] = meshA
            stats["mesh B"] = meshB
            stats["ranks A"] = ranksA
            stats["ranks B"] = ranksB
            stats.update(statsFromTimings(casedir))
            stats.update(memoryStats(casedir))
            allstats.append(stats)
            if not fields:
                fields += stats.keys()

    assert fields
    writer = csv.DictWriter(args.file, fieldnames=fields)
    writer.writeheader()
    writer.writerows(allstats)
    return 0

if __name__ == "__main__":
    import sys

    sys.exit(main(sys.argv))


import cv2
import numpy
import numpy as np

import utils
# This is a sample Python script.

########
web_cam = False
image1 = './images/toma11_1.png' ##1200*901
image2 = './images/toma11_2.png' ##1200*901
image3 = './images/toma11_3.png' ##1200*901
#image3 = './images/toma12_2.jpg'
cap = cv2.VideoCapture(0)
cap.set(10, 160)
cap.set(3, 1920)
cap.set(4, 1080)
scale = 1
wP = 1500 * scale # el original fue 210 * scale
hP = 2250 * scale # 297 * scale
########

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

# set the lower and upper bounds for the green hue
lower_green = np.array([50, 100, 50])
upper_green = np.array([70, 255, 255])
lower_red = np.array([160, 100, 50])
upper_red = np.array([180, 255, 255])
lower_yel = np.array([15, 50, 0])
upper_yel = np.array([35, 255, 255])
lower_white = np.array([0, 0, 60])
upper_white = np.array([107, 111, 62])
lower_blue = np.array([100, 150, 0])
upper_blue = np.array([140, 255, 255])

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

def show_image():
    img1 = cv2.imread(image1)
    img2 = cv2.imread(image2)
    img3 = cv2.imread(image3)

    img1 = cv2.resize(img1, (0, 0), None, 0.5, 0.5)
    img2 = cv2.resize(img2, (0, 0), None, 0.5, 0.5)
    img3 = cv2.resize(img3, (0, 0), None, 0.5, 0.5)

    cv2.imshow('Original_1', img1)
    cv2.imshow('Original_2', img2)
    cv2.imshow('Original_3', img3)
    cv2.waitKey(0)

    # convert the BGR image to HSV colour space
    hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)

    ## create a mask for green colour using inRange function
    ymask = cv2.inRange(hsv, lower_yel, upper_yel)
    #rmask = cv2.inRange(hsv, lower_red, upper_red)
    ##mask = cv2.bitwise_or(ymask, rmask)
    mask = ymask

    #cv2.imshow('yell_mask', mask)
    res1 = cv2.bitwise_and(img1, img1, mask=mask)
    #cv2.imshow('Original_yel1', res1)

    hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
    ymask = cv2.inRange(hsv, lower_blue, upper_blue)
    #rmask = cv2.inRange(hsv, lower_red, upper_red)
    #mask = cv2.bitwise_or(ymask, rmask)
    mask = ymask
    res2 = cv2.bitwise_or(img1, img1, mask=mask)
    cv2.imshow('Original_blue', res2)
    cv2.waitKey(0)

    hsv = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)
    ymask = cv2.inRange(hsv, lower_yel, upper_yel)
    rmask = cv2.inRange(hsv, lower_red, upper_red)
    #mask = cv2.bitwise_or(ymask, rmask)
    mask = ymask
    #mask = cv2.inRange(hsv, lower_red, upper_red)
    res3 = cv2.bitwise_and(img3, img3, mask=mask)
    cv2.imshow('Original_yel3', res3)

    utils.getContours(res1, showCanny=True, draw=True)
    cv2.waitKey(0)
    utils.getContours(res2, showCanny=True, draw=True)
    cv2.waitKey(0)
    utils.getContours(res3, showCanny=True, draw=True)
    cv2.waitKey(0)

    cv2.destroyAllWindows()

def originalSizing():
    img = cv2.imread(image3)
    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)

    cv2.imshow('Original', img)

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    ymask = cv2.inRange(hsv, lower_yel, upper_yel)
    wmask = cv2.inRange(hsv, lower_white, upper_white)
    bmask = cv2.inRange(hsv, lower_blue, upper_blue)
    mask = cv2.bitwise_or(ymask, bmask)
    #mask = bmask
    #cv2.imshow('blue&yell_mask', mask)
    res1 = cv2.bitwise_and(img, img, mask=mask)
    cv2.imshow('Original_yel1', res1)
    cv2.waitKey(0)

    imgContours, conts = utils.getContours(res1, minArea=20000, filter=4, showCanny=True)
    if len(conts) != 0:
        biggest = conts[0][2]
        # print(biggest)
        imgWarp = utils.warpImg(img, biggest, wP, hP)
        imgContours2, conts2 = utils.getContours(imgWarp, showCanny=True,
                                                 minArea=2000, filter=4,
                                                 draw=True)
        #cv2.imshow('warp', imgWarp)
        #cv2.imshow('contours2', imgContours2)
        #cv2.waitKey(0)
        if len(conts) != 0:
            for obj in conts2:
                cv2.polylines(imgContours2, [obj[2]], True, (0, 255, 0), 2)
                nPoints = utils.reorder(obj[2])
                nW = round((utils.findDis(nPoints[0][0] // scale, nPoints[1][0] // scale) / 10), 1)
                nH = round((utils.findDis(nPoints[0][0] // scale, nPoints[2][0] // scale) / 10), 1)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]),
                                (nPoints[1][0][0], nPoints[1][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]),
                                (nPoints[2][0][0], nPoints[2][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                x, y, w, h = obj[3]
                cv2.putText(imgContours2, '{}cm'.format(nW), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
                cv2.putText(imgContours2, '{}cm'.format(nH), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
        imgContours2 = cv2.resize(imgContours2, (0, 0), None, 0.5, 0.5)
        cv2.imshow('A4', imgContours2)

    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    cv2.imshow('Original', img)
    cv2.waitKey(0)

def filter_image(img, lower_mask, upper_mask):
    if type(img) != numpy.ndarray:
        img = cv2.imread(img)
    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    fmask = cv2.inRange(hsv, lower_mask, upper_mask)
    res1 = cv2.bitwise_and(img, img, mask=fmask)
    return res1

def filter_image_or(img, lower_mask1, upper_mask1, lower_mask2, upper_mask2):
    if type(img) != numpy.ndarray:
        img = cv2.imread(img)

    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask1 = cv2.inRange(hsv, lower_mask1, upper_mask1)
    mask2 = cv2.inRange(hsv, lower_mask2, upper_mask2)
    ormask = cv2.bitwise_or(mask1, mask2)
    res1 = cv2.bitwise_or(img, img, mask=ormask)
    return res1

def blue_image(image):
    return filter_image(image, lower_blue, upper_blue)

def yellow_image(image):
    return filter_image(image, lower_yel, upper_yel)

def blue_yellow_image(image):
    return filter_image_or(image, lower_blue, upper_blue, lower_yel, upper_yel)

def get_corners(image):
    img = cv2.imread(image)
    if img is None:
        print(image+" not found")
        return
    resizeFactor = 0.5
    resBlue = blue_image(image)
    #cv2.imshow('Original_blue', resBlue)
    resYell = yellow_image(image)
    #cv2.imshow('Original_yellow', resYell)
    img = cv2.resize(img, (0, 0), None, resizeFactor, resizeFactor)
    basePoints, _ = utils.getBaseContours(resBlue, minArea=20000, showCanny=False)

    #imgDoted = utils.mark_points(basePoints, img, len(basePoints))
    cv2.imshow('Original', img)
    cv2.waitKey(0)

    imgWrp = utils.warpImg(img, basePoints, wP, hP, 20)
    cv2.imshow('Original warped',  cv2.resize(imgWrp, (0, 0), None, resizeFactor, resizeFactor))
    cv2.waitKey(0)
    #imgWrp = cv2.resize(imgWrp, (0, 0), None, resizeFactor, resizeFactor)

    resYell = yellow_image(imgWrp)
    boxPoints, contsBox = utils.getBoxContours(resYell, minArea=20000, showCanny=False, draw=True)
    resBoth = blue_yellow_image(imgWrp)
    completeImg = utils.mark_points(boxPoints, resBoth, len(boxPoints))

    triPoints = utils.get3Points(boxPoints)

    pairOfPoints = utils.pairPoints(triPoints) #boxPoints

    for pointsPair in pairOfPoints:
        point1 = pointsPair[0]
        point2 = pointsPair[1]
        dist = round(utils.findDis(point1, point2) // resizeFactor // 10, 1)

#           ancho = round((utils.findDis(nPoints[0][0] // scale, nPoints[1][0] // scale) / 10), 1)
#           alto = round((utils.findDis(nPoints[0][0] // scale, nPoints[2][0] // scale) / 10), 1)
        cv2.arrowedLine(completeImg, (point1[0][0], point1[0][1]), (point2[0][0], point2[0][1]),
                        (255, 0, 255), 3, 8, 0, 0.05)
        cv2.putText(completeImg, '{}cm'.format(dist), (point1[0][0] + 30, point1[0][1] ), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                    (255, 0, 255), 2)
#           cv2.arrowedLine(imgWrp, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[2][0][0], nPoints[2][0][1]),
#                           (255, 0, 255), 3, 8, 0, 0.05)
#           x, y, w, h = fig[3]
    cv2.imshow('Warped_doted', completeImg)
    cv2.waitKey(0)

def makeTriFiles(root, seq):
    trio = ["", "", ""]
    for i in range(3):
        file = f"{seq}_{i+1}.jpg" #= './images/toma12_2.jpg'
        trio[i] = root + file
    return trio

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    #show_image()
    #originalSizing()
    #get_corners(image3)
    trio = makeTriFiles('./images/toma', 11)
    for f in trio:
        get_corners(f)

# See PyCharm help at https://www.jetbrains.com/help/pycharm/


import dataclasses
import numpy as np

from foundations import hparams
import models.base
from pruning import base
from pruning.mask import Mask

@dataclasses.dataclass
class PruningHparams(hparams.PruningHparams):
    pruning_fraction: float = 0.2
    pruning_layers_to_ignore: str = None

    _name = 'Hyperparameters for Sparse Global Pruning'
    _description = 'Hyperparameters that modify the way pruning occurs.'
    _pruning_fraction = 'The fraction of additional weights to prune from the network.'
    _layers_to_ignore = 'A comma-separated list of addititonal tensors that should not be pruned.'

class Strategy(base.Strategy):
    @staticmethod
    def get_pruning_hparams() -> type:
        return PruningHparams

    @staticmethod
    def prune(pruning_hparams: PruningHparams, trained_model: models.base.Model, current_mask: Mask = None):
        current_mask = Mask.ones_like(trained_model).numpy() if current_mask is None else current_mask.numpy()

        # Determine the number of weights that need to be pruned.
        number_of_remaining_weights = np.sum([np.sum(v) for v in current_mask.values()])
        number_of_weights_to_prune = np.ceil(
            pruning_hparams.pruning_fraction * number_of_remaining_weights).astype(int)

        # Determine which layers can be pruned.
        prunable_tensors = set(trained_model.prunable_layer_names)
        if pruning_hparams.pruning_layers_to_ignore:
            prunable_tensors -= set(pruning_hparams.pruning_layers_to_ignore.split(','))

        # Get the model weights.
        weights = {k: v.clone().cpu().detach().numpy()
                   for k, v in trained_model.state_dict().items()
                   if k in prunable_tensors}

        # Create a vector of all the unpruned weights in the model.
        weight_vector = np.concatenate([v[current_mask[k] == 1] for k, v in weights.items()])
        threshold = np.sort(np.abs(weight_vector))[number_of_weights_to_prune]

        new_mask = Mask({k: np.where(np.abs(v) > threshold, current_mask[k], np.zeros_like(v))
                         for k, v in weights.items()})
        for k in current_mask:
            if k not in new_mask:
                new_mask[k] = current_mask[k]

        return new_mask


import time
import datetime
import random
import json
from django.shortcuts import render
from api.models import temp_log
from api.serializers import tempSerializer
import datetime

date = '2022-03-28T22:40:41.730193+05:30'
date = date.split(".")[0]
datem = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
print(datem.day)

def index(request):
    context = {}
    context['temp'] = 0
    context['humidity'] = 0
    return render(request, 'home.html', context)

def linechart(request):
    """
    lineChart page
    """

    context = {}
    queryset = temp_log.objects.all().order_by('-id')
    ser = tempSerializer(queryset, many=True)
    first = ser.data[0]["time"]
    date = first.split(".")[0]
    f_date = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S').day

    arr = [['Time', 'Temperature']]

    for i in ser.data:
        date = i["time"].split(".")[0]
        datem = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
        if f_date == datem.day:
            time = str(datem.hour) + ":"+str(datem.minute)
            arr.append([time, i["temp"]])

    context['temp'] = json.dumps(arr)
    print(context['temp'])

    return render(request, 'linechart.html', context)


#!/usr/bin/env python3

import json
from copy import deepcopy

import pytest

from vyper.cli.vyper_json import _parse_args
from vyper.exceptions import JSONError

FOO_CODE = """
import contracts.bar as Bar

@external
def foo(a: address) -> bool:
    return Bar(a).bar(1)
"""

BAR_CODE = """
@external
def bar(a: uint256) -> bool:
    return True
"""

BAR_ABI = [
    {
        "name": "bar",
        "outputs": [{"type": "bool", "name": "out"}],
        "inputs": [{"type": "uint256", "name": "a"}],
        "stateMutability": "nonpayable",
        "type": "function",
    }
]

INPUT_JSON = {
    "language": "Vyper",
    "sources": {
        "contracts/foo.vy": {"content": FOO_CODE},
        "contracts/bar.vy": {"content": BAR_CODE},
    },
    "interfaces": {"contracts/ibar.json": {"abi": BAR_ABI}},
    "settings": {"outputSelection": {"*": ["*"]}},
}

def _no_errors(output_json):
    return "errors" not in output_json or not any(
        err["severity"] == "error" for err in output_json["errors"]
    )

def test_to_stdout(tmp_path, capfd):
    path = tmp_path.joinpath("input.json")
    with path.open("w") as fp:
        json.dump(INPUT_JSON, fp)
    _parse_args([path.absolute().as_posix()])
    out, _ = capfd.readouterr()
    output_json = json.loads(out)
    assert _no_errors(output_json), (INPUT_JSON, output_json)
    assert "contracts/foo.vy" in output_json["sources"]
    assert "contracts/bar.vy" in output_json["sources"]

def test_to_file(tmp_path):
    path = tmp_path.joinpath("input.json")
    with path.open("w") as fp:
        json.dump(INPUT_JSON, fp)
    output_path = tmp_path.joinpath("output.json")
    _parse_args([path.absolute().as_posix(), "-o", output_path.absolute().as_posix()])
    assert output_path.exists()
    with output_path.open() as fp:
        output_json = json.load(fp)
    assert _no_errors(output_json), (INPUT_JSON, output_json)
    assert "contracts/foo.vy" in output_json["sources"]
    assert "contracts/bar.vy" in output_json["sources"]

def test_pretty_json(tmp_path, capfd):
    path = tmp_path.joinpath("input.json")
    with path.open("w") as fp:
        json.dump(INPUT_JSON, fp)
    _parse_args([path.absolute().as_posix()])
    out1, _ = capfd.readouterr()
    _parse_args([path.absolute().as_posix(), "--pretty-json"])
    out2, _ = capfd.readouterr()
    assert len(out2) > len(out1)
    assert json.loads(out1) == json.loads(out2)

def test_traceback(tmp_path, capfd):
    path = tmp_path.joinpath("input.json")
    input_json = deepcopy(INPUT_JSON)
    del input_json["sources"]
    with path.open("w") as fp:
        json.dump(input_json, fp)
    _parse_args([path.absolute().as_posix()])
    out, _ = capfd.readouterr()
    output_json = json.loads(out)
    assert not _no_errors(output_json)
    with pytest.raises(JSONError):
        _parse_args([path.absolute().as_posix(), "--traceback"])


# from stacks_and_queues import Node, InvalidOperationError

class Node:
    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next

    def getValue(self):
        return self.value

    def getNext(self):
        return self.next

class AnimalShelter:
    def __init__(self, front=None, rear=None):
        self.front = front
        self.rear = rear

    def enqueue(self, value=None):
        animal = Node(value.lower())
        if self.front is None:
            self.front = animal
            self.rear = animal
        else:
            self.rear.next = animal
            self.rear = animal

    def dequeue(self, pref=None):
        if pref is None:
            return self.front.value
        pref = pref.lower()
        if pref != 'cat' and pref != 'dog':
            return None
        else:
            current = self.front
            while current.value != pref:
                current = current.next
            return current.value

x = AnimalShelter()

x.enqueue('cat')
x.enqueue('dog')
x.enqueue('dog')

y = x.dequeue('MONKEY')

print(x.front.value, y)


from rest_framework import status
from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from rest_framework.exceptions import APIException
from rest_framework.response import Response
from .serializers import DeliverySerializer, DeliveryDetailSerializer, DeliveryListSerializer, DeliveryUpdateSerializer
from ..canceled_order.serializers import CanceledOrderSerializer, CanceledOrderDetailSerializer
from ..order.serializers import OrderSerializer, OrderedProductSerializer

class DeliveryViewSet(ModelViewSet):
    queryset = DeliverySerializer.Meta.model.objects.all()
    lookup_field = 'order'

    def get_serializer_class(self):
        if self.action == 'list':
            return DeliveryListSerializer
        if self.action == 'retrieve':
            return DeliveryDetailSerializer
        if self.action in ['update', 'partial_update']:
            return DeliveryUpdateSerializer
        return DeliverySerializer

    def create(self, request, *args, **kwargs):
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(data=request.data)
        if serializer.is_valid():
            order = OrderSerializer.Meta.model.objects.filter(id=request.data['order']).first()
            serializer.save()
            order.state = serializer.data['status']
            order.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def update(self, request, pk=None, *args, **kwargs):
        partial = self.kwargs.pop('partial', False)
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        if serializer.is_valid():
            if 'status' in request.data:
                order = instance.order
                order.state = request.data.get('status')
                order.save()
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def partial_update(self, request, *args, **kwargs):
        self.kwargs['partial'] = True
        return self.update(request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):
        reason = request.data.get('reason', 'No especified')
        instance = self.get_object()
        order = instance.order
        data = {
            'reason': reason,
            'order': order.id
        }
        serializer = CanceledOrderSerializer(data=data)
        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        serializer.save()
        order.state = 'Cancelled'
        order.save()
        ordered = OrderedProductSerializer.Meta.model.objects.filter(order=order)
        for ordered_product in ordered:
            product = ordered_product.product
            product.quantity += ordered_product.quantity
            product.save()
        instance.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


from pyspark.sql import SparkSession

if __name__ == "__main__":

    spark = SparkSession\
        .builder\
        .master("local[*]") \
        .appName("retail") \
        .getOrCreate()

    df = spark.read \
        .option("header", "true") \
        .csv("./data/online-retail-dataset.csv") \
        .repartition(2)

    result = df.selectExpr("instr(Description, 'GLASS') >= 1 as is_glass") \
        .groupBy("is_glass") \
        .count() \
        .collect()

    print(result)
    # sit here so you can look at the SparkUI
    # localhost:4040
    ip = input("Press return to end: ")

    spark.stop()


import pandas as pd
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from scipy import stats
from sklearn.linear_model import LinearRegression
import statsmodels.formula.api as smf

def import_data():

    Two_wk_advanced = pd.read_csv("Data\\2wk_Advanced_Pitchers.csv")
    Two_wk_batted = pd.read_csv("Data\\2wk_Batted_Ball_Pitchers.csv")

    ROS_advanced = pd.read_csv("Data\\ROS_Advanced_Pitchers.csv")
    ROS_batted = pd.read_csv("Data\\ROS_Batted_Ball_Pitchers.csv")

    Two_wk_advanced.columns = Two_wk_advanced.columns.str.replace(r"[%]", "per")
    Two_wk_batted.columns = Two_wk_batted.columns.str.replace(r"[%]", "per")

    ROS_advanced.columns = ROS_advanced.columns.str.replace(r"[%]", "per")
    ROS_batted.columns = ROS_batted.columns.str.replace(r"[%]", "per")

    Two_wk_advanced.columns = Two_wk_advanced.columns.str.replace(r"[/]", "per")
    Two_wk_batted.columns = Two_wk_batted.columns.str.replace(r"[/]", "per")

    ROS_advanced.columns = ROS_advanced.columns.str.replace(r"[/]", "per")
    ROS_batted.columns = ROS_batted.columns.str.replace(r"[/]", "per")

    Two_wk_advanced.columns = Two_wk_advanced.columns.str.replace(r"[-]", "minus")
    Two_wk_batted.columns = Two_wk_batted.columns.str.replace(r"[-]", "minus")

    ROS_advanced.columns = ROS_advanced.columns.str.replace(r"[-]", "minus")
    ROS_batted.columns = ROS_batted.columns.str.replace(r"[-]", "minus")

    Two_wk_cols_to_use = Two_wk_advanced.columns.difference(Two_wk_batted.columns)
    ROS_cols_to_use = ROS_advanced.columns.difference(ROS_batted.columns)

    df_Two_wk = pd.merge(Two_wk_batted, Two_wk_advanced[Two_wk_cols_to_use], left_index=True, right_index=True, how='outer')
    df_ROS = pd.merge(ROS_batted, ROS_advanced[ROS_cols_to_use], left_index=True, right_index=True, how='outer')

    columns = list(df_ROS)

    pitcher_data = pd.merge(df_ROS, df_Two_wk, on='Name', how='inner', suffixes=('','_two_wk'))

    return pitcher_data, columns

def corr_test(data):

    data['eKper9'] = (data['Kper9']*29.32) + (data['Pullper']*-20.85) + (data['IFHper']*16.98) + 9.53
    print('K/9 Coeff:',data['eKper9'].corr(data['Kper9_two_wk']))

    data['eBBper9'] = (data['BBper']*21.39) + (data['LDper']*-14.04) + (data['TBF']*0.0024) + (data['Centper']*10.35) + (data['Softper']*-8.66) + 0.91
    print('BB/9 Coeff:',data['eBBper9'].corr(data['BBper9_two_wk']))

    data['eHRper9'] = (data['HRper9']*0.86) + (data['IFFBper']*5.42) + (data['Pullper']*-2.95) + 0.749
    print('HR/9 Coeff:',data['eHRper9'].corr(data['HRper9_two_wk']))

def main():

    batter_data, columns = import_data()

    corr_test(batter_data)

main()


import h5py

import os
import numpy as np
from sortedcontainers import SortedList
from torch.utils.data import Dataset
import glob
import string

from tqdm import tqdm
import DALI as dali_code
from utils import load, write_wav, load_lyrics, ToolFreq2Midi

import soundfile as sf

import logging
# logging.basicConfig(level=logging.DEBUG)

def getDALI(database_path, level, lang, genre):
    dali_annot_path = os.path.join(database_path, 'annot_tismir')
    dali_audio_path = os.path.join(database_path, 'audio')
    dali_data = dali_code.get_the_DALI_dataset(dali_annot_path, skip=[], keep=[])

    # get audio list
    audio_list = os.listdir(os.path.join(dali_audio_path))

    subset = list()
    duration = list()
    total_line_num = 0
    discard_line_num = 0

    for file in audio_list:
        if file.endswith('.mp3') and os.path.exists(os.path.join(dali_annot_path, file[:-4] + '.gz')):
            # get annotation for the current song
            try:
                entry = dali_data[file[:-4]]
                entry_info = entry.info

                # language filter
                if lang is not None and entry_info['metadata']['language'] != lang:
                    continue
                # genre filter
                if genre is not None and genre not in entry_info['metadata']['genres']:
                    continue

                song = {"id": file[:-4], "annot": [], "path": os.path.join(dali_audio_path, file)}
                samples = entry.annotations['annot'][level]

                notes_raw = entry.annotations['annot']["notes"]
                notes = [{"freq": note_raw['freq'][0], "pitch": ToolFreq2Midi(note_raw['freq'][0]), "time": note_raw['time']} for note_raw in
                         notes_raw]
                song["notes"] = notes

                subset.append(song)

                for sample in samples:
                    sample["duration"] = sample["time"][1] - sample["time"][0]

                    if sample["duration"] > 10.22:
                        print(sample)
                        discard_line_num += 1

                    song["annot"].append(sample)
                    duration.append(sample["duration"])

                    total_line_num += 1

                logging.debug("Successfully loaded {} songs".format(len(subset)))
            except:
                logging.warning("Error loading annotation for song {}".format(file))
                pass

    logging.debug("Scanning {} songs.".format(len(subset)))
    logging.debug("Total line num: {} Discarded line num: {}".format(total_line_num,  discard_line_num))

    return np.array(subset, dtype=object)

def get_dali_folds(database_path, level, lang="english", genre=None):
    dataset = getDALI(database_path, level, lang, genre)

    total_len = len(dataset)
    train_len = np.int(0.8 * total_len)

    train_list = np.random.choice(dataset, train_len, replace=False)
    val_list = [elem for elem in dataset if elem not in train_list]
    logging.debug("First training song: " + str(train_list[0]["id"]) + " " + str(len(train_list[0]["annot"])) + " lines")
    logging.debug("train_list {} songs val_list {} songs".format(len(train_list), len(val_list)))
    return {"train" : train_list, "val" : val_list}

def crop(mix, targets, shapes):
    '''
    Crops target audio to the output shape required by the model given in "shapes"
    '''
    for key in targets.keys():
        if key != "mix":
            targets[key] = targets[key][:, shapes["output_start_frame"]:shapes["output_end_frame"]]
    return mix, targets

def random_amplify(mix, targets, shapes, min, max):
    '''
    Data augmentation by randomly amplifying sources before adding them to form a new mixture
    :param mix: Original mixture
    :param targets: Source targets
    :param shapes: Shape dict from model
    :param min: Minimum possible amplification
    :param max: Maximum possible amplification
    :return: New data point as tuple (mix, targets)
    '''
    residual = mix  # start with original mix
    for key in targets.keys():
        if key != "mix":
            residual -= targets[key]  # subtract all instruments (output is zero if all instruments add to mix)
    mix = residual * np.random.uniform(min, max)  # also apply gain data augmentation to residual
    for key in targets.keys():
        if key != "mix":
            targets[key] = targets[key] * np.random.uniform(min, max)
            mix += targets[key]  # add instrument with gain data augmentation to mix
    mix = np.clip(mix, -1.0, 1.0)
    return crop(mix, targets, shapes)

class LyricsAlignDataset(Dataset):
    def __init__(self, dataset, partition, sr, shapes, hdf_dir, in_memory=False, dummy=False, pad_length=150):
        '''

        :param dataset:     a list of song with line level annotation
        :param sr:          sampling rate
        :param shapes:      dict, keys: "output_frames", "output_start_frame", "input_frames"
        :param hdf_dir:     hdf5 file
        :param in_memory:   load in memory or not
        :param dummy:       use a subset
        '''

        super(LyricsAlignDataset, self).__init__()

        self.hdf_dataset = None
        os.makedirs(hdf_dir, exist_ok=True)
        if dummy == False:
            self.hdf_file = os.path.join(hdf_dir, partition + ".hdf5")
        else:
            self.hdf_file = os.path.join(hdf_dir, partition + "_dummy.hdf5")

        self.sr = sr
        self.shapes = shapes
        self.hop = (shapes["output_frames"] // 2)
        self.in_memory = in_memory
        self.pad_length = pad_length

        # PREPARE HDF FILE

        # Check if HDF file exists already
        if not os.path.exists(self.hdf_file):
            # Create folder if it did not exist before
            if not os.path.exists(hdf_dir):
                os.makedirs(hdf_dir)

            # Create HDF file
            with h5py.File(self.hdf_file, "w") as f:
                f.attrs["sr"] = sr

                print("Adding audio files to dataset (preprocessing)...")
                for idx, example in enumerate(tqdm(dataset[partition])):
                    # Load song
                    y, _ = load(example["path"], sr=self.sr, mono=True)

                    # Add to HDF5 file
                    grp = f.create_group(str(idx))
                    grp.create_dataset("inputs", shape=y.shape, dtype=y.dtype, data=y)

                    grp.attrs["audio_name"] = example["id"]
                    grp.attrs["input_length"] = y.shape[1]

                    # word level annotation
                    annot_num = len(example["annot"])
                    lyrics = [sample["text"].encode() for sample in example["annot"]]
                    times = np.array([sample["time"] for sample in example["annot"]])

                    # note level annotation
                    notes = np.array(example["notes"])
                    note_num = len(notes)
                    pitches = np.array([note["freq"] for note in notes])
                    note_times = np.array([np.array([note['time'][0], note['time'][1]]) for note in notes])

                    grp.attrs["annot_num"] = annot_num
                    grp.attrs["note_num"] = note_num

                    # words and corresponding times
                    grp.create_dataset("lyrics", shape=(annot_num, 1), dtype='S100', data=lyrics)
                    grp.create_dataset("times", shape=(annot_num, 2), dtype=times.dtype, data=times)

                    # notes and corresponding times
                    grp.create_dataset("freqs", shape=(note_num, 1), dtype=np.short, data=pitches)
                    grp.create_dataset("note_times", shape=(note_num, 2), dtype=note_times.dtype, data=note_times)

        # In that case, check whether sr and channels are complying with the audio in the HDF file, otherwise raise error
        with h5py.File(self.hdf_file, "r", libver='latest', swmr=True) as f:
            if f.attrs["sr"] != sr:
                raise ValueError(
                    "Tried to load existing HDF file, but sampling rate is not as expected. Did you load an out-dated HDF file?")

        # HDF FILE READY

        # SET SAMPLING POSITIONS

        # Go through HDF and collect lengths of all audio files
        with h5py.File(self.hdf_file, "r") as f:
            # length of song
            lengths = [f[str(song_idx)].attrs["input_length"] for song_idx in range(len(f))]

            # Subtract input_size from lengths and divide by hop size to determine number of starting positions
            lengths = [( (l - self.shapes["output_frames"]) // self.hop) + 1 for l in lengths]

        self.start_pos = SortedList(np.cumsum(lengths))
        self.length = self.start_pos[-1]

        self.shuffled_buffer = np.arange(self.length)
        self.shuffle_data_list()

    def shuffle_data_list(self):
        np.random.shuffle(self.shuffled_buffer)

    def __getitem__(self, index):

        # Open HDF5
        if self.hdf_dataset is None:
            driver = "core" if self.in_memory else None  # Load HDF5 fully into memory if desired
            self.hdf_dataset = h5py.File(self.hdf_file, 'r', driver=driver)

        while True:
            # Loop until it finds a valid sample

            # Find out which slice of targets we want to read
            song_idx = self.start_pos.bisect_right(index)
            if song_idx > 0:
                index = index - self.start_pos[song_idx - 1]

            # Check length of audio signal
            audio_length = self.hdf_dataset[str(song_idx)].attrs["input_length"]
            annot_num = self.hdf_dataset[str(song_idx)].attrs["annot_num"]
            target_length = self.shapes["output_frames"]

            # Determine position where to start targets
            start_target_pos = index * self.hop
            end_target_pos = start_target_pos + self.shapes["output_frames"]

            # READ INPUTS
            # Check front padding
            start_pos = start_target_pos - self.shapes["output_start_frame"]
            if start_pos < 0:
                # Pad manually since audio signal was too short
                pad_front = abs(start_pos)
                start_pos = 0
            else:
                pad_front = 0

            # Check back padding
            end_pos = start_target_pos - self.shapes["output_start_frame"] + self.shapes["input_frames"]
            if end_pos > audio_length:
                # Pad manually since audio signal was too short
                pad_back = end_pos - audio_length
                end_pos = audio_length
            else:
                pad_back = 0

            # read audio and zero padding
            audio = self.hdf_dataset[str(song_idx)]["inputs"][:, start_pos:end_pos].astype(np.float32)
            if pad_front > 0 or pad_back > 0:
                audio = np.pad(audio, [(0, 0), (pad_front, pad_back)], mode="constant", constant_values=0.0)

            # find the lyrics within (start_target_pos, end_target_pos)
            words_start_end_pos = self.hdf_dataset[str(song_idx)]["times"][:]
            try:
                first_word_to_include = next(x for x, val in enumerate(list(words_start_end_pos[:, 0]))
                                             if val > start_target_pos/self.sr)
            except StopIteration:
                first_word_to_include = np.Inf

            try:
                last_word_to_include = annot_num - 1 - next(x for x, val in enumerate(reversed(list(words_start_end_pos[:, 1])))
                                             if val < end_target_pos/self.sr)
            except StopIteration:
                last_word_to_include = -np.Inf

            targets = ""
            if first_word_to_include - 1 == last_word_to_include + 1: # the word covers the whole window
                # invalid sample, skip
                targets = None
                index = np.random.randint(self.length)
                continue
            if first_word_to_include <= last_word_to_include: # the window covers word[first:last+1]
                lyrics = self.hdf_dataset[str(song_idx)]["lyrics"][first_word_to_include:last_word_to_include+1]
                lyrics_list = [s[0].decode() for s in list(lyrics)]
                targets = " ".join(lyrics_list)
                targets = " ".join(targets.split())

            if len(targets) > 120:
                index = np.random.randint(self.length)
                continue

            seq = self.text2seq(targets)
            break

        return audio, targets, seq

    def text2seq(self, text):
        seq = []
        for c in text.lower():
            idx = string.ascii_lowercase.find(c)
            if idx == -1:
                if c == "'":
                    idx = 26
                elif c == " ":
                    idx = 27
                else:
                    continue # remove unknown characters
            seq.append(idx)
        # if len(seq) == 0:
        #     seq.append(28) # insert epsilon for instrumental segments
        return np.array(seq)

    def __len__(self):
        return self.length

class JamendoLyricsDataset(Dataset):
    def __init__(self, sr, shapes, hdf_dir, dataset, jamendo_dir, in_memory=False):
        super(JamendoLyricsDataset, self).__init__()
        self.hdf_dataset = None
        os.makedirs(hdf_dir, exist_ok=True)
        self.hdf_file = os.path.join(hdf_dir, dataset + ".hdf5")

        self.sr = sr
        self.shapes = shapes
        self.hop = shapes["output_frames"]
        self.in_memory = in_memory

        audio_dir = os.path.join(jamendo_dir, 'mp3')
        lyrics_dir = os.path.join(jamendo_dir, 'lyrics')
        self.audio_list = [file for file in os.listdir(audio_dir) if file.endswith('.mp3')]

        # PREPARE HDF FILE

        # Check if HDF file exists already
        if not os.path.exists(self.hdf_file):
            # Create folder if it did not exist before
            if not os.path.exists(hdf_dir):
                os.makedirs(hdf_dir)

            # Create HDF file
            with h5py.File(self.hdf_file, "w") as f:
                f.attrs["sr"] = sr

                print("Adding audio files to dataset (preprocessing)...")
                for idx, audio_name in enumerate(tqdm(self.audio_list)):

                    # Load song
                    y, _ = load(os.path.join(audio_dir, audio_name), sr=self.sr, mono=True)

                    lyrics, words, idx_in_full = load_lyrics(os.path.join(lyrics_dir, audio_name[:-4]))
                    annot_num = len(words)

                    # Add to HDF5 file
                    grp = f.create_group(str(idx))
                    grp.create_dataset("inputs", shape=y.shape, dtype=y.dtype, data=y)

                    grp.attrs["input_length"] = y.shape[1]
                    grp.attrs["audio_name"] = audio_name[:-4]
                    print(len(lyrics))

                    grp.create_dataset("lyrics", shape=(1, 1), dtype='S3000', data=np.array([lyrics.encode()]))
                    grp.create_dataset("idx", shape=(annot_num, 2), dtype=np.int, data=idx_in_full)

        # In that case, check whether sr and channels are complying with the audio in the HDF file, otherwise raise error
        with h5py.File(self.hdf_file, "r", libver='latest', swmr=True) as f:
            if f.attrs["sr"] != sr:
                raise ValueError(
                    "Tried to load existing HDF file, but sampling rate is not as expected. Did you load an out-dated HDF file?")

        # HDF FILE READY

        # SET SAMPLING POSITIONS

        # Go through HDF and collect lengths of all audio files
        with h5py.File(self.hdf_file, "r") as f:
            # length of song
            lengths = [f[str(song_idx)].attrs["input_length"] for song_idx in range(len(f))]

            # Subtract input_size from lengths and divide by hop size to determine number of starting positions
            lengths = [np.int(np.ceil(l / self.hop)) for l in lengths]

        self.lengths = lengths
        self.length = len(lengths)

    def __getitem__(self, index):

        # Open HDF5
        if self.hdf_dataset is None:
            driver = "core" if self.in_memory else None  # Load HDF5 fully into memory if desired
            self.hdf_dataset = h5py.File(self.hdf_file, 'r', driver=driver)

        # select song: index
        # Check length of audio signal
        audio_length = self.hdf_dataset[str(index)].attrs["input_length"]

        # number of chunks for that song
        num_chunk = self.lengths[index]

        chunks = []

        for i in np.arange(num_chunk):
            # Determine position where to start targets
            start_target_pos = i * self.hop
            end_target_pos = start_target_pos + self.shapes["output_frames"]

            # READ INPUTS
            # Check front padding
            start_pos = start_target_pos - self.shapes["output_start_frame"]
            if start_pos < 0:
                # Pad manually since audio signal was too short
                pad_front = abs(start_pos)
                start_pos = 0
            else:
                pad_front = 0

            # Check back padding
            end_pos = start_target_pos - self.shapes["output_start_frame"] + self.shapes["input_frames"]
            if end_pos > audio_length:
                # Pad manually since audio signal was too short
                pad_back = end_pos - audio_length
                end_pos = audio_length
            else:
                pad_back = 0

            # read audio and zero padding
            audio = self.hdf_dataset[str(index)]["inputs"][:, start_pos:end_pos].astype(np.float32)
            audio_name = self.hdf_dataset[str(index)].attrs["audio_name"]
            lyrics = self.hdf_dataset[str(index)]["lyrics"][0, 0].decode()
            align_idx = self.hdf_dataset[str(index)]["idx"]
            if pad_front > 0 or pad_back > 0:
                audio = np.pad(audio, [(0, 0), (pad_front, pad_back)], mode="constant", constant_values=0.0)

            chunks.append(audio)

        return chunks, align_idx, (lyrics, audio_name, audio_length)

    def __len__(self):
        return self.length


import unittest
from app import create_app
from app.receipts_manager import ReceiptsManager

class TestGetReceiptPoints(unittest.TestCase):
    def setUp(self):
        # Create a test client for the Flask app
        self.app = create_app().test_client()

        # Create an instance of ReceiptsManager
        self.manager = ReceiptsManager()

        # Process a receipt to get a receipt_id for testing
        data = {
            "retailer": "Test Retailer",
            "purchaseDate": "2023-10-15",
            "purchaseTime": "15:30",
            "items": [
                {
                    "shortDescription": "Item 1",
                    "price": "10.99"
                },
                {
                    "shortDescription": "Item 2",
                    "price": "5.99"
                }
            ],
            "total": "16.98"
        }
        response = self.app.post('/receipts/process', json=data)
        json_data = response.get_json()
        self.receipt_id = json_data["receipt_id"]  # Capture the receipt_id

    def test_getting_points(self):
        # Use self.receipt_id in your test to retrieve receipt points
        response = self.app.get(f'/receipts/{self.receipt_id}/points')
        json_data = response.get_json()
        self.assertEqual(response.status_code, 200)
        self.assertIn("points", json_data)

    def tearDown(self):
        # Clean up by removing the test receipt after the test is done
        if self.receipt_id in self.manager.receipts:
            del self.manager.receipts[self.receipt_id]

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


import dateparser
from PIL import Image
from django.test import TestCase
from numpy import asarray
from cameratrap.OCRUtils import OCRUtils

class OCRUtilTests(TestCase):

    def test_ocr_bushnell(self):
        img = Image.open(r'./cameratrap/testdata/bushnell.jpg')
        img_as_numpy = asarray(img)
        ocr = OCRUtils(image=img_as_numpy)
        dt = ocr.extractDateTime()
        dt_in_image = dateparser.parse('2022-05-04 16:08:36+00:00')
        self.assertEqual(dt.date(), dt_in_image.date())
        self.assertEqual(dt.time(), dt_in_image.time())
        print(dt)

    def test_ocr_deer(self):
        img = Image.open(r'./cameratrap/testdata/deer.jpg')
        img_as_numpy = asarray(img)
        ocr = OCRUtils(image=img_as_numpy)
        dt = ocr.extractDateTime()
        dt_in_image = dateparser.parse('2022-03-04 07:18:00+00:00')
        self.assertEqual(dt.date(), dt_in_image.date())
        self.assertEqual(dt.time(), dt_in_image.time())
        print(dt)