import os
import os
import netCDF4 as nc4
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import imageio
import cartopy
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy import feature as cfeature
from cartopy.feature import NaturalEarthFeature, LAND, COASTLINE, OCEAN, LAKES, BORDERS
import matplotlib.ticker as mticker
import imageio
from IPython.display import Image

def map_var(ds,res):

    """
    This function transfers data from dataset into mappable variables

    Parameters:
    ===========
    ds  : netCDF4 dataset
    res : int - time resolution for animation

    Returns:
    ===========
    lon        : array - longitude values
    lat        : array - latitude values
    temp       : array - temperature values
    year       : list - years (repetition dependent on res)
    year_range : array - index value of years with (res) intervals
    """

    #dataset variables
    time = ds.variables['time']
    lat = ds.variables['lat'][:]
    lon = ds.variables['lon'][:]
    z = ds.variables['z'][:]
    temp_var = ds.variables['anom'] #(time, z, lat, lon)

    #sync time with temperature data and res
    years = nc4.num2date(time[:], time.units) #transferring time data from 'Days since 01-01-1800'
    years = np.array(years)
    #lists to reorganize year data
    y = []
    year = []
    #for loops to organize years into YYYY
    for i in range(len(years)):
        y.append(str(years[i]))
    for i in range(len(y)):
        year.append(y[i][:4]) #years in YYYY
    year_range = np.arange(0,len(year),res) #determines how many figures to create

    #cleaning temp_var from dataset
    t = np.array(temp_var)
    t = np.where(t > -900,t,np.nan) #removing artifacts of sentinal values

    #taking np.ptp (statistical range) of temperature values in original dataset format
    temp = [[[np.ptp(t[k:k+12,0,j,i])for i in range(len(lon))]for j in np.arange(0,len(lat),1)]for k in np.arange(0,len(year),12)]
    temp = np.array(temp)

    #values used in map2png
    return lon,lat,temp,year, year_range

def map2png(lon,lat,temp,year, year_range,**kwargs):
    """
    This function creates a map figure and saves a '.png' file in the 'Fig' directory.

    Parameters:
    ============
    lon        : 1d array - longitude values
    lat        : 1d array - latitude values
    temp       : 4d array - temperatures in shape (len(lat),len(lon))
    year       : 1d array - dates measured
    year_range : 1d array - date range by increments
    kwargs     : dictionary - projection types

    Returns:
    ============
    None

    """

    #creating new 'Fig' directory
    L = os.listdir("./")
    if 'Fig' not in L:
        os.mkdir('Fig')
    for m in L:
        if '.png' in m:
            os.remove('Fig/' + m)

    #transferring variables into mappable format for each frame (date spread decided by year_range)
    for i in range(len(year_range)):
        temps,years,year_ranges = temp[round(year_range[i]/12),:,:],year[year_range[i]],year_range[i]

        #plot
        plt.clf() # Clear the plot in case you call it multiple times
        plt.figure(1, (6, 6),dpi=900)

        #choosing projection type
        for key in kwargs:
            if kwargs[key] == 'LambertConformal':
                ax = plt.axes(projection=ccrs.LambertConformal(central_longitude=260.0, central_latitude=33.0))
            elif kwargs[key] == 'Orthographic':
                ax = plt.axes(projection=ccrs.Orthographic(-75, 42))
            elif kwargs[key] == 'Mollweide':
                ax = plt.axes(projection=ccrs.Mollweide(central_longitude=180.0))
            elif kwargs[key] == 'Mercator':
                ax = plt.axes(projection=ccrs.Mercator(central_longitude=180.0, min_latitude=-70.0, max_latitude=70.0, globe=None))
            else:
                ax = plt.axes(projection=ccrs.Orthographic(-75, 42)) #make Orthographic the default proj type
                kwargs[key] = 'Orthographic' #this is for naming files later
            p = kwargs[key] #projection type

        #formatting
        gl = ax.gridlines(crs=ccrs.PlateCarree(), color='black', linewidth=1, linestyle='dotted')
        gl.ylocator = mticker.FixedLocator(np.arange(-80, 81, 20))
        gl.xlocator = mticker.FixedLocator(np.arange(-180, 181, 60));
        gl.xformatter = LONGITUDE_FORMATTER
        gl.yformatter = LATITUDE_FORMATTER
        X, Y = np.meshgrid(lon, lat)
        levels = np.arange(0, 29, 1) # Define contour intervals
        #color plot
        m = ax.contourf(X, Y, temps,levels,\
                        transform=ccrs.PlateCarree(),
                        cmap=cm.jet)
        ax.coastlines()
        ax.set_global()

        cbar = plt.colorbar(m) # Put on a color bar of temperatures
        plt.title('Temperature Range - Year: '+ years + '\n($T_{Max} - T_{Min}$[˚C])', fontsize=12);
        plt.savefig('Fig/' + years +p+ str(year_ranges) + '.png') # Saves the figure to a file
        #file name example: 1880Mercator100.png

def animate_map(spf):

    """
    This function creates a map animation and saves a '.GIF' file in the 'Fig' directory.

    Parameters:
    ===========
    spf : float - seconds per frame

    Returns:
    ===========
    None
    """
    #recalling files from 'Fig' directory
    filenames = sorted(os.listdir('Fig/')) # Listing of the directory
    images = [] # Make a container to put the image files in
    for file in filenames:                          # Step through all the maps
        if '.png' in file:                          # Skip hidden files
            filename = 'Fig/' + file               # Make filename from the folder name and the file name
            images.append(imageio.imread(filename)) # Read it in and stuff in the container
    kwargs = {'duration': spf}                       # Use spf for second delay between frames
    imageio.mimsave('Fig/map_1.gif', images, 'GIF', **kwargs) # Save to an animated gif.

    return Image(filename="Fig/map_1.gif") #display GIF


"""
this python file handle the classify section
@author: Maayan Eliya
"""
# import the necessary packages
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np
import pickle
import os
from PIL import Image
import PIL.ImageOps
from matplotlib import image
from imutils import paths
import prints_types

IMAGE_SIZE = (28,28)

class ImagePredictor():

    def __init__(self, model_path, labels_path, image_path, sign, sorted_data_path):
        """
        Create the ImagePredictor class object.
        :param model_path: string
        :param labels_path: string
        :param image_path: string
        :param sign: boolian
        :param sorted_data_path: string
        :return: None
        """
        self.__model_path = model_path #a path to the trained model
        self.__labels_path = labels_path #a path to the updated labels
        self.__image_path = image_path #a path to an image that should be checked
        self.__sign = sign #True if in the image the letter is black and the background is white,
                           #or False if it the letter is white and the background is black(like in the dataset).
        self.__sorted_data_path = sorted_data_path #a path to the sorted dataset that contains all the letters with the labels
        self.__model = None #the model itself
        self.__lb = None #the label binzrizer

    def handle_classify(self):
        """
        this function manage the ImagePredictor (classify) class
        :param: None
        :return: None
        """
        self.__load_Model() #loads the trained model
        image_arr = self.__load_Image() #load the image as array
        self.__predict_Image(image_arr) #predict the image

    def __load_Model(self):
        """
        This function loads the trained model and the label binarizer
        :param: None
        :return: None
        """
        prints_types.printProcess("[INFO] Loading network...")
        self.__model = load_model(self.__model_path) #loads the trained model
        self.__lb = pickle.loads(open(self.__labels_path, "rb").read()) #load the label binzrizer

    def __load_Image(self):
        """
        this function load the image that should be checked - as an array
        :param: None
        :return: image_arr
        :rtype: numpy array
        it returns the image as a numpy array (after fitting the image - normalization (pixels scale), #colors, #dims,  )
        """
        # pre-process the image for classification
        image_arr = Image.open (self.__image_path)
        if self.__sign == True: #if it is a new image - black letter and white background
            image_arr = PIL.ImageOps.invert(image_arr) #make the letter white and the background black
            image_arr.save(self.__image_path) #save the inverted image
        else:
            image_arr = Image.open (self.__image_path) #if the sign is false then the image is shown on the screen - if it's true then it was already shown
            image_arr.show() #show the image on the screen
        image_arr = image_arr.convert('L') #make the image to be on grayscale
        image_arr.save(self.__image_path) #save the grayscaled image
        image_arr = image_arr.resize((IMAGE_SIZE)) #change the image's size to be (28,28)
        image_arr.save(self.__image_path) #save the image with the new size
        image_arr = image.imread(self.__image_path) #load the image
        image_arr = image_arr.astype("float") / 255.0 #normalization
        image_arr = img_to_array(image_arr) #convert the image to a numpy array
        image_arr = np.expand_dims(image_arr, axis=0) #expand the shape of the array
        return image_arr

    def __predict_Image(self,image_arr):
        """
        This function predicts the letter of the image that the user pass.
        It prints "correct" if the input image label is fit to the prediction label,
        else we will print "incorrect".
        It plots the image on the screen
        :param image_arr: numpy array
        :return: None
        """
        # classify the input image
        prints_types.printProcess("[INFO] Classifying image...")
        Predict = self.__model.predict(image_arr)[0] #output predictions for the input image
        idx = np.argmax(Predict) #the index of the max prediction value
        label = self.__lb.classes_[idx] #the label of the prediction
        #check if it predicted correctly
        split_pimg = os.path.split(self.__image_path) #split the path of image that should be predicted to head and tail
        filename = split_pimg[-1] #the name of image that should be predicted (the tail)
        imagePaths = sorted(list(paths.list_images(self.__sorted_data_path))) #list of all the images paths
        flag = False
        for imagePath in imagePaths:
           split = os.path.split(imagePath) #split the path of the image that is currently checked to head and tail
           imgname = split[-1] #the name of the image that is currently checked (the tail)
           if filename == imgname: # if the name of the image that should be predicted equals to the name of the image that is currently checked
               flag = True #it found the image. this is an image from the dataset
               split = os.path.split(split[0])
               imglabel = split[-1] #the actuall label of the image
               if label == imglabel: #if the label that it predicted equals to the actuall label of the image
                   correct = "correct"
               else:
                    correct = "incorrect"
               break
        if flag == False: #if it is a new image
            #check if it predicted correctly
            imglabel = filename.split(".")
            imglabel = imglabel[0] #imglabel = the new label of the image (the letter)
            if imglabel == label: #if the label that it predicted equals to the actuall label of the image
                correct = "correct"
            else:
                correct = "incorrect"
        # print the results
        result = "the letter it predicted: {}, prediction: {:.2f}%,  \nthe real label of the image letter: {} \n({})".format(label, Predict[idx] * 100, imglabel, correct)
        prints_types.printProcess("[INFO] {}".format(result))


from django.shortcuts import render, redirect, get_object_or_404, HttpResponse
from django.contrib import messages
from django.middleware.csrf import rotate_token

def general_report_form(request):
    from mobile.forms import GeneralReportForm

    if request.method == 'POST':
        form = GeneralReportForm(request.POST)

        if form.is_valid():
            month = form.cleaned_data['month']
            year = form.cleaned_data['year']

            return redirect(to='mobile:general_report', month=month, year=year)
        else:
            context = {'form': form}
            return render(request, 'mobile/report/general_form.html', context)
    else:
        form = GeneralReportForm()
        context = {'form': form}
        return render(request, 'mobile/report/general_form.html', context)

def walk_deps_tree(lst, res=[], root=None, level=0, top_parent_id=None):
    for item in lst:
        if item.parent_department_id == root:
            item.level = level
            item.levels = range(level)
            if level == 0:
                item.top_parent_id = item.id
            else:
                item.top_parent_id = top_parent_id
            res.append(item)
            walk_deps_tree(lst, res, root=item.id, level=level + 1, top_parent_id=item.top_parent_id)
    return res

def general_report(request, year, month):
    from main.models import Department, Employee
    from mobile.models import Bill, Sim
    from django.db import connection

    deps = walk_deps_tree(Department.objects.all().order_by('division', 'id'), [])

    emps = dict()
    for emp in Employee.objects.all():
        emps[emp.id] = {
            'name': emp.short_name(),
            'dep': emp.department_id
        }

    bills = Bill.objects.with_sim_details(year, month)

    table = []
    for dep in deps:
        for bill in bills:
            emp = emps[bill.sim_owner_id]
            if emp['dep'] == dep.id:
                row = {
                    'dep_id': dep.id,
                    'dep_name': dep.name,
                    'dep_level': dep.level,
                    'dep_levels': dep.levels,
                    'top_parent_id': dep.top_parent_id,
                    'emp': emp['name'],
                    'number': bill.number,
                    'contract': bill.contract_name,
                    'amount': bill.amount,
                    'amount_nonds': bill.amount / 1.18,
                    'totals': 0
                }
                if bill.sim_limit_infinite:
                    row['limit'] = 'Безлимит'
                    row['economy'] = 0
                    row['exceed'] = 0
                else:
                    row['limit'] = bill.sim_limit
                    if bill.sim_limit >= bill.amount:
                        row['economy'] = bill.sim_limit - bill.amount
                        row['exceed'] = 0
                    else:
                        row['economy'] = 0
                        row['exceed'] = bill.amount - bill.sim_limit
                table.append(row)

    dep_totals = []
    for dep in deps:
        row = {'totals': 1}
        row['dep_id'] = dep.id
        row['dep_name'] = dep.name
        row['dep_level'] = dep.level
        row['dep_levels'] = dep.levels
        row['dep_limit'] = sum([x['limit'] for x in table if x['dep_id'] == dep.id and x['limit'] != 'Безлимит'])
        row['dep_amount'] = sum([x['amount'] for x in table if x['dep_id'] == dep.id])
        row['dep_amount_nonds'] = row['dep_amount'] / 1.18
        row['dep_economy'] = sum([x['economy'] for x in table if x['dep_id'] == dep.id])
        row['dep_exceed'] = sum([x['exceed'] for x in table if x['dep_id'] == dep.id])

        if dep.level == 0:
            row['top_dep_limit'] = sum([x['limit'] for x in table if x['top_parent_id'] == dep.id and x['limit'] != 'Безлимит'])
            row['top_dep_amount'] = sum([x['amount'] for x in table if x['top_parent_id'] == dep.id])
            row['top_dep_amount_nonds'] = row['top_dep_amount'] / 1.18
            row['top_dep_economy'] = sum([x['economy'] for x in table if x['top_parent_id'] == dep.id])
            row['top_dep_exceed'] = sum([x['exceed'] for x in table if x['top_parent_id'] == dep.id])

        dep_totals.append(row)

    i = 0
    dep_id = None
    while i < len(table):
        if table[i]['dep_id'] != dep_id:
            dep_id = table[i]['dep_id']
            for row in dep_totals:
                if row['dep_id'] == dep_id:
                    table.insert(i, row)
                    i += 1
            if dep.level == 0:
                pass

    context = {
        'year': year,
        'month': month,
        'table': table
    }

    msg = 'Queries: ' + str(len(connection.queries)) + '\n' + 'Time: ' + str(
        round(sum([float(x['time']) for x in connection.queries]), 3)) + ' seconds'
    messages.debug(request, msg)
    return render(request, 'mobile/report/general.html', context)


from .main_panel import ALINKER_PT_MainPanel

classes = (
    ALINKER_PT_MainPanel,
)

def register_panels():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

def unregister_panels():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)


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

# In[1]:

import pyspark

# In[2]:

from pyspark import SparkContext, SparkConf
from pyspark.sql import SparkSession
from pyspark.sql.functions import *
conf = pyspark.SparkConf().setAppName('appName').setMaster('local')
sc = pyspark.SparkContext(conf=conf)
spark = SparkSession(sc)

# In[3]:

#read the whole dataset
flight_data = spark.read.option("inferSchema", "true").option("header", "true").csv("671009038_T_ONTIME_REPORTING.csv")
flight_data.count()

# In[4]:

flight_data.show(3)

# In[5]:

flight_data.createOrReplaceTempView("flight_data")

# In[23]:

#convert the NULL values with 0 number
flight_data = spark.sql("""
SELECT *, ifnull(DEP_DELAY, '0') as DEP_DELAY1, ifnull(ARR_DELAY, '0') as ARR_DELAY1
FROM flight_data
""")
flight_data.show(3)

# In[24]:

#drop the previous columns referring to the delays
flight_data = flight_data.drop("DEP_DELAY", "ARR_DELAY")
flight_data.createOrReplaceTempView("flight_data")

# In[25]:

flight_data.show(3)

# In[26]:

#create the dataset of the airports (in the DEST column the airports were
#the same as in the origin column)
flights = spark.sql("""
SELECT ORIGIN AS airports, count(ORIGIN) as flight_numbers
FROM flight_data
GROUP BY ORIGIN
HAVING count(ORIGIN) > (SELECT count(ORIGIN)*0.01 FROM flight_data)
""")
flights.show() #the number of flights for each airport

# In[27]:

print(flights.count()) #the size of the unique airports

# In[28]:

#create the dataset of the airways
airways = spark.sql("""
SELECT CARRIER AS airways, count(CARRIER) as flight_numbers
FROM flight_data
GROUP BY CARRIER
HAVING count(CARRIER) > (SELECT count(CARRIER)*0.01 FROM flight_data)
""")
airways.show()

# In[29]:

print(airways.count()) #the size of the unique airways

# In[30]:

#make those datasets views in order to use them in the below queries
flights.createOrReplaceTempView("flights")
airways.createOrReplaceTempView("airways")

# In[31]:

#Report 1
#we want the avg for the departures delays for every airport (origin)
avg_airports = spark.sql("""
SELECT fd.ORIGIN as Airports, round(avg(fd.DEP_DELAY1), 3) as Departures_Delay
FROM flight_data as fd, flights as f
WHERE fd.ORIGIN = f.airports
GROUP BY fd.ORIGIN
ORDER BY avg(fd.DEP_DELAY1) DESC""")
avg_airports.show()

# In[32]:

avg_airports.toPandas().to_csv('task2-ap-avg.csv', header=False)

# In[33]:

#Report 2
#place an iterator
c_airports = spark.sql("""
SELECT ROW_NUMBER() OVER(PARTITION BY ORIGIN
                            ORDER BY DEP_DELAY1 ASC) AS NUM_ROW,
        ORIGIN, DEP_DELAY1 AS DEP_DELAY
FROM flight_data, flights as f
WHERE ORIGIN = f.airports
""")
c_airports.show()

# In[34]:

#save the length of the airport's flights
count_airports = spark.sql("""
SELECT ORIGIN, count(ORIGIN) as COR
FROM flight_data, flights as f
WHERE ORIGIN = f.airports
GROUP BY ORIGIN
""")
count_airports.show()

# In[35]:

count_airports.createOrReplaceTempView("count_airports")

# In[36]:

#find the lines where the medians are
count_airports = spark.sql("""
SELECT ORIGIN, COR, CAST(IF(MOD(COR, 2)=0, COR/2, (COR+1)/2) AS int) AS P,
        CAST(IF(MOD(COR, 2)=0, COR/2+1, 0) AS int) AS P1
FROM count_airports
""")
count_airports.show()

# In[37]:

c_airports.createOrReplaceTempView("c_airports")
count_airports.createOrReplaceTempView("count_airports")

# In[38]:

#match to each P value the recording row of the delay
p_set = spark.sql("""
SELECT air.ORIGIN as AIRPORT, DEP_DELAY as DELAY1
FROM c_airports as air, count_airports as c
WHERE air.ORIGIN = c.ORIGIN AND NUM_ROW = P
""")
p_set.show()

# In[39]:

#match to each P1 value the recording row of the delay
p1_set = spark.sql("""
SELECT air.ORIGIN as AIRPORT, DEP_DELAY as DELAY2
FROM c_airports as air, count_airports as c
WHERE air.ORIGIN = c.ORIGIN AND NUM_ROW = P1
""")
p1_set.show()

# In[40]:

p_set.createOrReplaceTempView("p_set")
p1_set.createOrReplaceTempView("p1_set")

# In[41]:

#join the above tables to a final one
med_air = spark.sql("""
SELECT p_set.AIRPORT, DELAY1, DELAY2
FROM p_set
LEFT JOIN p1_set
ON p_set.AIRPORT = p1_set.AIRPORT
""")
med_air.show()

# In[42]:

med_air.createOrReplaceTempView("med_air")

# In[43]:

#find the median and sort it
med_air = spark.sql("""
SELECT AIRPORT, IF((DELAY2 IS NULL), DELAY1, (DELAY1 + DELAY2)/2) AS MEDIAN
FROM med_air
ORDER BY MEDIAN DESC
""")
med_air.show()

# In[44]:

med_air.toPandas().to_csv('task2-ap-med.csv', header=False)

# In[45]:

#Report 3
#we want the avg for the departures delays for every airway (carrier)
avg_airways = spark.sql("""
SELECT fd.CARRIER as Airways, round(avg(fd.DEP_DELAY1), 3) as Departures_Delay
FROM flight_data as fd, airways as a
WHERE fd.CARRIER = a.airways
GROUP BY fd.CARRIER
ORDER BY avg(fd.DEP_DELAY1) DESC""")
avg_airways.show()

# In[46]:

avg_airways.toPandas().to_csv('task2-aw-avg.csv', header=False)

# In[47]:

#Report 4
#The same steps as the "Report 2"
c_airways = spark.sql("""
SELECT ROW_NUMBER() OVER(PARTITION BY CARRIER
                            ORDER BY DEP_DELAY1 ASC) AS NUM_ROW,
        CARRIER, DEP_DELAY1 AS DEP_DELAY
FROM flight_data, airways as a
WHERE CARRIER = a.airways
""")
c_airways.show()

# In[49]:

count_airways = spark.sql("""
SELECT CARRIER, count(CARRIER) as CAR
FROM flight_data, airways as a
WHERE CARRIER = a.airways
GROUP BY CARRIER
""")
count_airways.show()

# In[50]:

count_airways.createOrReplaceTempView("count_airways")

# In[51]:

count_airways = spark.sql("""
SELECT CARRIER, CAR, CAST(IF(MOD(CAR, 2)=0, CAR/2, (CAR+1)/2) AS int) AS P,
        CAST(IF(MOD(CAR, 2)=0, CAR/2+1, 0) AS int) AS P1
FROM count_airways
""")
count_airways.show()

# In[52]:

c_airways.createOrReplaceTempView("c_airways")
count_airways.createOrReplaceTempView("count_airways")

# In[53]:

p_set = spark.sql("""
SELECT air.CARRIER as AIRWAY, DEP_DELAY as DELAY1
FROM c_airways as air, count_airways as c
WHERE air.CARRIER = c.CARRIER AND NUM_ROW = P
""")
p_set.show()

# In[54]:

p1_set = spark.sql("""
SELECT air.CARRIER as AIRWAY, DEP_DELAY as DELAY2
FROM c_airways as air, count_airways as c
WHERE air.CARRIER = c.CARRIER AND NUM_ROW = P1
""")
p1_set.show()

# In[55]:

p_set.createOrReplaceTempView("p_set")
p1_set.createOrReplaceTempView("p1_set")

# In[56]:

med_air = spark.sql("""
SELECT p_set.AIRWAY, DELAY1, DELAY2
FROM p_set
LEFT JOIN p1_set
ON p_set.AIRWAY = p1_set.AIRWAY
""")
med_air.show()

# In[57]:

med_air.createOrReplaceTempView("med_air")

# In[ ]:

med_air = spark.sql("""
SELECT AIRWAY, IF((DELAY2 IS NULL), DELAY1, (DELAY1 + DELAY2)/2) AS MEDIAN
FROM med_air
ORDER BY MEDIAN DESC
""")
med_air.show()

# In[ ]:

med_air.toPandas().to_csv('task2-aw-med.csv', header=False)

# In[ ]:

sc.stop()


import setting as s
import math
import os
import glob
from mutagen.mp3 import MP3
from line.messenger import push_message
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials

# GoogleDrive共有フォルダID
MUSIC_FOLDER_ID = s.MUSIC_FOLDER_ID
VIDEO_FOLDER_ID = s.VIDEO_FOLDER_ID

# コンテンツ
def uploader(tag,data,dir,lineapi,lineid):

    scope = "https://www.googleapis.com/auth/drive"
    json_file = glob.glob("credentials/*.json")[0]
    gauth = GoogleAuth()
    gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scope)
    drive = GoogleDrive(gauth)

    video_ext_dicts = {
        '.mp4':'video/mp4',
        '.webm':'video/webm',
        '.mkv':'video/x-matroska'
    }
    title = data.lstrip(dir)
    if tag == "/mp3":
        f = drive.CreateFile(
            {
            'title': title,
            'mimeType': 'audio/mpeg',
            'parents':
                [{'kind': 'drive#fileLink', 'id':MUSIC_FOLDER_ID}]
            }
        )
        file_length = MP3(data).info.length
        dur = math.floor(file_length * 1000)
    else:
        dur = None
        _, ext = os.path.splitext(data)
        mimeType = video_ext_dicts.get(ext)

        f = drive.CreateFile(
            {
            'title': title,
            'mimeType': mimeType,
            'parents':
                [{'kind': 'drive#fileLink', 'id':VIDEO_FOLDER_ID}]
            }
        )

    f.SetContentFile(dir + f['title'])
    # GoogleDriveにアップロード
    f.Upload()

    # GoogleDriveのファイルIDを取得
    file_id = drive.ListFile(
        {'q': 'title =\"' + title +  '\"'}
    ).GetList()[0]['id']

    link = f"https://drive.google.com/uc?export=view&id={file_id}"
    push_message(link,lineid,tag,title,dur,lineapi)
    os.remove(data)


from util import *
import matplotlib.pyplot as plt

page_path= 'dataset/PALEOGRAPHY_TRAIN_AND_TEST_v3/TEST/orientalsquare/0129_FL12951770.jpg'
patches = sample_patches_from_page(page_path, number_of_patches=5)

for patch in patches:
    plt.imshow(patch)
    plt.show()


import numpy as np
from shapely.geometry import Point

def possibly_intersecting(dataframebounds, geometry, buffer=0):
    """
    Efficiently determine possibly intersecting geometries using a bounding box.

    Finding intersecting profiles for each branch is a slow process in case of large datasets
    To speed this up, we first determine which profile intersect a square box around the branch
    With the selection, the intersecting profiles can be determines much faster.

    Parameters
    ----------
    dataframebounds : numpy.array
    geometry : shapely.geometry.Polygon
    """

    geobounds = geometry.bounds
    idx = (
        (dataframebounds[0] - buffer < geobounds[2])
        & (dataframebounds[2] + buffer > geobounds[0])
        & (dataframebounds[1] - buffer < geobounds[3])
        & (dataframebounds[3] + buffer > geobounds[1])
    )
    # Get intersecting profiles
    return idx

def find_nearest_branch(branches, geometries, method="overall", maxdist=5):
    """
    Determine nearest branch for each geometry.

    The nearest branch can be found by finding t from both ends (ends) or the nearest branch from the geometry
    as a whole (overall), the centroid (centroid), or intersecting (intersect).

    Parameters
    ----------
    branches : geopandas.GeoDataFrame
        Geodataframe with branches
    geometries : geopandas.GeoDataFrame
        Geodataframe with geometries to snap
    method='overall' : str
        Method for determine branch
    maxdist=5 : int or float
        Maximum distance for finding nearest geometry
    minoffset : int or float
        Minimum offset from the end of the corresponding branch in case of method=equal
    """
    # Check if method is in allowed methods
    allowed_methods = ["intersecting", "overall", "centroid", "ends"]
    if method not in allowed_methods:
        raise NotImplementedError(f'Method "{method}" not implemented.')

    # Add columns if not present
    if "branch_id" not in geometries.columns:
        geometries["branch_id"] = ""
    if "branch_offset" not in geometries.columns:
        geometries["branch_offset"] = np.nan

    if method == "intersecting":
        # Determine intersection geometries per branch
        geobounds = geometries.bounds.to_numpy().T
        for branch in branches.itertuples():
            selection = geometries.loc[
                possibly_intersecting(geobounds, branch.geometry)
            ].copy()
            intersecting = selection.loc[
                selection.intersects(branch.geometry).to_numpy()
            ]

            # For each geometry, determine offset along branch
            for geometry in intersecting.itertuples():
                # Determine distance of profile line along branch
                geometries.loc[geometry.Index, "branch_id"] = branch.Index

                # Calculate offset
                branchgeo = branch.geometry
                mindist = min(0.1, branchgeo.length / 2.0)
                offset = round(
                    branchgeo.project(
                        branchgeo.intersection(geometry.geometry).centroid
                    ),
                    3,
                )
                offset = max(mindist, min(branchgeo.length - mindist, offset))
                geometries.loc[geometry.Index, "branch_offset"] = offset

    else:
        branch_bounds = branches.bounds.to_numpy().T
        # In case of looking for the nearest, it is easier to iteratie over the geometries instead of the branches
        for geometry in geometries.itertuples():
            # Find near branches
            nearidx = possibly_intersecting(
                branch_bounds, geometry.geometry, buffer=maxdist
            )
            selection = branches.loc[nearidx]

            if method == "overall":
                # Determine distances to branches
                dist = selection.distance(geometry.geometry)
            elif method == "centroid":
                # Determine distances to branches
                dist = selection.distance(geometry.geometry.centroid)
            elif method == "ends":
                # Since a culvert can cross a channel, it is
                crds = geometry.geometry.coords[:]
                dist = (
                    selection["geometry"]
                    .apply(
                        lambda x: max(
                            x.distance(Point(*crds[0])), x.distance(Point(*crds[-1]))
                        )
                    )
                    .astype(float)
                )
                # dist = (
                #     selection.distance(Point(*crds[0]))
                #     + selection.distance(Point(*crds[-1]))
                # ) * 0.5

            # Determine nearest
            if dist.min() < maxdist:
                branchidxmin = dist.idxmin()
                geometries.loc[geometry.Index, "branch_id"] = dist.idxmin()
                if isinstance(geometry.geometry, Point):
                    geo = geometry.geometry
                else:
                    geo = geometry.geometry.centroid

                # Calculate offset
                branchgeo = branches.loc[branchidxmin, "geometry"]
                mindist = min(0.1, branchgeo.length / 2.0)
                offset = max(
                    mindist,
                    min(branchgeo.length - mindist, round(branchgeo.project(geo), 3)),
                )
                geometries.loc[geometry.Index, "branch_offset"] = offset


# coding=<utf8>

import serial
import time

ser = serial.Serial("/dev/ttyAMA0", 9600)

Payload = {'iTOW' : 0,
               'lon' : 0,
               'lat' : 0,
               'height' : 0,
               'hMSL' : 0,
               'hAcc' : 0,
               'vAcc' : 0}
CK_A = 0
CK_B = 0
UBX_NAV_VELNED = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x12,
               'Length' : 36,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

UBX_NAV_SOL = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x06,
               'Length' : 52,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

numCh = 0
UBX_NAV_SVINFO = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x30,
               'Length' : 8 + 12 * numCh,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

UBX_NAV_STATUS = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x03,
               'Length' : 16,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

UBX_NAV_POSLLH = {'Header' : [0xB5, 0x62],
               'Class' : 0x01,
               'ID' : 0x02,
               'Length' : 28,
               'Payload' : Payload,
               'Checksum':[CK_A, CK_B]}

def getData():
    count = ser.inWaiting()
    if count != 0:
        data = ser.read(911)  ####################################################################各帧不按顺序发送？
        if parseData(data) == 0:
            print("parseData failed!")
    else:
        data = getData()
    return data

def parseData(data):
    data = map(ord, data)
    indexs = []
    i = 0
    flag = 0
    start = 0
    end = 0
    for byte in data:
        if byte == 0xB5:
            indexs.append(i)
        i += 1
    if indexs is None:
        return 0
    for index in indexs:
        if index + 24 < len(data):
            if data[index + 1] == 0x62:
                # find start frame and end frame
                if data[index + 3] == 0x12:
                    if flag == 0:
                        flag += 1
                        start = index
                    elif flag == 1:
                        end = index
                        flag += 1

    j = 0
    # get 5 complete frames
    for index in indexs:
        if index < start or index >= end:
            indexs.pop(j)
        j += 1

    for index in indexs:
        if index + 24 > len(data):
            return 0
        if data[index + 3] == 0x12:
            print("UBX_NAV_VELNED")
        elif data[index + 3] == 0x06:
            print("UBX_NAV_SOL")
        elif data[index + 3] == 0x30:
            print("UBX_NAV_SVINFO")
        elif data[index + 3] == 0x03:
            print("UBX_NAV_STATUS")
        elif data[index + 3] == 0x02:
            print("UBX_NAV_POSLLH")
            UBX_NAV_POSLLH['Payload']['lon'] = ((data[index + 8] << 24)
                                                + (data[index + 7] << 16)
                                                + (data[index + 6] << 8)
                                                + (data[index + 5])) / 10000000 * 3600
            UBX_NAV_POSLLH['Payload']['lat'] = ((data[index + 12] << 24)
                                                + (data[index + 11] << 16)
                                                + (data[index + 10] << 8)
                                                + (data[index + 9])) / 10000000 * 3600
    return 1

def getGPS():
    while True:
        getData()
        print("lon={},lat={}".format(UBX_NAV_POSLLH['Payload']['lon'], UBX_NAV_POSLLH['Payload']['lat']))
        time.sleep(0.1)

if __name__ == '__main__':
    try:
        getGPS()
    except KeyboardInterrupt:
        if ser != None:
            ser.close()


import logging
import os
from typing import List
from pydantic import BaseModel

import requests
import uvicorn
from dotenv import load_dotenv
from fastapi import BackgroundTasks, FastAPI, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

from utils import chroma, openai, s3
from utils.types import CompletionObj, DeleteObj, NoteObj, PromptTooLongError, UploadObj

load_dotenv()

app = FastAPI()

def upload_notes(notes: List[NoteObj]):
    print("Start: uploading notes", len(notes))
    for note in notes:
        qa = chroma.tokenize(note)
        openai.summarize(qa, note)
    print("End: uploading notes")

@app.post("/upload")
async def upload(data: UploadObj, background_tasks: BackgroundTasks):
    background_tasks.add_task(upload_notes, data.notes)
    return {
        "success": True
    }

@app.post("/completion")
def completion(data: CompletionObj):
    context = []
    try:
        assistantMsg, context = openai.get_completion_q_a(data)
    except PromptTooLongError as e:
        print("Prompt too long", data.classId, data.noteId)
        assistantMsg = e.assistantMsg
    except Exception as e:
        print("Error in completion", data.classId, data.noteId, e)
        assistantMsg = "Sorry, but I've encountered rate limits from OpenAI. Please try again in a few seconds."
    print(context)
    return {
        "assistantMsg": assistantMsg,
        "context": context
    }

@app.post("/collection/delete")
def deleteCollection(data: DeleteObj):
    print("Start: deleting collection", data.classId)
    try:
        requests.post(
            f"{os.getenv('CHROMA_URL')}/delete_collection",
            json={
                "classId": data.classId,
                "noteId": data.noteId
            }
        )
        s3.delete_obj(data.classId, data.noteId)
        print("End: deleting collection", data.classId)
        return {
            "success": True
        }
    except Exception as e:
        print("Error in deleteCollection", data.classId, data.noteId, e)
        return {
            "success": False,
            "message": e
        }

@app.get("/")
def hello():
    return {
        "success": True
    }

class CompletionApp(BaseModel):
    prompt: str

@app.post("/completion/app")
def completion_app(data: CompletionApp):
    return {
        "assistantMsg": openai.get_completion_summary_for_app(data.prompt)
    }

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
	exc_str = f'{exc}'.replace('\n', ' ').replace('   ', ' ')
	logging.error(f"{request}: {exc_str}")
	content = {'status_code': 10422, 'message': exc_str, 'data': None}
	return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)

if os.getenv("RAILWAY_ENVIRONMENT") == "production":
    origins = ["https://www.jini.gg",]
else:
    origins = ["http://localhost:3000"]

app.add_middleware(
    CORSMiddleware,
    allow_credentials=True,
    allow_origins=origins,
    allow_methods=["*"],
    allow_headers=["*"],
)

if __name__ == "__main__":
    if os.getenv("RAILWAY_ENVIRONMENT") == "production":
        uvicorn.run("main:app", host="0.0.0.0",
                     port=int(os.getenv("PORT", 8000)), log_level="info", workers=4)
    else:
        uvicorn.run("main:app", host="0.0.0.0", port=8002,
        log_level="info", reload=True)


# pylint: disable=arguments-differ
# pylint: disable=too-few-public-methods
# pylint: disable=invalid-name
"""CoreX Objects

The unit of the request body when sending corex apis. Support turning
each CoreX Object to json format.

"""
import json

from decanter.core.extra.decorators import corex_obj

class ComplexEncoder(json.JSONEncoder):
    """Extending JSONEncoder

    Define own JSON Encoder for the object having the function `jsonable`. It will
    return Dictionary object is having `jsonable`.

    Returns:
        function: `jsonable`
    """

    def default(self, obj):
        if hasattr(obj, "jsonable"):
            return obj.jsonable()
        # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)

class CoreBodyObj:
    """Base class of all CoreX Object.

    Has the jsonable function to return the Dictionary of object.

    """

    def __init__(self, **kwargs):
        """Add the argument pass in as attributes."""
        self.__dict__.update((k, v) for k, v in kwargs.items() if v is not None)

    def jsonable(self):
        """Reutrn the Dictionary of Object"""
        return self.__dict__

class CVTrain(CoreBodyObj):
    """Specification for the train fold"""

    @classmethod
    @corex_obj(required={"start", "end"})
    def create(cls, **kwargs):
        """Return CVTrain object with passed kwargs as attributes"""
        return cls(**kwargs)

class CVObject(CoreBodyObj):
    """The specifications for the cross validation folds"""

    @classmethod
    @corex_obj(required={"cvTrain", "cvTest"})
    def create(cls, **kwargs):
        """Return CVObject object with passed kwargs as attributes"""
        return cls(**kwargs)

class Column(CoreBodyObj):
    """The column names of the data"""

    @classmethod
    @corex_obj(required={"id", "data_type"})
    def create(cls, **kwargs):
        """Return Column object with passed kwargs as attributes"""
        return cls(**kwargs)

class TimeSeriesSplit(CoreBodyObj):
    """The specifications for the folds in time series cross validation.
    Cannot be used if nfold is specified"""

    @classmethod
    @corex_obj(required={"split_by", "cv", "train"})
    def create(cls, **kwargs):
        """Return TimeSeriesSplit object with passed kwargs as attributes"""
        return cls(**kwargs)

class TrainBody(CoreBodyObj):
    """The specifications for the folds in time series cross validation.
    Cannot be used if nfold is specified"""

    @classmethod
    @corex_obj(required={"target", "train_data_id", "algos"})
    def create(cls, **kwargs):
        """Return TrainBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class ClusterTrainBody(CoreBodyObj):
    """ClusterTrainBody"""

    @classmethod
    @corex_obj(required={"train_data_id"})
    def create(cls, **kwargs):
        """Return ClusterTrainBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class ModelBuildControl(CoreBodyObj):
    """Model build control"""

    @classmethod
    @corex_obj(required=None)
    def create(cls, **kwargs):
        """Return ModelBuildControl object with passed kwargs as attributes"""
        return cls(**kwargs)

class GeneticAlgorithmParams(CoreBodyObj):
    """The Genetic Algorithm parameters to be used in Auto Time Series"""

    @classmethod
    @corex_obj(required=None)
    def create(cls, **kwargs):
        """Return GeneticAlgorithmParams object with passed kwargs as
        attributes.

        Dictionary at_least specifies that the arguments passed in must
        includes at least one of the feature in the Dictionary.

        """
        at_least = {
            "max_iteration",
            "generation_size",
            "mutation_rate",
            "crossover_rate",
        }
        if all(kwargs[x] is None for x in at_least):
            return None
        return cls(**kwargs)

class BuildControl(CoreBodyObj):
    """Model build control."""

    @classmethod
    @corex_obj(required={"genetic_algorithm"})
    def create(cls, **kwargs):
        """Return BuildControl object with passed kwargs as attributes"""
        return cls(**kwargs)

class ModelSpec(CoreBodyObj):
    """The model specification"""

    @classmethod
    @corex_obj(required={"endogenous_features"})
    def create(cls, **kwargs):
        """Return ModelSpec object with passed kwargs as attributes"""
        return cls(**kwargs)

class TSGroupBy(CoreBodyObj):
    """Time series group by."""

    @classmethod
    @corex_obj(required=None)
    def create(cls, **kwargs):
        """Return TSGroupBy object with passed kwargs as attributes"""
        return cls(**kwargs)

class InputSpec(CoreBodyObj):
    """The input specification"""

    @classmethod
    @corex_obj(
        required={
            "train_data_id",
            "target",
            "datetime_column",
            "forecast_horizon",
            "gap",
        }
    )
    def create(cls, **kwargs):
        """Return InputSpec object with passed kwargs as attributes"""
        return cls(**kwargs)

class BuildSpec(CoreBodyObj):
    """Attribute for TrainAutoTSBody."""

    @classmethod
    @corex_obj(required={"genetic_algorithm"})
    def create(cls, **kwargs):
        """Return BuildSpec object with passed kwargs as attributes"""
        return cls(**kwargs)

class TrainAutoTSBody(CoreBodyObj):
    """Body for auto time series train api."""

    @classmethod
    @corex_obj(required={"build_spec", "input_spec"})
    def create(cls, **kwargs):
        """Return TrainAutoTSBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class PredictBody(CoreBodyObj):
    """Body for predict api."""

    @classmethod
    @corex_obj(required={"data_id", "model_id"})
    def create(cls, **kwargs):
        """Return PredictBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class PredictBodyTSModel(CoreBodyObj):
    """Body for predict time series forecast model api."""

    @classmethod
    @corex_obj(required={"data_id", "model_id"})
    def create(cls, **kwargs):
        """Return PredictBodyTSModel object with passed kwargs as attributes"""
        return cls(**kwargs)

class SetupBody(CoreBodyObj):
    """Body for setup api."""

    @classmethod
    @corex_obj(required={"data_source", "data_columns"})
    def create(cls, **kwargs):
        """Return SetupBody object with passed kwargs as attributes"""
        return cls(**kwargs)

class Accessor(CoreBodyObj):
    """Data accessor"""

    @classmethod
    @corex_obj(required={"uri", "format"})
    def create(cls, **kwargs):
        """Return SetupBody object with passed kwargs as attributes"""
        return cls(**kwargs)

def column_array(cols):
    """Turn Josn columns to list of Column objects."""
    if cols is None:
        return None
    result = []
    for col in cols:
        nullable = col.get("nullable", None)
        try:
            result.append(
                Column(id=col["id"], data_type=col["data_type"], nullable=nullable)
            )
        except KeyError as err:
            raise ValueError("missing required value in Column %s" % err)
    return result

def cv_obj_array(cv):
    """Turn JSON cv to list of CVObjects."""

    def build_cv_obj(cv):
        cv_objs = []
        for cv_obj in cv:
            try:
                cv_objs.append(Column(train=cv_obj["train"], test=cv_obj["test"]))
            except KeyError as err:
                raise ValueError("missing required value in cv types %s" % err)
        return cv_objs

    return None if cv is None else build_cv_obj(cv)


#!/usr/bin/env python
# _#_ coding:utf-8 _*_
from rest_framework import viewsets, permissions
from OpsManage.serializers import *
from OpsManage.models import *
from rest_framework import status
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.contrib.auth.decorators import permission_required
from django.db.models import Sum, Count

@api_view(['GET', 'PUT'])
@permission_required('OpsManage.can_read_tf_asset_soft_info', raise_exception=True)
def tf_asset_soft_num(request, type, format=None):
    try:
        snippet = TF_Asset_Soft_Info.objects.filter(sw_type=type).values('sw_name').annotate(sw_sum=Count('sw_name'))
    except TF_Asset_Soft_Info.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        data = []
        for item in snippet:
            tmp = {}
            tmp['sw_name'] = item['sw_name']
            tmp['sw_sum'] = item['sw_sum']
            data.append(tmp)
        return Response(data)

@api_view(['GET', 'PUT', 'DELETE'])
@permission_required('OpsManage.can_read_tf_asset_soft_info', raise_exception=True)
def tf_asset_soft_name(request, name, format=None):
    try:
        snippet = TF_Asset_Soft_Info.objects.filter(sw_name=name)
    except TF_Asset_Soft_Info.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = TfAssetSoftInfoSerializer(snippet, many=True)
        return Response(serializer.data)


import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()


import cv2
import numpy as np

o = cv2.imread('2.jpg')
gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,
 cv2.RETR_TREE,
 cv2.CHAIN_APPROX_SIMPLE)

cnt = contours[0]
#hull = cv2.convexHull(cnt,returnPoints = False)
hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)

for i in range(defects.shape[0]):
    s,e,f,d = defects[i,0]
    start = tuple(cnt[s][0])           # 得到的是索引，要再轮廓中选出来
    end = tuple(cnt[e][0])
    far = tuple(cnt[f][0])
    cv2.line(o,start,end,[0,0,255],2)
    cv2.circle(o,far,5,[255,0,0],-1)

cv2.imshow('result',o)
cv2.waitKey(0)
cv2.destroyAllWindows()


import socket
from typing import Tuple

class Server:
    def __init__(self, port: int):
        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        serversocket.bind(('', 8080))
        serversocket.listen(10)
        self.serversocket = serversocket

    def put_point(self, position: Tuple[float, float]):
        (x, y) = position
        self.serversocket.send()


from rest_framework import serializers

from .models import ToiletLecture, Toilet

class ToiletLectureSerializer(serializers.Serializer):
    in_use = serializers.BooleanField()
    toilet = serializers.PrimaryKeyRelatedField(queryset=Toilet.objects.all())

    def save(self, **kwargs):
        toilet = self.validated_data['toilet']
        last_lecture = ToiletLecture.last_active_lecture(toilet)
        if self.validated_data['in_use']:
            if not last_lecture:
                ToiletLecture.objects.create(toilet=toilet)
        else:
            if last_lecture:
                last_lecture.end_lecture()


from flask import Flask, render_template, request
from summarizer import *
app = Flask(__name__)

@app.route('/')
def home():
    return render_template("home.html")

@app.route('/summarize', methods=["POST"])
def summarize():
	res = request.form
	return render_template("summarized.html", result=generate_summary(res['userInput']))

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


from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import numpy as np
import time
import mlrose_hiive as mlrose

from utils import plot_fitness_curve, plot_time_curve, plot_accuracy_curve

def run_GA(X, y):
    print("...GA LEARNER....")
    iterations = [100, 200, 300, 500, 1000, 2000]
    random_states = [3, 98, 654]

    # scaling = MinMaxScaler(feature_range=(-1, 1)).fit(X)
    # X = scaling.transform(X)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=9098, shuffle=True)

    train_times = np.zeros((len(random_states), len(iterations)))
    test_times = np.zeros((len(random_states), len(iterations)))
    y_train_accuracies = np.zeros((len(random_states), len(iterations)))
    y_test_accuracies = np.zeros((len(random_states), len(iterations)))

    curves = np.zeros((len(iterations) * len(random_states), max(iterations)))

    for j, state in enumerate(random_states):
        # print (state)

        for i, num_iterations in enumerate(iterations):
            nn_model = mlrose.NeuralNetwork(hidden_nodes=[5, 2], activation='relu',
                                            algorithm='genetic_alg', max_iters=num_iterations,
                                            bias=True, is_classifier=True, learning_rate=0.01, clip_max=5,
                                            early_stopping=False, max_attempts=100, curve=True, pop_size=100,
                                            mutation_prob=0.1, random_state=state)

            tstart = time.time()
            nn_model.fit(X_train, y_train)
            tend = time.time()
            train_time = tend - tstart
            train_times[j, i] = train_time
            tstart = time.time()

            tend = time.time()
            test_time = tend - tstart
            test_times[j, i] = test_time
            y_train_accuracy = accuracy_score(y_train, nn_model.predict(X_train))
            y_train_accuracies[j, i] = y_train_accuracy
            y_test_accuracy = accuracy_score(y_test, nn_model.predict(X_test))
            y_test_accuracies[j, i] = y_test_accuracy

            curves[j * len(iterations) + i, :len(nn_model.fitness_curve)] = nn_model.fitness_curve

    plot_fitness_curve(curves, "ga", filename="plots\\NN\\ga_loss_curve.png")
    plot_time_curve(train_times, iterations, "ga", filename="plots\\NN\\ga_time_curve.png")
    plot_accuracy_curve(y_test_accuracies, y_train_accuracies, iterations, "sa",
                        filename="plots\\NN\\ga_accuracy_curve.png")

    return np.mean(y_test_accuracies[:, -1], axis=0), np.mean(train_times[:, -1], axis=0)


# -*- coding: utf-8 -*-
"""
Created on Wed May 25 14:54:47 2016
@author: Kostya S, nms code from: http://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/ and
    https://github.com/layumi/2015_Face_Detection/blob/master/nms.m
"""

import scipy as sp
import itertools
import os
from fnmatch import fnmatch

class Util():
    k_max = 48

    sn = (0.83, 0.91, 1.0, 1.10, 1.21)
    xn = (-0.17, 0.0, 0.17)
    yn = (-0.17, 0.0, 0.17)
    prod = sp.array([e for e in itertools.product(sn,xn,yn)])

    @staticmethod
    def load_from_npz(data_name):
        with sp.load(data_name) as f:
            values = [f['arr_%d' % i] for i in range(len(f.files))][0]
            return values

    @staticmethod
    def get_files(rootdir = '',fexpr = ''):
        paths = []
        for path, subdirs, files in os.walk(rootdir):
            for indx,fname in enumerate(files):
                if fexpr == None or fexpr == '' or fnmatch(fname, fexpr):
                    paths.append(os.path.join(path, fname))
        return paths

    @staticmethod
    def get_splited_files(paths,batch_size = 2):
        splited_files = paths
        N = max(1,batch_size)
        for item in [splited_files[i:i + N] for i in range(0, len(splited_files), N)]:
            labels = [0 if e.find('neg') > 0 else 1 for e in item]
            yield item,labels

    @staticmethod
    def calib (i,j,h,w,n):
        prod = Util.prod
        new_i = round(i-(prod[n][1]*w/prod[n][0]))
        new_j = round(j-(prod[n][2]*h/prod[n][0]))
        new_h = round(h/prod[n][0])
        new_w = round(w/prod[n][0])
        return (new_i,new_j,new_h,new_w)

    @staticmethod
    def calib_apply(i,j,h,w,sn,xn,yn):

        new_i = round(i-(xn*w/sn))
        new_j = round(j-(yn*h/sn))
        new_h = round(h/sn)
        new_w = round(w/sn)
        return (new_i,new_j,new_h,new_w)

    @staticmethod
    def inv_calib (i,j,h,w,n):
        prod = Util.prod
        new_i = round(i-(-prod[n][1]*w/(prod[n][0]**-1)))
        new_j = round(j-(-prod[n][2]*h/(prod[n][0]**-1)))
        new_h = round(h/prod[n][0]**-1)
        new_w = round(w/prod[n][0]**-1)
        return (new_i,new_j,new_h,new_w)

    @staticmethod
    def nms(dets,proba, T):

        dets = dets.astype("float")
        if len(dets) == 0:
            return []

        x1 = dets[:, 0]
        y1 = dets[:, 1]
        x2 = dets[:, 2]
        y2 = dets[:, 3]
        scores = proba

        areas = (x2 - x1 + 1) * (y2 - y1 + 1)
        order = scores.argsort()[::-1]

        keep = []
        while order.size > 0:
            i = order[0]
            keep.append(i)
            xx1 = sp.maximum(x1[i], x1[order[1:]])
            yy1 = sp.maximum(y1[i], y1[order[1:]])
            xx2 = sp.minimum(x2[i], x2[order[1:]])
            yy2 = sp.minimum(y2[i], y2[order[1:]])

            w = sp.maximum(0.0, xx2 - xx1 + 1)
            h = sp.maximum(0.0, yy2 - yy1 + 1)
            inter = w * h
            ovr = inter / (areas[i] + areas[order[1:]] - inter)
            inds = sp.where(ovr <= T)[0]
            order = order[inds + 1]

        return keep

    @staticmethod
    def minmax(arr):
        minv = arr[0]
        maxv = arr[0]
        for val in arr:
            if val > maxv:
                maxv = val
            elif val < minv:
                minv = val
        return int(sp.floor(minv)), int(sp.ceil(maxv))

    @staticmethod
    def ellipse2bbox(a, b, angle, cx, cy):
        a, b = max(a, b), min(a, b)
        ca = sp.cos(angle)
        sa = sp.sin(angle)
        if sa == 0.0:
            cta = 2.0 / sp.pi
        else:
            cta = ca / sa

        if ca == 0.0:
            ta = sp.pi / 2.0
        else:
            ta = sa / ca

        x = lambda t: cx + a * sp.cos(t) * ca - b * sp.sin(t) * sa

        y = lambda t: cy + b * sp.sin(t) * ca + a * sp.cos(t) * sa

        # x = cx + a * cos(t) * cos(angle) - b * sin(t) * sin(angle)
        # tan(t) = -b * tan(angle) / a
        tx1 = sp.arctan(-b * ta / a)
        tx2 = tx1 - sp.pi
        x1, y1 = x(tx1), y(tx1)
        x2, y2 = x(tx2), y(tx2)

        # y = cy + b * sin(t) * cos(angle) + a * cos(t) * sin(angle)
        # tan(t) = b * cot(angle) / a
        ty1 = sp.arctan(b * cta / a)
        ty2 = ty1 - sp.pi
        x3, y3 = x(ty1), y(ty1)
        x4, y4 = x(ty2), y(ty2)

        minx, maxx = Util.minmax([x1, x2, x3, x4])
        miny, maxy = Util.minmax([y1, y2, y3, y4])
        return sp.floor(minx), sp.floor(miny), sp.ceil(maxx), sp.ceil(maxy)

    @staticmethod
    def kfold(X,y, k_fold = 2):
        for k in range(k_fold):
            t_indx = [i for i, e in enumerate(X) if i % k_fold != k]
            v_indx = [i for i, e in enumerate(X) if i % k_fold == k]
            yield t_indx,v_indx


import datetime as dt
import numpy as np
import pandas as pd
from sklearn.utils.validation import check_array

result_mapping = {1: [1, 0, 0],
                  0: [0, 1, 0],
                  2: [0, 0, 1]}

def ranked_probability_loss(obs, preds, change_order=True):
    """
    >>> y_true = [1, 1]
    >>> y_prob = [[0.5, 0.3, 0.2], [0.5, 0.2, 0.3]]
    >>> ranked_probability_loss(y_true, y_prob) # array([0.145, 0.17 ])

    >>> y_true = [1]
    >>> y_prob = [[0.7, 0.3, 0]]
    >>> ranked_probability_loss(y_true, y_prob) # array([0.045])
    """
    if change_order:
        preds = [np.concatenate([m[:2][::-1], m[2:]]) for m in preds]

    obs = check_array(obs, ensure_2d=False)
    preds = check_array(preds, ensure_2d=False)
    obs = np.array([result_mapping[i[0]] for i in obs])

    cum_diff = np.cumsum(preds, axis=1) - np.cumsum(obs, axis=1)
    result = np.sum(np.square(cum_diff), axis=1)/2
    return np.round(result, 5)

def rps_metric(obs, preds):
    return np.mean(ranked_probability_loss(obs, preds))

def create_output(df):
    """
    Converts dataframe to comma separated string
    """
    output_list = df.to_string(header=False,
                               index=False,
                               index_names=False).split('\n')
    output_string = ','.join([','.join(ele.split()) for ele in output_list])
    return output_string

def week_converter(timestamp):
    """
    year is 2019 for dates between 2019-07 and 2020-06,
    22nd week just random splitter,
    there might be better representation

    is_national is True for Friday, Saturday, Sunday, Monday
    False otherwise
    """
    year, week, day = timestamp.isocalendar()
    season = year - 1 if week < 27 else year
    is_weekend = day >= 5 or day == 1
    return [timestamp, season, year, week, is_weekend]

def find_results(match_ids):
    matches = pd.read_csv('data/matches.zip')
    matches = matches[matches['match_id'].isin(match_ids)]
    matches['result'] = np.nan
    matches.loc[matches.match_hometeam_score > matches.match_awayteam_score,
                'result'] = 1
    matches.loc[matches.match_hometeam_score == matches.match_awayteam_score,
                'result'] = 0
    matches.loc[matches.match_hometeam_score < matches.match_awayteam_score,
                'result'] = 2
    return matches[['match_id', 'result']]

def data_prepare(bets_df, matches_df):
    final_df = bets_df.merge(matches_df, on='match_id').dropna()
    matches = pd.read_csv("data/matches.zip")
    common_cols = ["match_id", "match_hometeam_score", "match_awayteam_score"]
    matches = matches[common_cols]

    matches["total_score"] = matches["match_hometeam_score"] + \
        matches["match_awayteam_score"]

    matches["over_under"] = np.nan
    matches.loc[matches.total_score >= 3, "over_under"] = "over"
    matches.loc[matches.total_score < 3, "over_under"] = "under"

    matches['result'] = np.nan
    matches.loc[matches.match_hometeam_score > matches.match_awayteam_score,
                'result'] = 1
    matches.loc[matches.match_hometeam_score == matches.match_awayteam_score,
                'result'] = 0
    matches.loc[matches.match_hometeam_score < matches.match_awayteam_score,
                'result'] = 2
    matches.dropna(inplace=True)
    matches['result'] = matches['result'].astype(int)

    final_df = final_df.merge(matches, on="match_id")
    final_df = final_df.drop(["match_hometeam_score", "match_awayteam_score"],
                             axis=1)
    return final_df.dropna()


from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import strawberry
from strawberry.fastapi import GraphQLRouter

from .database import Query

app = FastAPI()

origins = [
    "http://localhost:3000",
    "localhost:3000"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

schema = strawberry.Schema(Query)
graphql_app = GraphQLRouter(schema)
app.include_router(graphql_app, prefix='/api')


from collections import defaultdict, namedtuple

import blockdiag.metrics
from blockdiag.utils import XY, Box

class DiagramMetrics(blockdiag.metrics.DiagramMetrics):
    def __init__(self, diagram, **kwargs):
        span_height = self.span_height
        span_width = self.span_width
        self.span_height = 0
        self.span_width = 0
        self.node_width = self.cellsize * 3
        self.colwidth = diagram.colwidth
        super(DiagramMetrics, self).__init__(diagram, **kwargs)

        # reset node_width FORCE
        self.spreadsheet.node_width = defaultdict(lambda: self.node_width)

        self.spreadsheet.set_span_height(0, span_height * 2)
        self.spreadsheet.set_span_height(diagram.colheight, span_height)
        self.spreadsheet.set_span_width(0, span_width)
        self.spreadsheet.set_span_width(diagram.colwidth, span_width)

    def measure_line(self, n):
        _Node = namedtuple('Node', 'xy')

        if n == self.colwidth:
            node = _Node(XY(n - 1, 0))
            pt = self.spreadsheet._node_topleft(node, use_padding=False)
            pt = pt.shift(x=self.node_width)
        else:
            node = _Node(XY(n, 0))
            pt = self.spreadsheet._node_topleft(node, use_padding=False)

        if n * 2.0 % self.colwidth == 0:
            return (XY(pt.x, pt.y - self.cellsize * 4), pt)
        elif n * 4.0 % self.colwidth == 0:
            return (XY(pt.x, pt.y - self.cellsize * 4), pt)
        else:
            return (XY(pt.x, pt.y - self.cellsize * 2), pt)

    def measure_label(self, n):
        top, _ = self.measure_line(n)
        cellsize = self.cellsize

        return Box(top.x - cellsize * 4, top.y - cellsize * 3,
                   top.x + cellsize * 4, top.y)


import sqlite3      #sqlite3 CRUD
#1. 연결 : database 생성
conn=sqlite3.connect('employee.db')
#2. 커서 생성
cursor=conn.cursor()
#3. 테이블 생성  employees   id(pk,autoincrement) 정수 ,name 문자열 nn,age 정수 nn ,city 문자열 nn
cursor.execute("DROP TABLE IF EXISTS employees")
cursor.execute(
    '''
    CREATE TABLE IF NOT EXISTS employees(
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        age INTEGER NOT NULL,
        city TEXT NOT NULL
    )
    '''
)

#4. insert_t, select, delete_t, update_t (_t : transaction)
def insert_employees(name, age, city):
    cursor.execute('insert into employees (name, age, city) values (?,?,?)',(name,age,city))
    conn.commit()

def selectall_employees():
    cursor.execute('select * from employees')
    return cursor.fetchall()
#수정 : 번호로 이름, 나이, 주소를 변경해보자
#1번 친구를 정길동 35 인천으로 변경해보자
def update_employees(id, name, age, city):
    cursor.execute('update employees set name=?, age=?, city=? where id =? ',(name,age,city,id))
    conn.commit()

def delete_employees(id):
    cursor.execute('delete from employees where id =?',(id,))
    conn.commit()

#5. 실행 결과
############insert

insert_employees('zoe',26,'london1')
insert_employees('hermione',23,'london2')
insert_employees('harry',23,'london3')

print(selectall_employees())
update_employees(1,'정길동',35,'인천')
print(selectall_employees())
delete_employees(1)
print(selectall_employees())
#6. conn close : 매우매우매우 중요!!
conn.close()


import discord
from discord.ext import commands
import io
import numpy as np
import cv2

# コグとして用いるクラスを定義。
class ImageCog(commands.Cog):

    # TestCogクラスのコンストラクタ。Botを受取り、インスタンス変数として保持。
    def __init__(self, bot):
        self.bot = bot

    # コマンドの作成。コマンドはcommandデコレータで必ず修飾する。
    @commands.command()
    async def color(self, ctx, col): # 関数名=コマンド名
        '''カラーコードから画像を生成''' # helpコマンドに載せる説明を書く
        rgb = []
        try:
            rgb = [int(col[4:6], 16), int(col[2:4], 16), int(col[0:2], 16)]
        except:
            rgb = [0, 0, 0]
        img = np.zeros((200, 300, 3), dtype=np.uint8)
        for i in range(3):
            img[:,:,i] = rgb[i]
        _, buf = cv2.imencode('.png', img)
        buf = io.BytesIO(buf)
        buf.seek(0)
        fl = discord.File(buf, col + '.png')
        await ctx.send(f'rgb = {list(reversed(rgb))}', file=fl)

# Bot本体側からコグを読み込む際に呼び出される関数。
def setup(bot):
    bot.add_cog(ImageCog(bot))


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import sys

import copy

import numpy as np
import scipy as scp

import logging

logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                    level=logging.INFO,
                    stream=sys.stdout)

from collections import OrderedDict

from pyvision import pretty_printer as pp
import matplotlib.pyplot as plt

class PoseMetric(object):
    """docstring for DistMetric"""
    def __init__(self, threshholds=[6, 12, 24], keep_raw=False):
        super(PoseMetric, self).__init__()

        self.distances = []
        self.rotation_dists = []
        self.thres = threshholds
        self.keep_raw = keep_raw

        self.pos = [0 for i in self.thres]
        self.neg = [0 for i in self.thres]

        self.count = 0

        self.sorted = False

    def add(self, prediction, translation, rotation):
        self.count += 1

        dist = np.linalg.norm(prediction[:3] - translation)

        self.distances.append(dist)

        for i, thres in enumerate(self.thres):
            self.pos[i] += np.sum(dist < thres)
            self.neg[i] += np.sum(dist >= thres)
            assert self.count == self.pos[i] + self.neg[i]

        rot_dist = np.linalg.norm(prediction[3:] - rotation)

        self.rotation_dists.append(rot_dist)

    def get_pp_names(self, time_unit='s', summary=False):

        pp_names1 = ["Translation Dist", "Rotation Dist", 'class_seperator']

        pp_names2 = ["T Acc @{}".format(i) for i in self.thres]

        pp_names = pp_names1 + pp_names2

        return pp_names

    def get_pp_values(self, ignore_first=True,
                      time_unit='s', summary=False):

        pp_values1 = [np.mean(self.distances) / 100,
                      np.mean(self.rotation_dists) / 100,
                      pp.NEW_TABLE_LINE_MARKER]

        pp_values2 = [self.pos[i] / self.count for i in range(len(self.thres))]

        return pp_values1 + pp_values2

    def get_pp_dict(self, ignore_first=True, time_unit='s', summary=False):

        names = self.get_pp_names(time_unit=time_unit, summary=summary)
        values = self.get_pp_values(ignore_first=ignore_first,
                                    time_unit=time_unit,
                                    summary=summary)

        return OrderedDict(zip(names, values))

    def plot_histogram(self):

        assert self.keep_raw

        x = np.linspace(0, 100, len(self.distances))
        plt.plot(x, self.distances)

        plt.show()

if __name__ == '__main__':
    logging.info("Hello World.")


#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock

from heat.engine.clients.os import monasca as client_plugin
from heat.engine import resource
from heat.engine.resources.openstack.monasca import alarm_definition
from heat.engine import stack
from heat.engine import template
from heat.tests import common
from heat.tests import utils

sample_template = {
    'heat_template_version': '2015-10-15',
    'resources': {
        'test_resource': {
            'type': 'OS::Monasca::AlarmDefinition',
            'properties': {
                'name': 'sample_alarm_id',
                'description': 'sample alarm def',
                'expression': 'sample expression',
                'match_by': ['match_by'],
                'severity': 'low',
                'ok_actions': ['sample_notification'],
                'alarm_actions': ['sample_notification'],
                'undetermined_actions': ['sample_notification'],
                'actions_enabled': False
            }
        }
    }
}

RESOURCE_TYPE = 'OS::Monasca::AlarmDefinition'

class MonascaAlarmDefinition(alarm_definition.MonascaAlarmDefinition):
    '''
    Monasca service is not available by default. So, this class overrides
    the is_service_available to return True
    '''
    @classmethod
    def is_service_available(cls, context):
        return True

class MonascaAlarmDefinitionTest(common.HeatTestCase):

    def setUp(self):
        super(MonascaAlarmDefinitionTest, self).setUp()

        self.ctx = utils.dummy_context()
        # As monascaclient is not part of requirements.txt, RESOURCE_TYPE is
        # not registered by default. For testing, its registered here
        resource._register_class(RESOURCE_TYPE,
                                 MonascaAlarmDefinition)
        self.stack = stack.Stack(
            self.ctx, 'test_stack',
            template.Template(sample_template)
        )

        self.test_resource = self.stack['test_resource']

        # Mock client plugin
        self.test_client_plugin = mock.MagicMock()
        self.test_resource.client_plugin = mock.MagicMock(
            return_value=self.test_client_plugin)
        self.test_client_plugin.get_notification.return_value = (
            'sample_notification'
        )

        # Mock client
        self.test_client = mock.MagicMock()
        self.test_resource.client = mock.MagicMock(
            return_value=self.test_client)

    def _get_mock_resource(self):
        value = dict(id='477e8273-60a7-4c41-b683-fdb0bc7cd152')

        return value

    def test_resource_handle_create(self):
        mock_alarm_create = self.test_client.alarm_definitions.create
        mock_alarm_patch = self.test_client.alarm_definitions.patch
        mock_resource = self._get_mock_resource()
        mock_alarm_create.return_value = mock_resource

        # validate the properties
        self.assertEqual(
            'sample_alarm_id',
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.NAME))
        self.assertEqual(
            'sample alarm def',
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.DESCRIPTION))
        self.assertEqual(
            'sample expression',
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.EXPRESSION))
        self.assertEqual(
            ['match_by'],
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.MATCH_BY))
        self.assertEqual(
            'low',
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.SEVERITY))
        self.assertEqual(
            ['sample_notification'],
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.OK_ACTIONS))
        self.assertEqual(
            ['sample_notification'],
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.ALARM_ACTIONS))
        self.assertEqual(
            ['sample_notification'],
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.UNDETERMINED_ACTIONS))
        self.assertEqual(
            False,
            self.test_resource.properties.get(
                alarm_definition.MonascaAlarmDefinition.ACTIONS_ENABLED))

        self.test_resource.data_set = mock.Mock()
        self.test_resource.handle_create()
        # validate physical resource id
        self.assertEqual(mock_resource['id'], self.test_resource.resource_id)

        args = dict(
            name='sample_alarm_id',
            description='sample alarm def',
            expression='sample expression',
            match_by=['match_by'],
            severity='low',
            ok_actions=['sample_notification'],
            alarm_actions=['sample_notification'],
            undetermined_actions=['sample_notification']
        )

        mock_alarm_create.assert_called_once_with(**args)
        mock_alarm_patch.assert_called_once_with(
            alarm_id=self.test_resource.resource_id,
            actions_enabled=False)

    def test_resource_handle_update(self):
        mock_alarm_patch = self.test_client.alarm_definitions.patch
        self.test_resource.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'

        prop_diff = {
            alarm_definition.MonascaAlarmDefinition.NAME:
                'name-updated',
            alarm_definition.MonascaAlarmDefinition.DESCRIPTION:
                'description-updated',
            alarm_definition.MonascaAlarmDefinition.EXPRESSION:
                'expression-updated',
            alarm_definition.MonascaAlarmDefinition.ACTIONS_ENABLED:
                True,
            alarm_definition.MonascaAlarmDefinition.SEVERITY:
                'medium',
            alarm_definition.MonascaAlarmDefinition.OK_ACTIONS:
                ['sample_notification'],
            alarm_definition.MonascaAlarmDefinition.ALARM_ACTIONS:
                ['sample_notification'],
            alarm_definition.MonascaAlarmDefinition.UNDETERMINED_ACTIONS:
                ['sample_notification']}

        self.test_resource.handle_update(json_snippet=None,
                                         tmpl_diff=None,
                                         prop_diff=prop_diff)

        args = dict(
            alarm_id=self.test_resource.resource_id,
            name='name-updated',
            description='description-updated',
            expression='expression-updated',
            actions_enabled=True,
            severity='medium',
            ok_actions=['sample_notification'],
            alarm_actions=['sample_notification'],
            undetermined_actions=['sample_notification']
        )

        mock_alarm_patch.assert_called_once_with(**args)

    def test_resource_handle_update_sub_expression(self):
        '''
        Monasca does not allow to update the metrics in the expression though
        it allows to update the metrics measurement condition range. Monasca
        client raises HTTPUnProcessable in this case

        so UpdateReplace is raised to re-create the alarm-definition with
        updated new expression.
        '''
        # TODO(skraynev): remove it when monasca client will be
        #                 merged in global requirements
        class HTTPUnProcessable(Exception):
            pass

        client_plugin.monasca_exc = mock.Mock()
        client_plugin.monasca_exc.HTTPUnProcessable = HTTPUnProcessable
        mock_alarm_patch = self.test_client.alarm_definitions.patch
        mock_alarm_patch.side_effect = (
            client_plugin.monasca_exc.HTTPUnProcessable)
        self.test_resource.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'

        prop_diff = {alarm_definition.MonascaAlarmDefinition.EXPRESSION:
                     'expression-updated'}

        self.assertRaises(resource.UpdateReplace,
                          self.test_resource.handle_update,
                          json_snippet=None,
                          tmpl_diff=None,
                          prop_diff=prop_diff)

    def test_resource_handle_delete(self):
        mock_alarm_delete = self.test_client.alarm_definitions.delete
        self.test_resource.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
        mock_alarm_delete.return_value = None

        self.assertIsNone(self.test_resource.handle_delete())
        mock_alarm_delete.assert_called_once_with(
            alarm_id=self.test_resource.resource_id
        )

    def test_resource_handle_delete_resource_id_is_none(self):
        self.test_resource.resource_id = None
        self.assertIsNone(self.test_resource.handle_delete())

    def test_resource_handle_delete_not_found(self):
        # TODO(skraynev): remove it when monasca client will be
        #                 merged in global requirements
        class NotFound(Exception):
            pass

        client_plugin.monasca_exc = mock.Mock()
        client_plugin.monasca_exc.NotFound = NotFound

        self.test_resource.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
        mock_alarm_delete = self.test_client.alarm_definitions.delete
        mock_alarm_delete.side_effect = client_plugin.monasca_exc.NotFound
        self.assertIsNone(self.test_resource.handle_delete())
        self.assertEqual(1,
                         self.test_client_plugin.ignore_not_found.call_count)
        e_type = type(self.test_client_plugin.ignore_not_found.call_args[0][0])
        self.assertEqual(type(client_plugin.monasca_exc.NotFound()), e_type)

    def test_resource_mapping(self):
        mapping = alarm_definition.resource_mapping()
        self.assertEqual(1, len(mapping))
        self.assertEqual(alarm_definition.MonascaAlarmDefinition,
                         mapping[RESOURCE_TYPE])
        self.assertIsInstance(self.test_resource,
                              alarm_definition.MonascaAlarmDefinition)

    def test_resource_show_resource(self):
        mock_notification_get = self.test_client.alarm_definitions.get
        mock_notification_get.return_value = {}

        self.assertEqual({},
                         self.test_resource._show_resource(),
                         'Failed to show resource')


from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from ezdxf.lldxf import validator
from ezdxf.lldxf.attributes import (
    DXFAttr,
    DXFAttributes,
    DefSubclass,
    XType,
    RETURN_DEFAULT,
    group_code_mapping,
    merge_group_code_mappings,
)
from ezdxf.lldxf.const import DXF12, SUBCLASS_MARKER
from ezdxf.math import NULLVEC, Z_AXIS
from ezdxf.math.transformtools import OCSTransform
from .dxfentity import base_class, SubclassProcessor
from .dxfgfx import (
    DXFGraphic,
    acdb_entity,
    elevation_to_z_axis,
    acdb_entity_group_codes,
)
from .factory import register_entity

if TYPE_CHECKING:
    from ezdxf.entities import DXFNamespace
    from ezdxf.lldxf.tagwriter import AbstractTagWriter
    from ezdxf.math import Matrix44

__all__ = ["Shape"]

# Description of the "name" attribute from the DWG documentation: 20.4.37 SHAPE (33)
# In DXF the shape name is stored. When reading from DXF, the shape is found by
# iterating over all the text styles and when the text style contains a shape file,
# iterating over all the shapes until the one with the matching name is found.

acdb_shape = DefSubclass(
    "AcDbShape",
    {
        # Elevation is a legacy feature from R11 and prior, do not use this
        # attribute, store the entity elevation in the z-axis of the vertices.
        # ezdxf does not export the elevation attribute!
        "elevation": DXFAttr(38, default=0, optional=True),
        # Thickness could be negative:
        "thickness": DXFAttr(39, default=0, optional=True),
        # Insertion point (in WCS)
        "insert": DXFAttr(10, xtype=XType.point3d, default=NULLVEC),
        # Shape size:
        "size": DXFAttr(40, default=1),
        # Shape name:
        "name": DXFAttr(2, default=""),
        # Rotation angle in degrees:
        "rotation": DXFAttr(50, default=0, optional=True),
        # Relative X scale factor
        "xscale": DXFAttr(
            41,
            default=1,
            optional=True,
            validator=validator.is_not_zero,
            fixer=RETURN_DEFAULT,
        ),
        # Oblique angle in degrees:
        "oblique": DXFAttr(51, default=0, optional=True),
        "extrusion": DXFAttr(
            210,
            xtype=XType.point3d,
            default=Z_AXIS,
            optional=True,
            validator=validator.is_not_null_vector,
            fixer=RETURN_DEFAULT,
        ),
    },
)
acdb_shape_group_codes = group_code_mapping(acdb_shape)
merged_shape_group_codes = merge_group_code_mappings(
    acdb_entity_group_codes, acdb_shape_group_codes  # type: ignore
)

@register_entity
class Shape(DXFGraphic):
    """DXF SHAPE entity"""

    DXFTYPE = "SHAPE"
    DXFATTRIBS = DXFAttributes(base_class, acdb_entity, acdb_shape)

    def load_dxf_attribs(
        self, processor: Optional[SubclassProcessor] = None
    ) -> DXFNamespace:
        """Loading interface. (internal API)"""
        # bypass DXFGraphic, loading proxy graphic is skipped!
        dxf = super(DXFGraphic, self).load_dxf_attribs(processor)
        if processor:
            processor.simple_dxfattribs_loader(dxf, merged_shape_group_codes)
            if processor.r12:
                # Transform elevation attribute from R11 to z-axis values:
                elevation_to_z_axis(dxf, ("center",))
        return dxf

    def export_entity(self, tagwriter: AbstractTagWriter) -> None:
        """Export entity specific data as DXF tags."""
        super().export_entity(tagwriter)
        if tagwriter.dxfversion > DXF12:
            tagwriter.write_tag2(SUBCLASS_MARKER, acdb_shape.name)
        self.dxf.export_dxf_attribs(
            tagwriter,
            [
                "insert",
                "size",
                "name",
                "thickness",
                "rotation",
                "xscale",
                "oblique",
                "extrusion",
            ],
        )

    def transform(self, m: Matrix44) -> Shape:
        """Transform the SHAPE entity by transformation matrix `m` inplace."""
        dxf = self.dxf
        dxf.insert = m.transform(dxf.insert)  # DXF Reference: WCS?
        ocs = OCSTransform(self.dxf.extrusion, m)

        dxf.rotation = ocs.transform_deg_angle(dxf.rotation)
        dxf.size = ocs.transform_length((0, dxf.size, 0))
        dxf.x_scale = ocs.transform_length(
            (dxf.x_scale, 0, 0), reflection=dxf.x_scale
        )
        if dxf.hasattr("thickness"):
            dxf.thickness = ocs.transform_thickness(dxf.thickness)

        dxf.extrusion = ocs.new_extrusion
        self.post_transform(m)
        return self


import logging
from django.shortcuts import render
from django.views.generic import (TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView)
from django.core.paginator import Paginator
from django.http import JsonResponse
from operations import (models as o_models, forms)
from categories import models as c_models
from import_data import views as i_views

logger = logging.getLogger('django')

class OperationListView(ListView):
    model = o_models.Operation
    paginate_by = 50

    def post(self, request, *args, **kwargs):
        # print(request.POST)
        if request.is_ajax():
            if request.POST['action'] == 'delete_operation':
                result = delete_object('Operation', {'id': request.POST['data'].replace('"', "")})
                return JsonResponse({'result': result})
            if request.POST['action'] == 'update_operation':
                result = update_object('Operation', {'id': request.POST['id']},
                                       {request.POST['field']: request.POST['new_value']})
                return JsonResponse({'rows_updated': result})
            if request.POST['action'] == 'add_operation':
                insert_result = i_views.ImportTable(request.POST['data']).insert()
                return JsonResponse(insert_result, safe=False)

    def get_filters(self):
        # print(self.request.GET)
        filter_args = {}
        filter_args['status__in'] = self.request.GET.getlist('status[]') \
            if len(self.request.GET.getlist('status[]')) > 0 else ['OK']
        if self.request.GET.get('date_from'):
            filter_args['date__gte'] = self.request.GET.get('date_from')
        if self.request.GET.get('date_to'):
            filter_args['date__lte'] = self.request.GET.get('date_to') + ' 23:59:59'
        if self.request.GET.get('merchant_code[]') \
                or self.request.GET.get('category[]') \
                or self.request.GET.get('type[]'):

            filter_arr = []
            if self.request.GET.get('category[]'):
                mcc_cat = get_foreign_objects_by_filter('MerchantCode',
                                                        {'category__in': self.request.GET.getlist('category[]')})
                for value in mcc_cat:
                    filter_arr.append(value)

            if self.request.GET.get('type[]'):
                cat_type = get_foreign_objects_by_filter('Category',
                                                         {'type__in': self.request.GET.getlist('type[]')})
                mcc_cat = get_foreign_objects_by_filter('MerchantCode',
                                                        {'category__in': cat_type})
                for value in mcc_cat:
                    filter_arr.append(value)

            if self.request.GET.get('merchant_code[]'):
                for value in self.request.GET.getlist('merchant_code[]'):
                    filter_arr.append(value)

            filter_args['merchant_code__in'] = filter_arr

        # print(filter_args)
        return filter_args

    def get_queryset(self):
        filter_args = self.get_filters()

        if self.request.GET.get('orderby') == 'ASC':
            new_context = self.model.objects.filter(
                **filter_args
            ).order_by('date')
        else:
            new_context = self.model.objects.filter(
                **filter_args
            ).order_by('-date')

        return new_context

    def get_context_data(self, **kwargs):
        context = super(OperationListView, self).get_context_data(**kwargs)
        context['model_name'] = self.model.__name__
        context['status'] = get_column('Operation', 'status')
        context['merchant_code'] = get_row_and_id('MerchantCode', 'name')
        context['category'] = get_row_and_id('Category', 'name')
        context['type'] = get_row_and_id('Type', 'name')
        context['account'] = get_row_and_id('Account', 'number')
        context['orderby'] = self.request.GET.get('orderby')
        return context

class OperationsView(OperationListView):
    template_name = 'operations/operations.html'

    def get(self, request, *args, **kwargs):
        if self.request.GET.get('filter_applied'):
            operation_list = self.get_queryset()

            paginator = Paginator(operation_list, 50)
            page_number = request.GET.get('page')
            page_obj = paginator.get_page(page_number)
            operation_list_page = paginator.page(page_number).object_list if True else ''

            return render(request, 'operations/operation_list.html', {'operation_list': operation_list_page,
                                                                      'page_obj': page_obj,
                                                                      'merchant_code': get_row_and_id('MerchantCode',
                                                                                'name')})
        return super().get(request, *args, **kwargs)

def update_object(model, filters, updates):
    logger.info('Requested update for ' + model)
    logger.info('Objects to update: ')
    for k, v in filters.items():
        logger.info('where ' + k + ' = ' + str(v))
    for k, v in updates.items():
        logger.info('Field to update: ' + k + ' to ' + str(v))
    objects = None
    if model == 'MerchantCode':
        objects = c_models.MerchantCode.objects
    if model == 'Operation':
        objects = o_models.Operation.objects

    result = objects.filter(**filters).update(**updates)
    logger.info('Updated number of objects: ' + str(result))
    return result

def delete_object(model, filters):
    logger.warning('Requested delete from ' + model)
    logger.warning('Objects to delete: ')
    for k, v in filters.items():
        logger.warning('where ' + k + ' = ' + str(v))
    objects = None
    if model == 'Operation':
        objects = o_models.Operation.objects

    result = objects.filter(**filters).delete()[1]
    for k, v in result.items():
        logger.warning('Deleted from ' + k + '. Number of items: ' + str(v))
    return result

def get_column(model, column):
    logger.info('Requested ' + column + ' from ' + model)
    objects = None
    array = []
    if model == 'Operation':
        objects = o_models.Operation.objects
    if model == 'MerchantCode':
        objects = c_models.MerchantCode.objects

    for result in list(objects.values(column).distinct()):
        array.append(result[column])

    return array

def get_row_and_id(model, column):
    logger.info('Requested ' + column + ' from ' + model)
    objects = None
    array = {}
    if model == 'MerchantCode':
        objects = c_models.MerchantCode.objects
    if model == 'Category':
        objects = c_models.Category.objects
    if model == 'Type':
        objects = c_models.Type.objects
    if model == 'Account':
        objects = o_models.Account.objects

    for result in list(objects.values(column, 'id').order_by(column).distinct()):
        array[result['id']] = result[column]

    return array

def get_foreign_objects_by_filter(model, filter_args):
    # print(filter_args)
    objects = None
    array = []
    if model == 'MerchantCode':
        objects = c_models.MerchantCode.objects
    if model == 'Category':
        objects = c_models.Category.objects

    for result in list(objects.filter(**filter_args).values('id').distinct()):
        array.append(result['id'])

    return array


import cv2
import numpy as np

# Create a NumPy array to store the telemetry data
capture = cv2.VideoCapture(0)
while True:
    image = capture.read()[1]
    telemetry_data = np.array([25, 50, 75])

    # Use the `cv2.putText()` function to display the telemetry data on the screen
    cv2.putText(image, "Temperature: {} degrees Celsius".format(telemetry_data[0]), (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))

    # Display the image using the `cv2.imshow()` function
    cv2.imshow("Telemetry", image)

    if cv2.waitKey(1) == ord('q'):
        break

# Keep the window open until the user presses a key
capture.release()
# Release the image
cv2.destroyAllWindows()