# Histogram Equalization using inbuilt function

import cv2
import numpy as np

# READ an BGR image using imread() function of OpenCV
original = cv2.imread('./resources/itachi.jpg')

# Creating a numpy matrix with dimentions same as the
# original image and data type uint8 (unsigned int 8 bit)
img = np.zeros(original.shape,dtype='uint8')

# Dsplaying the original image
cv2.imshow('Original',original)

# Applying the histogram equalization for the R,G and B channels of the original
# image using the equalizeHist() function provided in the OpenCV library
img[:,:,0]=cv2.equalizeHist(original[:,:,0])
img[:,:,1]=cv2.equalizeHist(original[:,:,1])
img[:,:,2]=cv2.equalizeHist(original[:,:,2])

# Displaying the histogram equalized image
cv2.imshow('Equalised',img)

# Stacking the two images horizontally using the hstack() function provided in
# the numpy library and storing the result as a new image
res = np.hstack((original,img))

# Saving the result image to the disk using the imwrite() function provided in the openCV library
cv2.imwrite('Ques5a.png',res)

# Display the images and wait till any key is pressed
cv2.waitKey(0)
# Destroy all the windows created by the imshow() function of the OpenCV
cv2.destroyAllWindows()


#Tkinter GUI library
from tkinter import*
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessageBox

#SQLite3 Database
import sqlite3

#Define the Main Window for application.
rootWindow = Tk()
rootWindow.title("CRUD APPLICATION")
rootWindow.geometry('%dx%d+%d+%d' % (500, 500, ((rootWindow.winfo_screenwidth()/2) - (500/2)), (rootWindow.winfo_screenheight()/2) - (500/2)))
rootWindow.resizable(0, 0)

#Get connection from SQLite3 Database.
def getDBConnection():
    global conn, DBCursor
    conn = sqlite3.connect('crud_application.db')
    DBCursor = conn.cursor()
    DBCursor.execute("CREATE TABLE IF NOT EXISTS `crud_table` (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, content TEXT)")

#SAVE Operation
#Check if the input field (INPUT_CONTENT) defined below is empty or not.
#If the data is entered, then the connection is initiated and inserted into database. Following that, the data table clears all the data
#and then retrieve all the data again from database and populates it.
def SaveOperation():
    if  INPUT_CONTENT.get() == "":
        messageLabel.config(text="Oops! Enter some data to be saved.", fg="red")
    else:
        getDBConnection()
        DBCursor.execute("INSERT INTO `crud_table` (content) VALUES(?)", str(INPUT_CONTENT.get()))

        tree.delete(*tree.get_children())	#clear previous data from data table
        DBCursor.execute("SELECT * FROM `crud_table` ORDER BY `id` DESC")
        for data in DBCursor.fetchall():
            tree.insert('', 'end', values=(data[0], data[1]))
        conn.commit()
        DBCursor.close()
        conn.close()

        INPUT_CONTENT.set("")	#clear input field
        messageLabel.config(text="Data saved.", fg="green")

#READ Operation
#Clear previously populated data and retrieve all new data and populate it.
def ReadOperation():
    tree.delete(*tree.get_children())

    getDBConnection()
    DBCursor.execute("SELECT * FROM `crud_table` ORDER BY `id` DESC")

    for data in DBCursor.fetchall():
        tree.insert('', 'end', values=(data[0], data[1]))
    DBCursor.close()
    conn.close()

    messageLabel.config(text="Data retrieved.", fg="black")

#Update Data
#Check if the data supplied is empty or not.
#If not empty, update the data to database. Then, clear previous populated data and retrieve all new data from database and populate.
#Finally, set button state.
def UpdateOperation():
    getDBConnection()

    if INPUT_CONTENT.get() == "":
        messageLabel.config(text="Oops! Enter some content.", fg="red")
    else:
        DBCursor.execute("UPDATE `crud_table` SET `content` = ? WHERE `id` = ?", (str(INPUT_CONTENT.get()), int(DATA_IDENTIFIER)))
        conn.commit()

        tree.delete(*tree.get_children())
        DBCursor.execute("SELECT * FROM `crud_table` ORDER BY `id` DESC")
        for data in DBCursor.fetchall():
            tree.insert('', 'end', values=(data[0], data[1]))
        DBCursor.close()
        conn.close()

        INPUT_CONTENT.set("")
        BTNCreate.config(state=NORMAL)
        BTNRead.config(state=NORMAL)
        BTNUpdate.config(state=DISABLED)
        BTNDelete.config(state=NORMAL)
        messageLabel.config(text="Data updated.", fg="black")

#Prepare Data to be Updated
#This function is called from below, when the data is double clicked.
#The double clicked item is selected and save in DATA_IDENTIFIER and the value is set to INPUT_CONTENT to be edit ready.
def PrepareDataEditable(event):
    global DATA_IDENTIFIER;
    retrievedData =(tree.item(tree.focus()))	#retrieve selected (focused) item data
    selectedID = retrievedData['values']
    DATA_IDENTIFIER = selectedID[0]

    INPUT_CONTENT.set("")
    INPUT_CONTENT.set(selectedID[1])

    BTNCreate.config(state=DISABLED)
    BTNRead.config(state=DISABLED)
    BTNUpdate.config(state=NORMAL)
    BTNDelete.config(state=DISABLED)

#DELETE Operation
#Check if the data is selected.
#Prompt user for delete confirmation. If the user choose 'Yes', then the selected item is delete from data table view
#and next the data is deleted from database.
def DeleteOperation():
    if not tree.selection():
       messageLabel.config(text="You need to select data.", fg="red")
    else:
        result = tkMessageBox.askquestion('Delete Operation', 'You really want to delete [y/n]?', icon="warning")
        if result == 'yes':
            retrievedData =(tree.item(tree.focus()))
            selectedID = retrievedData['values']
            tree.delete(tree.focus())

            getDBConnection()
            DBCursor.execute("DELETE FROM `crud_table` WHERE `id` = %d" % selectedID[0])
            conn.commit()
            DBCursor.close()
            conn.close()

            messageLabel.config(text="Data deleted.", fg="black")

#Exit Application
#Prompt user for confirmation and exit the application.
def ExitApplication():
    result = tkMessageBox.askquestion('Exit Application', 'You want to exit[y/n]?', icon="warning")
    if result == 'y':
        rootWindow.destroy()
        exit()

#Used Variables
#INPUT_CONTENT variable will store string and is used to identify input text field in From.
INPUT_CONTENT = StringVar()

#Layout
#Design layout of the interface that is to be presented to user.
#Run this application and see how the following line is set frame in Main Window.
labelFrame = Frame(rootWindow, width=500, height=50, bd=8, relief="raise")
labelFrame.pack(side=TOP)
dataTableFrame = Frame(rootWindow, width=600, height=500, bd=8, relief="raise")
dataTableFrame.pack(side=BOTTOM)
formFrame = Frame(dataTableFrame, width=300, height=450)
formFrame.pack(side=TOP)
buttonFrame = Frame(dataTableFrame, width=300, height=100, bd=8, relief="raise")
buttonFrame.pack(side=BOTTOM)

#Label
#Application Title
appTitle = Label(labelFrame, width=500, font=('arial', 24), text = "CRUD Application")
appTitle.pack()
#Form Input Label
labelContent = Label(formFrame, text="Content:", font=('arial', 16), bd=15)
labelContent.grid(row=0, sticky="e")
#Label to display confirmation message.
messageLabel = Label(buttonFrame)
messageLabel.pack(side=TOP)

#Input Form
#The form contains one input field identified as INPUT_CONTENT.
inputContent = Entry(formFrame, textvariable=INPUT_CONTENT, width=30)
inputContent.grid(row=0, column=1)

#Button Widget
#Button for all CRUD Operations
BTNCreate = Button(buttonFrame, width=10, text="Create", command=SaveOperation)
BTNCreate.pack(side=LEFT)
BTNRead = Button(buttonFrame, width=10, text="Read", command=ReadOperation )
BTNRead.pack(side=LEFT)
BTNUpdate = Button(buttonFrame, width=10, text="Update", command=UpdateOperation, state=DISABLED)
BTNUpdate.pack(side=LEFT)
BTNDelete = Button(buttonFrame, width=10, text="Delete", command=DeleteOperation)
BTNDelete.pack(side=LEFT)
btn_exit = Button(buttonFrame, width=10, text="Exit", command=ExitApplication)
btn_exit.pack(side=LEFT)

#Data Table
#The interface for listing all the data from database in form of data table.
scrollbary = Scrollbar(dataTableFrame, orient=VERTICAL)
scrollbarx = Scrollbar(dataTableFrame, orient=HORIZONTAL)
tree = ttk.Treeview(dataTableFrame, columns=("ID", "Content"), selectmode="extended", height=500, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('ID', text="ID", anchor=W)
tree.heading('Content', text="Content", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=0)
tree.pack()
tree.bind('<Double-Button-1>', PrepareDataEditable)	#If double clicked, then call PrepareDataEditable() function.

#START APPLICATION
if __name__ == '__main__':
    rootWindow.mainloop()


from spacy.en import English

parser = English()

def extract_entities(str):
    entityToBeSave = ""
    parsedSentence = parser(str)
    entities = list(parsedSentence.ents)
    for entity in entities:
        entityToBeSave += "{1} -> {0} | ".format(entity.label_, ' '.join([t.orth_ for t in entity]))
    return entityToBeSave


#
# Copyright 2016 The BigDL Authors.
#
# 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.
#

from typing import Optional, List, Set, Dict
from importlib.util import find_spec
from .utils import AccelerationOption
from bigdl.nano.utils import CPUInfo

def _inc_checker():
    '''
    check if intel neural compressor is installed
    '''
    return not find_spec("neural_compressor") is None

def _ipex_checker():
    '''
    check if intel pytorch extension is installed
    '''
    return not find_spec("intel_extension_for_pytorch") is None

def _onnxruntime_checker():
    '''
    check if onnxruntime and onnx is installed
    '''
    onnxruntime_installed = not find_spec("onnxruntime") is None
    onnx_installed = not find_spec("onnx") is None
    return onnxruntime_installed and onnx_installed

def _openvino_checker():
    '''
    check if openvino-dev is installed
    '''
    return not find_spec("openvino") is None

def _bf16_checker():
    '''
    bf16 availablity will be decided dynamically during the optimization
    '''
    cpuinfo = CPUInfo()
    return cpuinfo.has_bf16

def available_acceleration_combination(excludes: Optional[List[str]],
                                       includes: Optional[List[str]],
                                       full_methods: Dict[str, AccelerationOption],
                                       all_methods: Dict[str, AccelerationOption] = None):
    '''
    :return: a dictionary states the availablity (if meet depdencies)
    '''
    dependency_checker = {"inc": _inc_checker,
                          "ipex": _ipex_checker,
                          "onnxruntime": _onnxruntime_checker,
                          "openvino": _openvino_checker,
                          "pot": _openvino_checker}
    if excludes is None:
        exclude_set: Set[str] = set()
    else:
        exclude_set: Set[str] = set(excludes)
        exclude_set.discard("original")

    if includes is None:
        include_set: Set[str] = set(full_methods.keys())
    else:
        include_set: Set[str] = set(includes)
        include_set.add("original")
        if all_methods is not None:
            for method in include_set:
                if method not in full_methods:
                    # append include method into full methods
                    full_methods[method] = all_methods[method]

    available_dict = {}
    for method, option in full_methods.items():
        if method not in include_set:
            continue

        if method in exclude_set:
            continue

        available_iter = True
        for name, value in option.__dict__.items():
            if value is True:
                if name in dependency_checker and not dependency_checker[name]():
                    available_iter = False
        available_dict[method] = available_iter
    return available_dict


#!/usr/bin/env python3
import asyncio
from datetime import datetime
import sys
import pandas as pd
import numpy as np
import boto3
from decimal import Decimal
from dateutil.relativedelta import relativedelta
from cbpro import PublicClient
import math
import time
import matplotlib.pyplot as plt
from copra.websocket import Channel, Client
from copra.rest import Client as CopraClient

def flatten(l): return [item for sublist in l for item in sublist]

def plot_multi(data, cols=None, spacing=.1, **kwargs):

    from pandas import plotting

    # Get default color style from pandas - can be changed to any other color list
    if cols is None:
        cols = data.columns
    if len(cols) == 0:
        return
    colors = getattr(getattr(plotting, '_matplotlib').style,
                     '_get_standard_colors')(num_colors=len(cols))

    # First axis
    ax = data.loc[:, cols[0]].plot(label=cols[0], color=colors[0], **kwargs)
    ax.set_ylabel(ylabel=cols[0])
    lines, labels = ax.get_legend_handles_labels()

    for n in range(1, len(cols)):
        # If you need multiple y-axes
        # ax_new = ax.twinx()
        # ax.spines['right'].set_position(('axes', 1 + spacing * (n - 1)))

        # Plot using the existing y-axis
        data.loc[:, cols[n]].plot(
            ax=ax, label=cols[n], color=colors[n % len(colors)])
        ax.set_ylabel(ylabel=cols[n])

        # Proper legend position
        line, label = ax.get_legend_handles_labels()
        lines += line
        labels += label

    ax.legend(lines, labels, loc=0)
    return ax

class Data:
    def __init__(self, n):
        self.public_client = PublicClient()
        self.end_time = datetime.now()
        self.increment = relativedelta(minutes=200)
        self.start_time = self.end_time - self.increment
        self.period = n
        self.runs = 2 * int(math.ceil(n / 200))

    def get_data(self):
        data = []
        while self.runs > 0:
            response = self.public_client.get_product_historic_rates(
                'BTC-USD', start=self.start_time, end=(self.end_time), granularity=60)
            if 'message' in response:
                self.runs += 1
                time.sleep(0.5)
            else:
                data.append(response)
                self.start_time -= self.increment
                self.end_time -= self.increment
                self.runs -= 1
                time.sleep(0.5)
        return (flatten(data))

    def get_ewm(self):
        data = self.get_data()
        labels = ['timestamp', 'low', 'high', 'open', 'close', 'volume']
        df = pd.DataFrame.from_records(data, columns=labels)
        df = df.set_index('timestamp')
        df = df.sort_index(ascending=True)
        df['ewm'] = df['close'].ewm(
            span=self.period, min_periods=0, adjust=False, ignore_na=False).mean()
        # plt.figure()
        # df.plot()
        # plot_multi(df, cols=["close", "ewm"])
        return df['ewm'].iloc[-1]

class EWM:
    def __init__(self, average, period):
        self.average = average
        self.period = period
        self.multiplier = 2.0 / float(1.0 + period)

    def next(self, value):
        self.average = ((value - self.average) *
                        self.multiplier) + self.average

    def get(self):
        return self.average

class Tick:
    def __init__(self, tick_dict, ewm):
        self.sequence = tick_dict['sequence']
        self.product_id = tick_dict['product_id']
        self.best_bid = float(tick_dict['best_bid'])
        self.best_ask = float(tick_dict['best_ask'])
        self.price = float(tick_dict['price'])
        self.side = tick_dict['side']
        self.size = float(tick_dict['last_size'])
        self.time = datetime.strptime(
            tick_dict['time'], '%Y-%m-%dT%H:%M:%S.%fZ')
        self.ewm = ewm

    def get_price(self):
        return self.price

    @property
    def spread(self):
        return self.best_ask - self.best_bid

    def __repr__(self):
        rep = "{}\t\t\t\t {}\n".format(self.product_id, self.time)
        rep += "======================================================================================\n"
        rep += "Price: ${:.2f}\t\tSize: {:.8f}\tSide: {: >5}\tewm: {: >5}\n".format(
            self.price, self.size, self.side, self.ewm)
        rep += "Best ask: ${:.2f}\tBest bid: ${:.2f}\tSpread: ${:.2f}\tSequence: {}\n".format(
            self.best_ask, self.best_bid, self.spread, self.sequence)
        rep += "======================================================================================\n"
        return rep

class Ticker(Client):
    def __init__(self, ewm, loop, channels):
        super().__init__(loop=loop, channels=channels)
        self.ewm = ewm

    def on_message(self, message):
        if message['type'] == 'ticker' and 'time' in message:
            average = self.ewm.get()
            tick = Tick(message, average)
            price = tick.get_price()
            self.ewm.next(price)
            print(tick, "\n\n")
        if message['type'] == 'heartbeat':
            print(message)
        if message['type'] == 'level1':
            print(message)

class Trade:
    def __init__(self, bids, asks, ewm):
        self.bids = bids
        self.asks = asks
        self.ewm = ewm

"""
    def should_buy():
        if price < ewm & (price / ewm) < pct:
            return True
        else:
            return False

    def should_sell():
        if price > ewm & (price / ewm) > pct:
            return True
        else:
            return False
            """

period = 200
data = Data(period)
starter = data.get_ewm()
average = EWM(starter, period)

#plt.show()

"""

loop = asyncio.get_event_loop()
channels = Channel('heartbeat', 'BTC-USD')
ticker = Ticker(ewm=average, loop=loop, channels=channels)

"""
loop2 = asyncio.get_event_loop()

client = CopraClient(loop2)

async def get_order_book():
    book = await client.order_book(product_id='BTC-USD', level=1)
    book['time'] = datetime.now().isoformat()
    print(book)

while True:
    starttime=time.time()
    loop2.run_until_complete(get_order_book())
    time.sleep(1.0 - ((time.time() - starttime) % 1.0))

loop2.run_until_complete(get_order_book())

try:
    loop.run_forever()
except KeyboardInterrupt:
    loop.run_until_complete(ticker.close())
    loop.close()


from bs4 import BeautifulSoup
import requests
import re
import time
import random

def extractValue(car_property):
	return car_property.split(':')[1].strip("\"")

file_header="site,car_id,car_descrip,car_make,car_model,car_trim,car_year,car_category,car_mileage,car_price,car_extColor,car_transmission,car_driveTrain,car_vendor,car_location,car_detail_link,car_img_link"

carscomFileName = "carsdotcom_06272017_02_20001_Used"
site = "carsDotCom"
baseUrl ="https://www.cars.com"
carsFile = open("./"+carscomFileName+".csv",'w')
carsFile.write(file_header+"\n")

for pageNum in range(18,51):
	htmlSource="https://www.cars.com/for-sale/searchresults.action/?page="+str(pageNum)+"&perPage=100&rd=150&searchSource=UTILITY&sort=distance-nearest&stkTypId=28881&zc=20001"
	r = requests.get(htmlSource)
	data = r.text
	soup = BeautifulSoup(data,'html.parser')

	for listing in soup.find_all('div',class_ ="shop-srp-listings__listing"):
		car_id = str(listing['id'].split('-')[1])
		car_detail_link = str(baseUrl+listing.a['href'])
		car_img_link = str(listing.img['src'])
		car_descrip = str(listing.img['alt'])

		car_price = str(-1)
		price_line_entry = listing.find('span',class_='listing-row__price')
		if price_line_entry is not None :
			price_line = price_line_entry.contents
			if (len(price_line) >= 2) :
				car_price = str(price_line[1].replace(',',''))

		car_mileage = str(-1)
		mileage_line_entry = listing.find('span',class_='listing-row__mileage')
		if mileage_line_entry is not None :
			mileage_line = mileage_line_entry.contents
			mileage_list = mileage_line[0].split(':')
			if (len(mileage_list) == 2) :
				car_mileage = str(mileage_list[1].strip().replace(',',''))

		metainfo_items = listing.find_all('span',class_='listing-row__meta-item')
		#print(metainfo_items[0].strong.string.split(':')[0])

		car_extColor = ""
		car_transmission = ""
		car_driveTrain = ""
		if len(metainfo_items) == 3:
			car_extColor = str(metainfo_items[0].contents[1].strip())
			car_transmission = str(metainfo_items[1].contents[1].strip())
			car_driveTrain = str(metainfo_items[2].contents[1].strip())

		car_vendor = str((listing.find('a',class_='listing-row__dealer-name').contents)[0].strip())
		car_location = str(listing.find_all('span',class_="listing-row__distance")[1].contents[0].strip())

		det_list = listing.find('div',class_='listing-row__save').a['vehicle'].split(',')

		car_make = str(extractValue(det_list[2]))
		car_model = str(extractValue(det_list[4]))
		car_trim = str(extractValue(det_list[6]))
		car_year = str(extractValue(det_list[8]))
		car_category = str(extractValue(det_list[9]))

		carsFile.write(site+","+car_id+","+car_descrip+","+car_make+","+car_model+","+car_trim+","+car_year+","+car_category+","+car_mileage+","+car_price+","+car_extColor+","+car_transmission+","+car_driveTrain+","+car_vendor+","+car_location+","+car_detail_link+","+car_img_link+"\n")

	time.sleep(random.randint(45,70))

carsFile.close()


import pymysql
from connect import connection
from datetime import datetime , timedelta
import re
import pandas as pd
import time

#transform list of id to str O
def listIdToStr(id_list):
    if id_list == None or len(id_list) == 0:
        return ","
    participant = str(id_list[0])
    for i in range(1 , len(id_list)):
        participant = participant + "," + str(id_list[i])
    return participant

#insert record O
def insertRecord(title = "testing record", roomname  = "TR-306", startDate = "2020-12-30", startSection  = "5", \
    endDate = "2020-12-30", endSection  = "8", participant = ['jerry','alien','wacky'], bookName = "jerry",type = "0"):

    #get booker userid
    sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,bookName)
        connection.commit()
        B_ID = cursor.fetchone()["userID"]

    #get classroom CR_ID
    sql = "SELECT  `CR_ID` FROM `classroom` WHERE `roomname`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,roomname)
        connection.commit()
        CR_ID = cursor.fetchone()["CR_ID"]
    print("CR_ID = "  + str(CR_ID))
    #process participant
    p_id = []
    sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s"
    for ppl in participant:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,ppl)
            connection.commit()
            result = cursor.fetchone()
            if result != None:
                p_id.append(result["userID"])
    p_id_str = listIdToStr(p_id)
    print("p_id_str = " + str(p_id_str))
    #insert record into database
    sql = "INSERT INTO `record` (`title`, `CR_ID`,`startDate` , `startSection` , `endDate` , `endSection` , `participant` ,  `B_ID` , `type`) \
    VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,(title,CR_ID,startDate , startSection , endDate, endSection , p_id_str , B_ID,type))
        connection.commit()

#XX not for front-end , return dict of records(one section one record) retrun none if date not match
def processRecord(record,date):
    if str(record["startDate"]) != date:
        return None
    if record["startDate"] < record["endDate"]:
        endSection = 14
    else :
        endSection = record["endSection"]
    recordDict = {}
    for i in range(record["startSection"] , endSection + 1):
        #type , title , bookerName
        sql = "SELECT  `userName` FROM `users` WHERE `userID`= %s"
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,record["B_ID"])
            connection.commit()
            name = cursor.fetchone()["userName"]
        recordDict[i] = (record["type"],record["title"] , name)
    return recordDict

#give the condition to search classroom
#parameter (building ,capacity ,roomname ,date)
def searchClassroom(building, capacity = -1 , roomname = "" , date = "2020-01-01"):
    if roomname != None and roomname != "":
        if  building != "":
            if roomname[0:2] != building:
                return None
        if  capacity != None and capacity != "" and int(capacity) > 0:
            sql = "SELECT * FROM classroom WHERE `roomname` = %s AND `capacity` >= %s"
            condition = (roomname,capacity)
        else:
            sql = "SELECT * FROM classroom WHERE `roomname` = %s"
            condition = roomname
    else:
        if capacity != None and capacity != "" and int(capacity) > 0:
            if building != "":
                sql = "SELECT * FROM classroom WHERE `building` = %s AND `capacity` >= %s"
                condition = (building,capacity)
            else:
                sql = "SELECT * FROM classroom WHERE `capacity` >= %s"
                condition = capacity
        elif building != "":
            sql = "SELECT * FROM classroom WHERE `building` = %s"
            condition = building
        else:
            print("no condition")
    print(condition)
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,condition)
        connection.commit()
        result = cursor.fetchall()
    output = []
    sql = "SELECT * FROM record WHERE `CR_ID` = %s AND `startDate` = %s"
    print(result)
    for i in result:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,(i["CR_ID"],date))
            connection.commit()
            tmp = cursor.fetchall()
            if tmp != ():
                for j in tmp:
                    item = processRecord(j , date)
                    #building , capacity , roomname , status
                    item = {"CR_ID" : i["CR_ID"] , "building" : i["building"] , "capacity" : i["capacity"] , "roomName" : i["roomname"] , "status" : item}
                    output.append(item)
            else:
                item = {"CR_ID" : i["CR_ID"] , "building" : i["building"] , "capacity" : i["capacity"] , "roomName" : i["roomname"] , "status" : {}}
                output.append(item)
    return output

#search for one classroom and return records of a week
# return format:  dict{ 'CR_ID' : CR_ID ,'building' : building , 'capacity' : capacity , 'roomName' : roomname , 'status' : dict{id : tuple()}(7days) }
#parameter (classroom name , date of the week)
def searchOneClassroom(CR_ID = "" , date = "2020-12-29") :
    sql = "SELECT * FROM classroom WHERE `CR_ID` = %s"
    with connection.cursor() as cursor:
        cursor.execute(sql,CR_ID)
        connection.commit()
        result = cursor.fetchone()
    building = result["building"]
    capacity = result["capacity"]
    roomname = result["roomname"]
    today = datetime.fromisoformat(date)
    n = today.weekday()
    delta = timedelta(days = n)
    startDay = str((today - delta).date())
    delta = timedelta(days = 6 - n)
    endDay = str((today + delta).date())
    print(startDay + " ~ " + endDay)
    sql = "SELECT * FROM record WHERE `CR_ID` = %s AND `startDate` >= %s AND `endDate` <= %s"
    output = []
    with connection.cursor() as cursor:
        cursor.execute(sql,(CR_ID,startDay,endDay))
        connection.commit()
        tmp = cursor.fetchall()
        if tmp != ():
            for d in range(0,7):
                delta = timedelta(days = n - d)
                dailyItem = {}
                for j in tmp:
                    item = processRecord(j,str((today - delta).date()))
                    if item != None:
                        dailyItem.update(item)
                output.append(dailyItem)
            return {'CR_ID' : CR_ID , 'building' : building , 'capacity' : capacity , 'roomName' :roomname , 'status' : output}
        else:
            for d in range(0,7):
                output.append({})
            return {'CR_ID' : CR_ID , 'building' : building , 'capacity' : capacity , 'roomName' :roomname , 'status' : output}

#search the records the user book
def getRecordByBooker(userName):
    sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,userName)
        connection.commit()
        B_ID = cursor.fetchone()["userID"]

    sql = "SELECT  * FROM `record` WHERE `B_ID`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,B_ID)
        connection.commit()
        results = cursor.fetchall()
    #sql1 = "SELECT `userName`  FROM `users` WHERE `userID`= %s"
    sql2 = "SELECT `roomname`  FROM `classroom` WHERE `CR_ID`= %s"
    for result in results:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql2,result['CR_ID'])
            connection.commit()
            tmp = cursor.fetchone()
        result['roomName'] = tmp
        p_name = []
        participants = result['participant']
        participants = participants.split(',')
        if len(participants) == 0:
            continue
        sql1 = "SELECT `userName` FROM `users` WHERE `userID` IN ({seq})".format(seq=','.join(['%s']*len(participants)))
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,participants)
            connection.commit()
            tmp = cursor.fetchall()
        for i in tmp:
            if i != None:
                p_name.append(i['userName'])
        result['participant'] = p_name
    return results

#search the records the user's email book
def getRecordByBookerEmail(email):
    sql = "SELECT  `userID` FROM `users` WHERE `email`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,email)
        connection.commit()
        B_ID = cursor.fetchone()["userID"]

    sql = "SELECT  * FROM `record` WHERE `B_ID`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,B_ID)
        connection.commit()
        results = cursor.fetchall()
    #sql1 = "SELECT `userName`  FROM `users` WHERE `userID`= %s"
    sql2 = "SELECT `roomname`  FROM `classroom` WHERE `CR_ID`= %s"

    for result in results:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql2,result['CR_ID'])
            connection.commit()
            tmp = cursor.fetchone()
        result['roomName'] = tmp['roomname']
        p_name = []
        participants = result['participant']
        participants = participants.split(',')
        if len(participants) == 0:
            continue
        sql1 = "SELECT `userName` FROM `users` WHERE `userID` IN ({seq})".format(seq=','.join(['%s']*len(participants)))
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql1,participants)
            connection.commit()
            tmp = cursor.fetchall()
        for i in tmp:
            if i != None:
                p_name.append(i['userName'])
        result['participant'] = p_name
    return results

#get the record by id
def getRecordById(id):
    sql = "SELECT * FROM `record` WHERE `recordID` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,id)
        connection.commit()
        result = cursor.fetchone()
    if result == None:
        print("no id of this result")
        return None
    sql1 = "SELECT `userName`  FROM `users` WHERE `userID`= %s"
    sql2 = "SELECT `roomname`  FROM `classroom` WHERE `CR_ID`= %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql2,result['CR_ID'])
        connection.commit()
        tmp = cursor.fetchone()
    result['roomName'] = tmp['roomname']
    p_name = []
    participants = result['participant']
    participants = participants.split(',')
    for i in participants:
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql1,i)
            connection.commit()
            tmp = cursor.fetchone()
            if tmp != None:
                p_name.append(tmp['userName'])
            else :
                print(i)
    result['participant'] = p_name
    return result

#update record by record id
def updateRecord(recordID , title ,participants):
    if title != None and title != "":
        sql = "UPDATE `record` SET `title` = %s WHERE `recordID` = %s"
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,(title,recordID))
            connection.commit()
    if participants != None and len(participants) != 0:
        p_id = []
        """
        sql = "SELECT  `userID` FROM `users` WHERE `userName`= %s FOR UPDATE"
        for ppl in participants:
            connection.ping(reconnect = True)
            with connection.cursor() as cursor:
                cursor.execute(sql,ppl)
                result = cursor.fetchone()
                if result != None:
                    p_id.append(result["userID"])
        """
        sql = "SELECT `userID` FROM `users` WHERE `userName` IN ({seq})".format(seq=','.join(['%s']*len(participants)))
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,participants)
            connection.commit()
            result = cursor.fetchall()
        for i in result:
            if i != None:
                p_id.append(i["userID"])
        p_id_str = listIdToStr(p_id)
        sql = "UPDATE `record` SET `participant` = %s WHERE `recordID` = %s"
        connection.ping(reconnect = True)
        with connection.cursor() as cursor:
            cursor.execute(sql,(p_id_str,recordID))
            connection.commit()
    return True

def deleteRecord(recordID):
    sql = "DELETE FROM `record` WHERE `recordID` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,recordID)
        connection.commit()
    return True
#display all record
def showRecord():
    sql = "SELECT * FROM record"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql)
        connection.commit()
        result = cursor.fetchall()
    print(result)
    return result

def modify_record(data):
    recordId = data['recordID']
    participants = []
    for i in range(int(data['counter'])):
        p = data.get('participant' + str(i))
        if  p != None and p != '':
            participants.append(data['participant' + str(i)])
    print(participants)
    result = updateRecord(recordId,data['title'],participants)
    return result

def borrow(data, borrow_type , booker):
    """
    borrow a classroom
    process data and call insertRecord
    """
    participants = []
    have = 0
    if borrow_type == 'ban':
        borrow_type = 0
    else:
        borrow_type = 1
    sql = "SELECT `CR_ID` FROM `classroom` WHERE `roomname` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,data['roomName'])
        connection.commit()
        CR_ID = cursor.fetchone()['CR_ID']

    sql = "SELECT * FROM `record` WHERE `CR_ID` = %s"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql,CR_ID)
        connection.commit()
        record_total = cursor.fetchall()
    d_startTime = datetime.fromisoformat(str(data["startDate"]))
    d_endTime = datetime.fromisoformat(str(data["endDate"]))
    record_delete = []
    for r in record_total:
        startTime = datetime.fromisoformat(str(r["startDate"]))
        endTime = datetime.fromisoformat(str(r["endDate"]))
        after_record = (endTime < d_startTime or (endTime == d_startTime and int(r["endSection"]) < int(data["startSection"])))
        before_record = (startTime > d_endTime or (startTime == d_endTime and int(r["startSection"]) > int(data["endSection"])))
        if not (after_record or before_record):
            record_delete.append(r)
            have = 1
    #already have record
    if borrow_type == 1 and have == 1:
        return False
    if borrow_type == 0:
        for r in record_delete:
            deleteRecord(r['recordID'])
    for i in range(int(data['counter'])):
        p = data.get('participant' + str(i))
        if  p != None and p != '':
            participants.append(data['participant' + str(i)])
    insertRecord(title = data['title'], roomname  = data['roomName'], \
    startDate = data['startDate'], startSection  = data['startSection'], \
    endDate = data['endDate'], endSection  = data['endSection'], participant = participants, \
    bookName = booker,type = borrow_type)

    return True

def filter_classroom(data): #開始日期 startDate,開始節數 startSection,結束日期endDate ,結束節數endSection,大樓(building),可容納人數(capacity)
    """
    get the record that between the time period
    """
    sql = "SELECT * FROM Classroom"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql)
        connection.commit()
        classroom_total = cursor.fetchall()
    classroom_total = pd.DataFrame(classroom_total)
    if data['building'] != None:
        building = re.findall("[A-Z0-9]+",data['building'])
        if(len(building) > 0):
            building = building[0]
        else:
            building = ""
        classroom_total = classroom_total.loc[classroom_total["building"] ==  building ]
    if data['capacity'] != None:
        classroom_total = classroom_total.loc[classroom_total["capacity"] > int(data["capacity"]) ]
    print(classroom_total)
    sql = "SELECT * FROM Record"
    connection.ping(reconnect = True)
    with connection.cursor() as cursor:
        cursor.execute(sql)
        connection.commit()
        record_total = cursor.fetchall()
    record_CR_ID = []
    for r in record_total:
        if r["CR_ID"] in classroom_total["CR_ID"]:
            record_CR_ID.append(r)
    d_startTime = datetime.fromisoformat(str(data["startDate"]))
    d_endTime = datetime.fromisoformat(str(data["endDate"]))
    for r in record_CR_ID:
        startTime = datetime.fromisoformat(str(r["startDate"]))
        endTime = datetime.fromisoformat(str(r["endDate"]))
        after_record = (endTime < d_startTime or (endTime == d_startTime and int(r["endSection"]) < int(data["startSection"])))
        before_record = (startTime > d_endTime or (startTime == d_endTime and int(r["startSection"]) > int(data["endSection"])))
        if not (after_record or before_record):
            classroom_total = classroom_total.drop(classroom_total.loc[classroom_total["CR_ID"] == r["CR_ID"]].index)
    classroom_total = classroom_total.drop("CR_ID",axis = 1)
    classroom_total = classroom_total.T.to_dict().values()
    return classroom_total


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Team 18

"""

###### importing necessary packages #####
import pandas as pd
from scipy.io import arff ### since the file is in arff format #####

###### dataset preprocessing ##########
data = arff.loadarff('OBS-Network-DataSet_2_Aug27.arff')
df = pd.DataFrame(data[0])
df.shape
df.describe()
####### Since the column are not named properly  so column renaming is required #########
columns_new=['Node','Utilised_Bandwith_Rate','Packet_Drop_Rate','Full_Bandwidth',
         'Average_Delay_Time_Per_Sec','Percentage_Of_Lost_Packet_Rate','Percentage_Of_Lost_Byte_Rate','Packet_Received_Rate',
         'of_Used_Bandwidth','Lost_Bandwidth','Packet_Size_Byte','Packet_Transmitted',
         'Packet_Received','Packet_lost','Transmitted_Byte','Received_Byte',
         '10_Run_AVG_Drop_Rate','10_Run_AVG_Bandwith_Use','10_Run_Delay','Node_Status',
         'Flood_Status','Class']
len(columns_new)
df.columns=columns_new
df.columns
df.info
df.describe()
################################
###### checking for missing values #######
missing_data = pd.DataFrame({'total_missing': df.isnull().sum(), 'perc_missing': (df.isnull().sum()/1075)*100})
missing_data
######### removing rows having null entries ######
df = df[pd.notnull(df['Packet_lost'])]

######## cross checking after removing null values ######

missing_data = pd.DataFrame({'total_missing': df.isnull().sum(), 'perc_missing': (df.isnull().sum()/1075)*100})
missing_data
df.shape
df.columns
############ Plotting  HIstogram #################
hist_se=df.hist(bins=10, figsize=(20, 15),grid=False)
###### from histogram we can see that the feature 'Node' and packet size byte are of no use ###
######### droping column which are of no use #######
df1=df.drop(columns=['Packet_Size_Byte','Node'])
df1.shape

df1.head
######### interchanging feature 'Flood_Status' and 'Node_Status' #######
df=df1.reindex(['Utilised_Bandwith_Rate', 'Packet_Drop_Rate', 'Full_Bandwidth',
       'Average_Delay_Time_Per_Sec', 'Percentage_Of_Lost_Packet_Rate',
       'Percentage_Of_Lost_Byte_Rate', 'Packet_Received_Rate',
       'of_Used_Bandwidth', 'Lost_Bandwidth', 'Packet_Transmitted',
       'Packet_Received', 'Packet_lost', 'Transmitted_Byte', 'Received_Byte',
       '10_Run_AVG_Drop_Rate', '10_Run_AVG_Bandwith_Use', '10_Run_Delay',
       'Flood_Status', 'Node_Status', 'Class'],axis=1)

df.shape
df.head(5)
######## Converting column values of 'Node_status' and 'Class' in nummerical values ######
df['Node_Status'].unique()
df['Class'].unique()
df['Node_Status']=df['Node_Status'].map({"b'B'":0,'b"\'P NB\'"':1,"b'NB'":2})
df['Class']=df['Class'].map({'b"\'No Block\'"':0,'b"\'NB-No Block\'"':1,"b'NB-Wait'":2,"b'Block'":3})
df.head(5)

############ plotting to check for Outlier using box plot #######
df.to_csv('Dataset_Rearranged.csv', index=False)


from Bio import SeqIO
def get_known_cluster_blast(bgc):
	if hasattr(bgc,'antismash_file'):
		bgc_file = bgc.antismash_file
		if not bgc_file:
			print("No antismash file in {}".format(bgc))
			return [] # Stops it being repeatedly called
		records = list(SeqIO.parse(bgc.antismash_file,'genbank'))
		hits = []
		for record in records:
			for feature in record.features:
				if feature.type == 'cluster':
					bgc_list = feature.qualifiers.get('knownclusterblast',None)
					if bgc_list:
						for bgc_item in bgc_list:
							tokens = bgc_item.split('\t')
							bgc_name = tokens[0].split('.')[1].strip()
							similarity = tokens[1].split('(')[1].split('%')[0]
							hits.append((bgc_name,similarity))
		return hits


from zope.component import getUtility
from zope.site.hooks import getSite
from zope.globalrequest import getRequest
from collective.beaker.interfaces import ISession
from plone.registry.interfaces import IRegistry

from chromsystems.shop.interfaces import ICSShopSettings
from chromsystems.shop.interfaces import IShoppingCartUtility

def format_price(price):
    if price < 0:
        return '-%0.2f EUR' % abs(price)
    else:
        return '%0.2f EUR' % price

def get_cart():
    return getUtility(IShoppingCartUtility).get(getSite())

def wipe_cart():
    return getUtility(IShoppingCartUtility).destroy(getSite())

def update_cart():
    return getUtility(IShoppingCartUtility).update(getSite())

def redirect_to_shop():
    site = getSite()
    request = getRequest()
    registry = getUtility(IRegistry)
    settings = registry.forInterface(ICSShopSettings)
    shop_url = settings.shop_url
    portal_url = site.absolute_url()
    url = portal_url + shop_url
    request.response.redirect(url)

def redirect_to_cart():
    site = getSite()
    request = getRequest()
    registry = getUtility(IRegistry)
    settings = registry.forInterface(ICSShopSettings)
    shop_url = settings.shop_url
    portal_url = site.absolute_url()
    url = portal_url + shop_url + '/@@cart'
    request.response.redirect(url)

def redirect_to_checkout():
    site = getSite()
    request = getRequest()
    session = ISession(request)
    session.save()
    registry = getUtility(IRegistry)
    settings = registry.forInterface(ICSShopSettings)
    shop_url = settings.shop_url
    portal_url = site.absolute_url()
    url = portal_url + shop_url + '/@@checkout'
    request.response.redirect(url)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 16 22:10:39 2020

@author: tomasg
"""

import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
import pickle
from scipy.ndimage import gaussian_filter as gauss_fil
from os import listdir
from skimage import measure
from scipy.ndimage import rotate

def Mover_Imagen(image,x,y):
    Diaba=image
    if (x>=0):
        Da=np.zeros([35,35+x])
        Diaba=np.append(Diaba, Da,axis=1)
        Diaba=np.append(Da[:,:35-x], Diaba,axis=1)
    elif (x<0):
        Da=np.zeros([35,35-x])
        Diaba=np.append(Da, Diaba,axis=1)
        Diaba=np.append(Diaba,Da[:,:35+x] ,axis=1)
    if (y>=0):
        Da=np.zeros([35+y,105])
        Diaba=np.append(Diaba, Da,axis=0)
        Diaba=np.append(Da[:35-y,:], Diaba,axis=0)
    elif (y<0):
        Da=np.zeros([35-y,105])
        Diaba=np.append(Da, Diaba,axis=0)
        Diaba=np.append(Diaba, Da[:35+y,:],axis=0)
    #plt.imshow(Diaba,cmap="jet")

    return Diaba

def Mapa_Corr(Image_org, min_spikes = 20):

    Mapa_c=np.zeros([70,70])

    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,i,j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,-i,-j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35-j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,-i,j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_org,i,-j)[35:70,35:70]
            Original=np.copy(Image_org)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>min_spikes:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35-j]=np.nan
    return Mapa_c

def Mapa_Cross_Corr(Image_Movil, Image_Fija):

    Mapa_c=np.zeros([70,70])

    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,i,j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,-i,-j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35-j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,-i,j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35-i][35+j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35-i][35+j]=np.nan
    for i in range(35):
        for j in range(35):
            Movida=Mover_Imagen(Image_Movil,i,-j)[35:70,35:70]
            Original=np.copy(Image_Fija)
            Movida, Original= Eliminar_zeros(Movida, Original)
            if len(Movida)>20:
                Diff=pd.DataFrame(np.concatenate((Original,Movida),axis=1))
                Mapa_c[35+i][35-j]=Diff.corr(method="pearson")[0][1]
            else:
                Mapa_c[35+i][35-j]=np.nan
    return Mapa_c

def Cargar(name):
    """Cargar archivos"""
    with open (name, "rb") as hand:
        dic=pickle.load(hand)
    return dic

def Propiedades_mapa_return_thres(map_correlacion):
    """Funcion para determinar las propiedas de las regiones del mapa de grilla. Retorna las
    propiedades del mapa y el mapa umbrelizado"""
    map_correlacion[map_correlacion>0.2]=1
    map_correlacion[map_correlacion<=0.2]=0
    label=measure.label(map_correlacion)
    propiedades=measure.regionprops(label)
    return propiedades,map_correlacion

def Radio_inter_externo_distancia_segundo(propiedades,Corre):
    """Calculo de radio interno, externo y la distancia de los centros.
    radio interno: pixel más cercano al centro de la imagen (considerando los 6 cluster más cercanos)
    radio externo: centro más alejado del centro de la imagen (considerando los 6 cluster más cercanos)
    """
    center_x=Corre.shape[0]/2
    center_y=Corre.shape[1]/2
    distancia_x=[reg.centroid[0] for reg in propiedades]
    distancia_y=[reg.centroid[1] for reg in propiedades]
    distancias=Calculo_distancias(distancia_x, distancia_y,center_x,center_y)
    radio_interno=Calculo_radio_interno(propiedades,distancias, center_x, center_y)
    distancias_copy=distancias.copy()
    distancias_copy.sort()
    distancias_copy=distancias_copy[1:7]
    radio_externo=Selecion_distancia_maxima(distancias_copy)
    return distancias,radio_interno,radio_externo

def Recort_inferior(corr, radio_interno, center_x, center_y):
    """Elimina todos los valores que se encuentran dentro de una circunferencia
    delimitada por el radio interno (centrada en la imagen)"""
    matriz_verificacion=np.zeros_like(corr)
    matriz_verificacion[matriz_verificacion==0]=False
    for i in range(matriz_verificacion.shape[0]):
        for j in range(matriz_verificacion.shape[1]):
            if np.sqrt(np.square(i-center_x) + np.square(j-center_y) )<=radio_interno:
                matriz_verificacion[i][j]=True
    co=np.copy(corr)
    co[matriz_verificacion==1]=np.nan
    return co

def Recort_superior(corr, radio_externo, center_x, center_y):
    """Elimina todos los valores que se encuentran dentro de una circunferencia
    delimitada por el radio interno (centrada en la imagen)"""
    matriz_verificacion=np.zeros_like(corr)
    matriz_verificacion[matriz_verificacion==0]=False
    for i in range(matriz_verificacion.shape[0]):
        for j in range(matriz_verificacion.shape[1]):
            if np.sqrt(np.square(i-center_x) + np.square(j-center_y) )>=radio_externo:
                matriz_verificacion[i][j]=True
    co=np.copy(corr)
    co[matriz_verificacion==1]=np.nan
    return co

def Recortar_mapa(Corr, radio_interno, radio_externo, center_x, center_y):
    """Funcion que recorto la imagen dejando los valores entre el radio interno
    y el externo"""
    corr_to_recort= np.copy(Corr)
    corr_to_recort=Recort_inferior(corr_to_recort,radio_interno, center_x, center_y)
    corr_to_recort=Recort_superior(corr_to_recort,radio_externo, center_x, center_y)
    return corr_to_recort

def Eliminar_nan(mapa1, mapa2):
    """Elimina ordenadamete los nan de ambos mapas"""
    vect1=mapa1.flatten()
    vect2=mapa2.flatten()
    vect1[np.isnan(vect1)]=900
    vect2[np.isnan(vect2)]=900
    auxiliar_1=np.take(vect1,np.where(vect2!=900))
    auxiliar_2=np.take(vect2,np.where(vect1!=900))
    vect1=np.take(auxiliar_1,np.where(auxiliar_1[0]!=900))
    vect2=np.take(auxiliar_2,np.where(auxiliar_2[0]!=900))
    return vect1,vect2

def Eliminar_zeros(mapa1, mapa2):
    """Elimina ordenadamete los 0 de ambos mapas"""
    vect1=mapa1.flatten()
    vect2=mapa2.flatten()
    vect1[vect1==0]=900
    vect2[vect2==0]=900
    auxiliar_1=np.take(vect1,np.where(vect2!=900))
    auxiliar_2=np.take(vect2,np.where(vect1!=900))
    vect1=np.take(auxiliar_1,np.where(auxiliar_1[0]!=900))
    vect2=np.take(auxiliar_2,np.where(auxiliar_2[0]!=900))
    return vect1,vect2

def Propiedades_mapa_return_thres(map_correlacion):
    """Funcion para determinar las propiedas de las regiones del mapa de grilla. Retorna las
    propiedades del mapa y el mapa umbrelizado"""
    map_correlacion[map_correlacion>0.2]=1
    map_correlacion[map_correlacion<=0.2]=0
    label=measure.label(map_correlacion)
    propiedades=measure.regionprops(label)
    return propiedades,map_correlacion

def Eliminar_zeros(array1, array2):
    array2 = np.nan_to_num(array2).flatten()
    array1 = np.nan_to_num(array1).flatten()
    array2_tmp = np.take(array2, np.where(array1 != 0))
    array1_tmp = np.take(array1, np.where(array2 != 0))
    array2 = np.take(array2_tmp, np.where(array2_tmp[0] != 0))
    array1 = np.take(array1_tmp, np.where(array1_tmp[0] != 0))
    return array1.T, array2.T

def Calculo_dist_radial(lista, center_x, center_y):
    """Devuelve una lista con las distancias de cada pixel al centro"""
    radio_interno=lista.coords-(center_x, center_y)
    radio_interno=np.square(radio_interno)
    radio_interno=np.sum(radio_interno,axis=1)
    radio_interno=np.sqrt(radio_interno)
    return radio_interno

def Calculo_radio_interno(propiedades, distancias, center_x, center_y):
    """Devuelve el radio interno (para eliminar el centroide del centro)"""

    distancias_copy=distancias.copy()
    distancias_copy.sort()
    distancias_copy=distancias_copy[1:7]
    dist_regions=[]
    for i in distancias_copy:
        region=distancias.index(i)
        dist_regions.append(min(Calculo_dist_radial(propiedades[region], center_x, center_y) ) )
    radio_externo=min(dist_regions)
    return radio_externo

def Selecion_distancia_maxima(distancia):
    """Selecciona el radio externo de tal forma de asegurarse
    de que el radio seleccionado no sea mayor a 30"""
    if int(distancia[len(distancia)-1])<30:
        return distancia[len(distancia)-1]
    else:
        return Selecion_distancia_maxima(distancia[:len(distancia)-1])

def Calculo_radio_externo(propiedades, distancias, center_x, center_y):
    """Devuelve el radio externo (para segmentar unicamente los 6 patrones
    centrale"""
    distancias_copy=distancias.copy()
    distancias_copy.sort()
    distancias_copy=distancias_copy[1:7]
    dist_regions=[]
    for i in distancias_copy:
        region=distancias.index(i)
        dist_regions.append(max(Calculo_dist_radial(propiedades[region], center_x,
                                                    center_y) ) )
    radio_externo=max(dist_regions)
    return radio_externo

def Calculo_distancias(distancia_x, distancia_y,center_x,center_y):
    """Determino la distancia de los distintos centroides al centro del
    mapa de correlacion"""
    fun_distancia=lambda x,y: np.sqrt(np.square(x-center_x)+np.square(y-center_y))
    distancias=list(map(fun_distancia,distancia_x,distancia_y))
    return distancias

def Calculo_grid_not_origin_extern_radios_variables(corre, date="none", clu1="none", clu2="none"):
    """ prueba calculando la corre con la imagen thresholdiada y variando el radio externo
    para determinar el maximo grid score"""
    center_x = corre.shape[0]/2
    center_y = corre.shape[1]/2
    mapa_corre = np.copy(corre)
    propiedades, mapa_corre=Propiedades_mapa_return_thres(mapa_corre)
    dist,radio_interno,radio_externo=Radio_inter_externo_distancia_segundo(propiedades,corre)
    score=[]
    if len(propiedades)>7:
        for radio_externo in range(int(radio_externo),int(center_y),1):
            coef=[]
            for angle in range(30,180,30):
                # rotado2=Rotar(corre,angle,center_x,center_y)
                mapa_corre1=np.nan_to_num(mapa_corre.copy())
                mapa_rotado1=rotate(mapa_corre1,angle,reshape=False)
                mapa_rotado1=np.round(mapa_rotado1)
                mapa_rotado1=Recortar_mapa(mapa_rotado1, radio_interno, radio_externo, center_x, center_y)
                mapa_corre1=Recortar_mapa(mapa_corre1, radio_interno, radio_externo, center_x, center_y)
                mapa_corre1, mapa_rotado1=Eliminar_nan(mapa_corre1, mapa_rotado1)
                # mapa_corre1, mapa_rotado1=Eliminar_zeros(mapa_corre1,mapa_rotado1)
                coef.append(np.corrcoef( mapa_corre1,mapa_rotado1)[0][1])
            grid_score=min([ coef[i] for i in [1, 3] ] )-max([coef[i] for i in [0, 2, 4] ] )
            score.append(grid_score)
        try:
            return max(score)
        except:
            print("No se puedo ejecutar la siguiente combinacion date={}_clu1={}_clu2={}.".format(date, clu1, clu2))
    else:
        return np.nan

if __name__ == "__main__":
    print("Se ejecuta modo manual")
    a=np.random.rand(35,35)
    plt.imshow(Mapa_Corr(a))
    plt.show()


import logging
# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="cooke"
__date__ ="$29-Sep-2011 13:58:46$"
import time

from ant.core import log
from sofiehdfformat.core.SofieUtils import convertRawDataToHexString

"""
Implements a File like object to read lines from the SparkFunLogFile file.

"""
def open(filename):
        reader = log.LogReader(filename)
        return SparkFunFile(reader);

class SparkFunFile(object):
    def __init__(self, reader):
        self.reader = reader

    def readline(self,size=0):
        """
        Reads one sample from the file.
        """
        event = self.reader.read()
        while (event != None):

            if event[0] == log.EVENT_READ:
                data = event[2]
                #logging.debug('========== [EVENT_READ:{0}] =========='.format(event[1]))
                #if logging.isEnabledFor(logging.debug):
                #    length = 8
                #    while data:
                #        row = data[:length]
                #        data = data[length:]
                #        hex_data = ['%02X' % ord(byte) for byte in row]
                #        logging.debug('0x'+' '.join(hex_data))
                logging.debug('TIMESTAMP:'+\
                    time.ctime(event[1])+\
                    ' : '+convertRawDataToHexString(data))
                return {'packet':data,'timestamp':event[1]};
            event = self.reader.read()
        if event == None:
            return False;

    def close(self):
        self.reader.close();

    def __iter__(self):
        return self

    def next(self):
        data = self.readline()
        if data == False or data == None:
            raise StopIteration
        return data


from django.urls import path
from .views import StatusList, StatusEdit, StatusDelete, StatusNovo

urlpatterns = [
    path('', StatusList.as_view(), name='list_status'),
    path('editar/<int:pk>', StatusEdit.as_view(), name='update_status'),
    path('delete/<int:pk>', StatusDelete.as_view(), name='delete_status'),
    path('novo', StatusNovo.as_view(), name='create_status'),

]


from tapiriik.services.service_base import ServiceAuthenticationType, ServiceBase
from tapiriik.services.service_record import ServiceRecord
from tapiriik.services.api import APIException, UserException, UserExceptionType
from tapiriik.services.interchange import UploadedActivity, ActivityType, ActivityStatistic, ActivityStatisticUnit, WaypointType, Waypoint, Location, Lap
from tapiriik.settings import WEB_ROOT, MAPMYFITNESS_CLIENT_KEY, MAPMYFITNESS_CLIENT_SECRET

from datetime import datetime, timedelta
from urllib.parse import parse_qs, urlencode
import json
import pytz
import requests
from django.core.urlresolvers import reverse
from requests_oauthlib import OAuth1

import logging

logger = logging.getLogger(__name__)

class MapMyFitnessService(ServiceBase):
    ID = "mapmyfitness"
    DisplayName = "MapMyFitness"
    DisplayAbbreviation = "MMR"
    AuthenticationType = ServiceAuthenticationType.OAuth
    UserAuthorizationURL = None
    AuthenticationNoFrame = True
    OutstandingOAuthRequestTokens = {}
    IsNew = True

    _activityMappings = {"16": ActivityType.Running,
                         "11": ActivityType.Cycling,
                         "41": ActivityType.MountainBiking,
                         "9": ActivityType.Walking,
                         "24": ActivityType.Hiking,
                         "398": ActivityType.DownhillSkiing,
                         "397": ActivityType.CrossCountrySkiing,  # actually "backcountry" :S
                         "107": ActivityType.Snowboarding,
                         "86": ActivityType.Skating,  # ice skating
                         "15": ActivityType.Swimming,
                         "57": ActivityType.Rowing,  # canoe/rowing
                         "211": ActivityType.Elliptical,
                         "21": ActivityType.Other}
    SupportedActivities = list(_activityMappings.values())

    def WebInit(self):
        redirect_uri = WEB_ROOT + reverse("oauth_return", kwargs={"service": "mapmyfitness"})
        params = {'client_id': MAPMYFITNESS_CLIENT_KEY,
                  'response_type': 'code',
                  'redirect_uri': redirect_uri}
        self.UserAuthorizationURL = \
            "https://api.mapmyfitness.com/v7.1/oauth2/authorize/?" + urlencode(params)

    def GenerateUserAuthorizationURL(self, session, level=None):
        oauth = OAuth1(MAPMYFITNESS_CLIENT_KEY, client_secret=MAPMYFITNESS_CLIENT_SECRET)
        response = requests.post("https://api.mapmyfitness.com/v7.1/oauth2/request_token", auth=oauth)
        credentials = parse_qs(response.text)
        token = credentials["oauth_token"][0]
        self.OutstandingOAuthRequestTokens[token] = credentials["oauth_token_secret"][0]
        reqObj = {"oauth_token": token, "oauth_callback": WEB_ROOT + reverse("oauth_return", kwargs={"service": "mapmyfitness"})}
        return "https://api.mapmyfitness.com/v7.1/oauth2/authorize?" + urlencode(reqObj)

    def _apiHeaders(self, serviceRecord):
        return {"Authorization": "Bearer " + serviceRecord.Authorization["Token"],
                "Accept-Charset": "UTF-8"}

    def _getUserId(self, serviceRecord):
        response = requests.get("https://api.mapmyfitness.com/v7.1/user/self", headers=self._apiHeaders(serviceRecord))
        responseData = response.json()
        return responseData["id"]

    def RetrieveAuthorizationToken(self, req, level):
        from tapiriik.services import Service

        code = req.GET.get("code")
        params = {"grant_type": "authorization_code",
                  "code": code,
                  "client_id": MAPMYFITNESS_CLIENT_KEY,
                  "client_secret": MAPMYFITNESS_CLIENT_SECRET,
                  "redirect_uri": WEB_ROOT + reverse("oauth_return", kwargs={"service": "mapmyfitness"})}

        response = requests.post("https://api.mapmyfitness.com/v7.1/oauth2/access_token",
                                 data=urlencode(params),
                                 headers={"Content-Type": "application/x-www-form-urlencoded",
                                          "api-key": MAPMYFITNESS_CLIENT_KEY})

        if response.status_code != 200:
            raise APIException("Invalid code")
        token = response.json()["access_token"]

        uid = self._getUserId(ServiceRecord({"Authorization": {"Token": token}}))

        return (uid, {"Token": token})

    def RevokeAuthorization(self, serviceRecord):
        # there doesn't seem to be a way to revoke the token
        pass

    def _getActivityTypeHierarchy(self, headers):
        if hasattr(self, "_activityTypes"):
            return self._activityTypes
        response = requests.get("https://api.mapmyfitness.com/v7.1/activity_type", headers=headers)
        data = response.json()
        self._activityTypes = {}
        for actType in data["_embedded"]["activity_types"]:
            self._activityTypes[actType["_links"]["self"][0]["id"]] = actType
        return self._activityTypes

    def _resolveActivityType(self, actType, headers):
        self._getActivityTypeHierarchy(headers)
        if actType in self._activityMappings:
            return self._activityMappings[actType]
        activity = self._activityTypes[actType]
        parentLink = activity["_links"].get("parent")
        if parentLink is not None:
            parentId = parentLink[0]["id"]
            return self._resolveActivityType(parentId, headers)
        return ActivityType.Other

    def DownloadActivityList(self, serviceRecord, exhaustive=False):
        logger.debug("DownloadActivityList")
        allItems = []
        headers=self._apiHeaders(serviceRecord)
        nextRequest = '/v7.1/workout/?user=' + str(serviceRecord.ExternalID)
        while True:
            response = requests.get("https://api.mapmyfitness.com" + nextRequest, headers=headers)
            if response.status_code != 200:
                if response.status_code == 401 or response.status_code == 403:
                    raise APIException("No authorization to retrieve activity list", block=True, user_exception=UserException(UserExceptionType.Authorization, intervention_required=True))
                raise APIException("Unable to retrieve activity list " + str(response), serviceRecord)
            data = response.json()
            allItems += data["_embedded"]["workouts"]
            nextLink = data["_links"].get("next")
            if not exhaustive or not nextLink:
                break
            nextRequest = nextLink[0]["href"]

        activities = []
        exclusions = []
        for act in allItems:
            # TODO catch exception and add to exclusions
            activity = UploadedActivity()
            activityID = act["_links"]["self"][0]["id"]
            activity.StartTime = datetime.strptime(act["start_datetime"], "%Y-%m-%dT%H:%M:%S%z")
            activity.Notes = act["notes"] if "notes" in act else None

            # aggregate
            aggregates = act["aggregates"]
            elapsed_time_total = aggregates["elapsed_time_total"] if "elapsed_time_total" in aggregates else "0"
            activity.EndTime = activity.StartTime + timedelta(0, round(float(elapsed_time_total)))
            activity.Stats.TimerTime  = ActivityStatistic(ActivityStatisticUnit.Seconds, value=float(elapsed_time_total))
            activity.Stats.MovingTime = ActivityStatistic(ActivityStatisticUnit.Seconds, value=float(elapsed_time_total))
            if "active_time_total" in aggregates:
                activity.Stats.MovingTime = ActivityStatistic(ActivityStatisticUnit.Seconds, value=float(aggregates["active_time_total"]))

            if "distance_total" in aggregates:
                activity.Stats.Distance = ActivityStatistic(ActivityStatisticUnit.Meters, value=float(aggregates["distance_total"]))

            if "speed_min" in aggregates:
                activity.Stats.Speed.Min = float(aggregates["speed_min"])
            if "speed_max" in aggregates:
                activity.Stats.Speed.Max = float(aggregates["speed_max"])
            if "speed_avg" in aggregates:
                activity.Stats.Speed.Average = float(aggregates["speed_avg"])

            if "heartrate_min" in aggregates:
                activity.Stats.HR.update(ActivityStatistic(ActivityStatisticUnit.BeatsPerMinute, min=float(aggregates["heartrate_min"])))
            if "heartrate_max" in aggregates:
                activity.Stats.HR.update(ActivityStatistic(ActivityStatisticUnit.BeatsPerMinute, max=float(aggregates["heartrate_max"])))
            if "heartrate_avg" in aggregates:
                activity.Stats.HR = ActivityStatistic(ActivityStatisticUnit.BeatsPerMinute, avg=float(aggregates["heartrate_avg"]))

            if "cadence_min" in aggregates:
                activity.Stats.Cadence.update(ActivityStatistic(ActivityStatisticUnit.RevolutionsPerMinute, min=int(aggregates["cadence_min"])))
            if "cadence_max" in aggregates:
                activity.Stats.Cadence.update(ActivityStatistic(ActivityStatisticUnit.RevolutionsPerMinute, max=int(aggregates["cadence_max"])))
            if "cadence_avg" in aggregates:
                activity.Stats.Cadence = ActivityStatistic(ActivityStatisticUnit.RevolutionsPerMinute, avg=int(aggregates["cadence_avg"]))

            if "power_min" in aggregates:
                activity.Stats.Power.update(ActivityStatistic(ActivityStatisticUnit.Watts, min=int(aggregates["power_min"])))
            if "power_max" in aggregates:
                activity.Stats.Power.update(ActivityStatistic(ActivityStatisticUnit.Watts, max=int(aggregates["power_max"])))
            if "power_avg" in aggregates:
                activity.Stats.Power = ActivityStatistic(ActivityStatisticUnit.Watts, avg=int(aggregates["power_avg"]))

            activityTypeLink = act["_links"].get("activity_type")
            activityTypeID = activityTypeLink[0]["id"] if activityTypeLink is not None else None

            privacyLink = act["_links"].get("privacy")
            privacyID = privacyLink[0]["id"] if privacyLink is not None else None
            activity.Private = privacyID == "0"

            activity.Type = self._resolveActivityType(activityTypeID, headers)

            activity.ServiceData = {
                "ActivityID": activityID,
                "activityTypeID": activityTypeID,
                "privacyID": privacyID
                }
            activity.CalculateUID()
            activities.append(activity)
        return activities, exclusions

    def DownloadActivity(self, serviceRecord, activity):
        activityID = activity.ServiceData["ActivityID"]
        logger.debug("DownloadActivity %s" % activityID)

        lap = Lap(stats=activity.Stats, startTime=activity.StartTime, endTime=activity.EndTime)
        activity.Laps = [lap]
        lap.Waypoints = []

        response = requests.get("https://api.mapmyfitness.com/v7.1/workout/" + activityID + "/?field_set=time_series", headers=self._apiHeaders(serviceRecord))
        data = response.json()

        activity.GPS = False
        activity.Stationary = True

        # add waypoints to laps
        if "time_series" in data and "position" in data["time_series"]:
            activity.Stationary = False
            for pt in data["time_series"]["position"]:
                timestamp = pt[0]
                wp = Waypoint(activity.StartTime + timedelta(seconds=round(timestamp)))

                pos = pt[1]
                if ("lat" in pos and "lng" in pos) or "elevation" in pos:
                    wp.Location = Location()
                    if "lat" in pos and "lng" in pos:
                        wp.Location.Latitude = pos["lat"]
                        wp.Location.Longitude = pos["lng"]
                        activity.GPS = True
                    if "elevation" in pos:
                        wp.Location.Altitude = pos["elevation"]

                lap.Waypoints.append(wp)

        return activity

    def UploadActivity(self, serviceRecord, activity):

        if activity.Private:
            privacy_option_id = "0"
        else:
            privacy_option_id = "3"

        activity_type_id = [k for k,v in self._activityMappings.items() if v == activity.Type][0]
        if not activity_type_id:
            activity_type_id = "1"

        elapsed_time_total = activity.EndTime - activity.StartTime

        aggregates = {
            "elapsed_time_total": elapsed_time_total.seconds
        }

        if activity.Stats.Distance.Value is not None:
            aggregates["distance_total"] = activity.Stats.Distance.asUnits(ActivityStatisticUnit.Meters).Value

        if activity.Stats.TimerTime.Value is not None:
            aggregates["active_time_total"] = activity.Stats.TimerTime.asUnits(ActivityStatisticUnit.Seconds).Value
        elif activity.Stats.MovingTime.Value is not None:
            aggregates["active_time_total"] = activity.Stats.MovingTime.asUnits(ActivityStatisticUnit.Seconds).Value
        else:
            aggregates["active_time_total"] = (activity.EndTime - activity.StartTime).total_seconds()

        speed_stats = activity.Stats.Speed.asUnits(ActivityStatisticUnit.MetersPerSecond)
        if speed_stats.Average is not None:
            aggregates["speed_avg"] = speed_stats.Average
        if speed_stats.Min is not None:
            aggregates["speed_min"] = speed_stats.Min
        if speed_stats.Max is not None:
            aggregates["speed_max"] = speed_stats.Max

        hr_stats = activity.Stats.HR.asUnits(ActivityStatisticUnit.BeatsPerMinute)
        if hr_stats.Average is not None:
            aggregates["heart_rate_avg"] = hr_stats.Average
        if hr_stats.Min is not None:
            aggregates["heart_rate_max"] = hr_stats.Min
        if hr_stats.Max is not None:
            aggregates["heart_rate_max"] = hr_stats.Max

        if activity.Stats.Power.Average is not None:
            aggregates["power_avg"] = activity.Stats.Power.asUnits(ActivityStatisticUnit.Watts).Average
        if activity.Stats.Power.Min is not None:
            aggregates["power_min"] = activity.Stats.Power.asUnits(ActivityStatisticUnit.Watts).Min
        if activity.Stats.Power.Max is not None:
            aggregates["power_max"] = activity.Stats.Power.asUnits(ActivityStatisticUnit.Watts).Max

        if activity.Stats.Cadence.Average is not None:
            aggregates["cadence_avg"] = activity.Stats.Cadence.asUnits(ActivityStatisticUnit.RevolutionsPerMinute).Average
        elif activity.Stats.RunCadence.Average is not None:
            aggregates["cadence_avg"] = activity.Stats.RunCadence.asUnits(ActivityStatisticUnit.StepsPerMinute).Average

        if activity.Stats.Cadence.Min is not None:
            aggregates["cadence_min"] = activity.Stats.Cadence.asUnits(ActivityStatisticUnit.RevolutionsPerMinute).Min
        elif activity.Stats.RunCadence.Min is not None:
            aggregates["cadence_min"] = activity.Stats.RunCadence.asUnits(ActivityStatisticUnit.StepsPerMinute).Min

        if activity.Stats.Cadence.Max is not None:
            aggregates["cadence_max"] = activity.Stats.Cadence.asUnits(ActivityStatisticUnit.RevolutionsPerMinute).Max
        elif activity.Stats.RunCadence.Max is not None:
            aggregates["cadence_max"] = activity.Stats.RunCadence.asUnits(ActivityStatisticUnit.StepsPerMinute).Max

        # time series
        position = []
        heartrate = []
        power = []
        distance = []
        speed = []
        cadence = []
        for wp in activity.GetFlatWaypoints():
            time = wp.Timestamp - activity.StartTime
            time = time.seconds
            if wp.Location:
                pos = {}
                if wp.Location.Latitude is not None and wp.Location.Longitude is not None:
                    pos["lat"] = wp.Location.Latitude
                    pos["lng"] = wp.Location.Longitude
                if wp.Location.Altitude is not None:
                    pos["elevation"] = wp.Location.Altitude
                pt = [time, pos]
                position.append(pt)
            if wp.HR is not None:
                pt = [time, round(wp.HR)]
                heartrate.append(pt)
            if wp.Power is not None:
                pt = [time, round(wp.Power)]
                power.append(pt)
            if wp.Distance is not None:
                pt = [time, round(wp.Distance)]
                distance.append(pt)
            if wp.Speed is not None:
                pt = [time, round(wp.Speed)]
                speed.append(pt)
            if wp.Cadence is not None:
                pt = [time, round(wp.Cadence)]
                cadence.append(pt)
            elif wp.RunCadence is not None:
                pt = [time, round(wp.RunCadence)]
                cadence.append(pt)

        time_series = {}
        if position:
            time_series["position"] = position
        if heartrate:
            time_series["heartrate"] = heartrate
        if power:
            time_series["power"] = power
        if distance:
            time_series["distance"] = distance
        if speed:
            time_series["speed"] = speed
        if cadence:
            time_series["cadence"] = cadence

        upload_data = {
            "start_datetime": activity.StartTime.isoformat(),
            "start_locale_timezone": activity.TZ.zone,
            "name": activity.Name,
            "privacy": "/v7.1/privacy_option/%s/" % privacy_option_id,
            "activity_type": "/v7.1/activity_type/%s/" % activity_type_id,
            "aggregates": aggregates,
            "time_series": time_series
        }

        if activity.Notes:
            upload_data["notes"] = activity.Notes

        upload_resp = requests.post(
            "https://api.mapmyfitness.com/v7.1/workout/",
             headers=self._apiHeaders(serviceRecord),
             data=json.dumps(upload_data))
        if upload_resp.status_code != 201:
            raise APIException("Could not upload activity %s %s" % (upload_resp.status_code, upload_resp.text))

        return upload_resp.json()["_links"]["self"][0]["id"]

    def DeleteCachedData(self, serviceRecord):
        pass

    def DeleteActivity(self, serviceRecord, uploadId):
        pass


import time
import tushare as ts
import pandas as pd
import datetime, calendar
import sys

#reload(sys)
#sys.setdefaultencoding('utf-8')

class StockBasics:
	def __init__(self, df):
		self.code = df.code
		self.name = df.name
		self.industry = df.industry
		self.area = df.area
		self.pe = df.pe
		self.outstanding = df.outstanding
		self.totals = df.totals
		self.totalAssets = df.totalAssets
		self.liquidAssets = df.liquidAssets
		self.fixedAssets = df.fixedAssets
		self.reserved = df.reserved
		self.reservedPerShare = df.reservedPerShare
		self.esp = df.esp
		self.bvps = df.bvps
		self.pb = df.pb
		self.timeToMarket = df.timeToMarket
		self.undp = df.undp
		self.perundp = df.perundp
		self.rev = df.rev
		self.profit = df.profit
		self.gpr = df.gpr
		self.npr = df.npr
		self.holders = df.holders

def update_stock_base_info():
#	df = ts.get_concept_classified()
#	df.to_csv('./data/stock_concept.csv')

#	df = ts.get_industry_classified()
#	df.to_csv('./data/stock_industry.csv')

	df = ts.get_stock_basics()
	df.to_csv('./data/stock_basics.csv')
	#print df
	return df

def update_k_data():
	df = pd.read_csv('./data/stock_basics.csv')
	for i in range(len(df.index)):
		stock0 =  StockBasics(df.ix[i])
		k_data = ts.get_k_data("%06d"%stock0.code)
		k_data.to_csv("./data/%06d.csv"%stock0.code)
		#print "./data/%06d.csv"%stock0.code

def is_stock_suspended(k_data):
	k_date_len = len(k_data.index)
	last_date = k_data.ix[k_date_len-1].date
	last_datetime = datetime.datetime.strptime(last_date, "%Y-%m-%d")
	deltaDays=datetime.timedelta(days=10)
	now = datetime.datetime.now()
	if (now - last_datetime) > deltaDays:
		return True
	else:
		return False

def is_rise_stock(k_data, percent,  days):
	if len(k_data.index) < days:
		return False
	tail_data = k_data.tail(days)
	if tail_data.iloc[0].close > tail_data.iloc[-1].close:
		return False
	nums = 0.0
	for i in range(len(tail_data.index)):
		if i == 0:
			continue
		if tail_data.iloc[i].close > tail_data.iloc[i-1].close:
			nums = nums + 1
	if nums/days*100 >= percent:
		rise_percent = tail_data.iloc[-1].close/tail_data.iloc[0].close - 1
		print ("%06d %f(%f/%f)(rise:%f|%f,%f)"%(tail_data.iloc[i].code, nums/days, nums, days, rise_percent, tail_data.iloc[0].close, tail_data.iloc[-1].close))

		return True
	else:
		return False

def is_3xx_stock(k_data):
	if k_data.ix[0].code > 299999 and k_data.ix[0].code < 400000:
		return True
	else:
		return False

def is_small_stock(socket_base, value):
	if socket_base.totals < value:
		return True
	else:
		return False

def is_last_day_rise_stock(k_data):
	if k_data.iloc[len(k_data.index)-1].close > k_data.iloc[len(k_data.index)-1].open:
		return True
	else:
		return False

def is_volume_abnormal_stock(k_data, multiplier, days):
	if len(k_data.index) < 10:
		return False

	if days > len(k_data.index):
		days = len(k_data.index)

	tail_data = k_data.tail(days)

	for i in range(days-1):
#		print ("-----")
#		print (tail_data.iloc[i].volume)
#		print (tail_data.iloc[i+1].volume)
		if (tail_data.iloc[i].volume*multiplier < tail_data.iloc[i+1].volume):
			return True

	return False

def is_new_stock(k_data, days):
	data_len = len(k_data.index)
	if data_len <= days and data_len > 0:
		print (data_len)
		print (k_data)
		return True
	return False

def stock_with_concept(concept):
	df = pd.read_csv('./data/stock_concept.csv')
	slist = []
	for i in range(len(df.index)):
		if df.iloc[i].c_name == concept:
			slist.append(df.iloc[i].code)
			#print ("%06d"%df.iloc[i].code)
	return slist

def update_data():
	update_stock_base_info()
	update_k_data()

def choose_stocks():
	df = pd.read_csv('./data/stock_basics.csv')
	for i in df.index:
		stock0 =  StockBasics(df.ix[i])
		k_data = pd.read_csv("./data/%06d.csv"%stock0.code)
		#is_new_stock(k_data, 5)
		#if not is_new_stock(k_data, 5):
		#	continue
		if not is_volume_abnormal_stock(k_data, 2, 2):
			continue
		is_rise_stock(k_data, 65, 20)

def find_stock_with_newstock():
	slist_tmp = stock_with_concept("次新股")
	slist = []
	for code in slist_tmp:
		k_data = pd.read_csv("./data/%06d.csv"%code)
#		for

def find_stock_with_volume():
	df = pd.read_csv('./data/stock_basics.csv')
	for i in df.index:
		stock0 =  StockBasics(df.ix[i])
		k_data = pd.read_csv("./data/%06d.csv"%stock0.code)
		if (is_volume_abnormal_stock(k_data, 10, 20)):
			print("%06d"%stock0.code)

def red_green_array(code):
	str = ""
	k_data = pd.read_csv("./data/%06d.csv"%code)
	for i in range(len(k_data.index)):
		if k_data.iloc[i].close >= k_data.iloc[i].open:
			str = str + '1'
		else:
			str = str + '0'

#	print (str)
	return str

def find_stock_with_rga(rga):
	df = pd.read_csv('./data/stock_basics.csv')
	for i in df.index:
		stock0 = StockBasics(df.ix[i])
		k_data = pd.read_csv("./data/%06d.csv"%stock0.code)
		if len(k_data.index) > 66:
			continue
		str = red_green_array(stock0.code)
		if rga in str:
			print ("%06d"%stock0.code)

def debug():
	find_stock_with_rga("011101010010010001010001111100011100001011100111101010000111")

def start():
#	print (slist)
	print("start...")
#	update_data()
	find_stock_with_rga("1111111000")
#	debug()
#	find_stock_with_volume()

start()
#debug()


# -*- coding:utf-8 -*-
# @author :adolf
# !python3
import os
import cv2
import math
import torch
import numpy as np
from concern.config import Config, Configurable
import os

os.environ['CUDA_VISIBLE_DEVICES'] = "5"

params = {
    'exp': 'experiments/seg_detector/rctw17_resnet50_deform_thre.yaml',
    'resume': '/home/shizai/data3/adolf/ocr_hup/DB/output_aws/outputs/workspace/DB/SegDetectorModel-seg_detector/deformable_resnet50/L1BalanceCELoss_rctw200_x30/model/final',
    'image_short_side': 512,
    'thresh': 0.1,
    'box_thresh': 0.1,
    'resize': False,
    'polygon': False,
    'verbose': False
}

class SegDetector(object):
    def __init__(self, args=params):
        self.RGB_MEAN = np.array([122.67891434, 116.66876762, 104.00698793])

        conf = Config()
        experiment_args = conf.compile(conf.load(args['exp']))['Experiment']
        experiment_args.update(cmd=args)
        self.experiment = Configurable.construct_class_from_config(experiment_args)
        self.experiment.load('evaluation', **args)
        self.args = args
        self.structure = self.experiment.structure
        self.model_path = args['resume']

        self.init_torch_tensor()
        self.model = self.init_model()
        self.resume(self.model, self.model_path)
        self.model.eval()
        self.is_resize = args['resize']

    def init_torch_tensor(self):
        # Use gpu or not
        torch.set_default_tensor_type('torch.FloatTensor')
        if torch.cuda.is_available():
            self.device = torch.device('cuda')
            torch.set_default_tensor_type('torch.cuda.FloatTensor')
        else:
            self.device = torch.device('cpu')

    def init_model(self):
        model = self.structure.builder.build(self.device)
        return model

    def resume(self, model, path):
        if not os.path.exists(path):
            print("Checkpoint not found: " + path)
            return
        print("Resuming from " + path)
        states = torch.load(
            path, map_location=self.device)
        model.load_state_dict(states, strict=False)
        print("Resumed from " + path)

    def resize_image(self, img, is_resize=True):
        height, width, _ = img.shape
        if is_resize:
            if height < width:
                new_height = self.args['image_short_side']
                new_width = int(math.ceil(new_height / height * width / 32) * 32)
            else:
                new_width = self.args['image_short_side']
                new_height = int(math.ceil(new_width / width * height / 32) * 32)
            resized_img = cv2.resize(img, (new_width, new_height))
        else:
            if height < width:
                scale = int(height / 32)
                new_image_short_side = scale * 32
                new_height = new_image_short_side
                new_width = int(math.ceil(new_height / height * width / 32) * 32)
            else:
                scale = int(width / 32)
                new_image_short_side = scale * 32
                new_width = new_image_short_side
                new_height = int(math.ceil(new_width / width * height / 32) * 32)
            resized_img = cv2.resize(img, (new_width, new_height))
            print(new_height, new_width)
        return resized_img

    def load_image(self, img):
        original_shape = img.shape[:2]
        img = self.resize_image(img, is_resize=self.is_resize)
        img = img.astype(np.float32)
        img -= self.RGB_MEAN
        img /= 255.
        img = torch.from_numpy(img).permute(2, 0, 1).float().unsqueeze(0)
        return img, original_shape

    def predict(self, imgs):
        outputs = dict()
        for i, img in enumerate(imgs):
            batch = dict()
            timg, original_shape = self.load_image(img)
            batch['shape'] = [original_shape]
            batch['image'] = timg

            with torch.no_grad():
                pred = self.model.forward(batch, training=False)
                output = self.structure.representer.represent(batch, pred, is_output_polygon=self.args['polygon'])
                text_lines, scores = output
                text_lines = text_lines[0]
                scores = scores[0]
                new_tls = []
                new_scs = []
                for text_line, score in zip(text_lines, scores):
                    if len(text_line) == 0 or score < 0.2:
                        continue
                    new_tls.append(text_line)
                    new_scs.append(score)

                outputs[i] = []
                for j, (text_line, score) in enumerate(zip(new_tls, new_scs)):
                    one_output = dict()
                    one_output['text_line'] = text_line.tolist()
                    one_output['score'] = score
                    outputs[i].append(one_output)

                    # cut_image = self.cut_image(text_line, img)
                # cv2.imwrite("results_2/{}.jpg".format(j), cut_image)
                # part_imgs.append((cut_image, text_line.reshape(-1)))
                # outputs[i] = part_imgs
        return outputs

if __name__ == "__main__":
    # debug
    img = cv2.imread("images/2.png")
    img = img.astype(np.float32)
    detector = SegDetector()
    outp = detector.predict([img])
    print(outp)


import numpy as np
import scipy.signal as signal

z = np.array( [ -0.8, 1 ] )
p = np.array( [ 0.6 + 0.8j, 0.6 - 0.8j, -1 ] )
k = 0.8

b, a = signal.zpk2tf( z, p, k )

print( "Numerator Polynomial Coefficients =", b )
print( "Denominator Polynomial Coefficients =", a )


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

import os
import random
from glob import glob
import csv
import tensorflow as tf
import cv2

class Dataset:
    def __init__(self, datadir, org_height, org_width, org_depth=3, batch_size=32, threads_num=4, type=1, crop=False):
        if type == 1:
            # hollywood
            self.data = glob(os.path.join(datadir, "*.jpg"))
            self.train_csv = "train.csv"
            with open(self.train_csv, "w") as f:
                allnum = len(self.data)
                for i, image in enumerate(self.data):
                    try:
                        print("num: %d %d" % (i, allnum))
                        # img = cv2.imread(image)
                        # height, width, channels = img.shape
                        # if height/width > 1:
                        #     print("wrong image %s" % image)
                        #     continue
                    except Exception as ex:
                        print("broken image %s" % ex)
                        continue
                    f.write(image)
                    f.write("\n")
            print("dataset number: %d" % (len(self.data)))
            self.data_num = len(self.data)
            self.org_height = org_height
            self.org_width = org_width
            self.org_depth = org_depth
            self.threads_num = threads_num
            self.batch_size = batch_size
            self.batch_idxs = len(self.data) / self.batch_size
            self.crop = crop
        elif type == 2:
            # lfw
            self.data = glob(os.path.join(datadir, "*.jpg"))
            self.train_csv = "train.csv"
            with open(self.train_csv, "w") as f:
                for image in self.data:
                    try:
                        img = cv2.imread(image)
                    except Exception as ex:
                        print("broken image %s" % ex)
                        continue
                    f.write(image)
                    f.write("\n")
            print("dataset number: %d" % (len(self.data)))
            self.data_num = len(self.data)
            self.org_height = org_height
            self.org_width = org_width
            self.org_depth = org_depth
            self.threads_num = threads_num
            self.batch_size = batch_size
            self.batch_idxs = len(self.data) / self.batch_size
            self.crop = crop

    def _generate_image_and_label_batch(self, image, min_queue_examples):
        num_preprocess_threads = self.threads_num
        images = tf.train.shuffle_batch(
            [image],
            batch_size=self.batch_size,
            num_threads=num_preprocess_threads,
            capacity=min_queue_examples + 3 * self.batch_size,
            min_after_dequeue=min_queue_examples
        )
        # Display the training images in the visualizer
        # tf.image_summary('images', images, max_images=BATCH_SIZE)
        return images

    def get_inputs(self, input_height, input_width):
        filename_queue = tf.train.string_input_producer([self.train_csv], shuffle=True)
        reader = tf.TextLineReader()
        _, serialized_example = reader.read(filename_queue)
        filename = tf.decode_csv(serialized_example, [["path"]])
        jpeg = tf.read_file(filename[0])
        image = tf.image.decode_jpeg(jpeg, channels=3)

        if self.crop:
            image = tf.image.resize_images(image, [self.org_height, self.org_width])
            # resize to input size
            image = tf.image.crop_to_bounding_box(image, offset_height=20, offset_width=20, target_height=input_height, target_width=input_width)
            image = tf.cast(image, tf.float32)
            image = tf.image.resize_images(image, (input_height, input_width))
        else:
            image = tf.cast(image, tf.float32)
            image = tf.image.resize_images(image, (input_height, input_width))
        image = tf.subtract(tf.div(image, 127.5), 1.0)
        min_fraction_of_examples_in_queue = 0.4
        min_queue_examples = int(10000 * min_fraction_of_examples_in_queue)
        print ('filling queue with %d train images before starting to train. This will take a few minutes.' % min_queue_examples)
        return self._generate_image_and_label_batch(image, min_queue_examples)

def load_csv(path):
    print("load csv: %s" % (path))
    images = []
    with open(path, 'r') as f:
        rows = csv.reader(f)
        for row in rows:
            images.append(row)
    return images