import datetime

from pyalgotrade import bar
from pyalgotrade.barfeed import membf
from pyalgotrade import dataseries
from pyalgotrade.barfeed import csvfeed

# Example BarFeed for dataframes with data for a single instrument.
class DataFrameMulitpleBarFeed(membf.BarFeed):
    # @dataframes : Dict [key = instrument, value = df]
    # @instruments : key of Dict
    def __init__(self, dataframes, frequency,  timezone=None, maxLen=dataseries.DEFAULT_MAX_LEN):
        if isinstance(timezone, int):
            raise Exception("timezone as an int parameter is not supported anymore. Please use a pytz timezone instead.")

        if frequency not in [bar.Frequency.DAY, bar.Frequency.WEEK]:
            raise Exception("Invalid frequency.")

        membf.BarFeed.__init__(self, frequency, maxLen)
        #csvfeed.BarFeed.__init__(self, frequency, maxLen)
        self.__dfs = dataframes
        self.__instruments = dataframes.keys()
        self.__timezone = timezone
        self.__sanitizeBars = False
        self.__barFilter = None

    def getBarFilter(self):
        return self.__barFilter

    def setBarFilter(self, barFilter):
        self.__barFilter = barFilter

    def sanitizeBars(self, sanitize):
        self.__sanitizeBars = sanitize

    def barsHaveAdjClose(self):
        return True

    def addMultipleBars(self, timezone=None):
        if isinstance(timezone, int):
            raise Exception("timezone as an int parameter is not supported anymore. Please use a pytz timezone instead.")

        if timezone is None:
            timezone = self.__timezone

        for instrument in self.__instruments:
            # add one bars series into BarFeed
            self.addBarsFromSequence(instrument, self.newBasicBar(self.__dfs[instrument]))

    # put df data quotes into bar.BasicBars sequence
    # bars : sequence of bar.BasicBars
    def newBasicBar(self, df):
        bars = []
        for index in df.index :
            date = df["Date"][index]
            dateandtime = datetime.datetime(date.year, date.month, date.day)
            bars.append(bar.BasicBar(dateandtime,
                    df["Open"][index],
                    df["High"][index],
                    df["Low"][index],
                    df["Close"][index],
                    df["Volume"][index],
                    df["AdjClose"][index],
                    self.getFrequency()))
        return bars


import logging
import pandas as pd
from retry import retry

def read_sql_file(path_to_file: str) -> str:
    """
    Loading an SQL query from a file

    :param path_to_file: str, path to the file
    :return: str, SQL query as string
    """

    try:
        return open(path_to_file).read()
    except FileNotFoundError:
        logging.debug(f'FileNotFoundError: No such sql file {path_to_file}')
        raise FileNotFoundError(f'No such sql file {path_to_file}')

def prepare_sql_template(sql_template: str, test_parameters: dict) -> str:
    """
    Preparing SQL query containing parameters

    :param sql_template: str, SQL query
    :param test_parameters: dict, parameters to be parsed
    :return: str, query with parsed parameters
    """

    query = sql_template.format(**test_parameters).strip()

    return query

def run_query(query: str, limit: int = None, conn=None, **kwargs) -> pd.DataFrame:
    """
    Run a query with query and return the result as a pandas.DataFrame

    :param query: the query as a string.
    :param limit: add a limit parameter to the query.
    :param conn: the connection to be used.
    :param kwargs: a dictionary to attach any additional parameters to pd.read_sql (for example: parameters)
    :return a pd.DataFrame with the result of the query.
    """

    if conn is None:
        logging.debug('Exception: database connection is not established')
        raise Exception('database connection is not established')

    if limit:
        query += f' LIMIT {limit}'

    if kwargs:
        return pd.read_sql(query, conn, **kwargs)
    else:
        return pd.read_sql(query, conn)

@retry(tries=10)
def get_df_from_query(query_path: str, limit: int = None, conn=None, **kwargs) -> pd.DataFrame:
    """
    Run a query from an SQL file specified by query_path and return the result as a pandas.DataFrame

    :param query_path: the path to the SQL file containing the query
    :param limit: add a limit parameter to the query.
    :param conn: the connection to be used.
    :param kwargs: a dictionary to attach any additional parameters to pd.read_sql (for example: parameters)
    :return a pd.DataFrame with the result of the query.
    """

    query = read_sql_file(query_path)

    return run_query(query, limit=limit, conn=conn)


from django.contrib import admin
from .models import Product, Photo, Tag, Category

class PhotoInline(admin.StackedInline):
    model = Photo

class TagInline(admin.TabularInline):
    model = Tag.product_set.through

class CategoryInline(admin.TabularInline):
    model = Category.product_set.through

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    inlines = [PhotoInline, TagInline, CategoryInline]

admin.site.register(Photo)
admin.site.register(Tag)
admin.site.register(Category)


import os
import glob
import cv2
import PIL.Image as Image
import torchvision.transforms as transforms
from skimage.color import rgb2yuv
import numpy as np

if __name__ == '__main__':

    root1 = "E:/RoboCup/YOLO/Train/"
    root2 = "E:/RoboCup/FinetuneHorizon/train/"

    imgSize = (256,192)
    imSize = (192,256)

    resize = transforms.Resize(imSize)
    labResize = transforms.Resize(imSize,Image.NEAREST)

    imgs = sorted(glob.glob1(root1,"*.png"))
    labels = sorted(glob.glob1(root2,"*.png"))

    if len(labels) != len(imgs):
        for i in imgs:
            img = cv2.resize(cv2.imread(root1 + i), imgSize)
            cv2.imwrite(root1+i,img)
    else:
        for i,l in zip(imgs,labels):
            img = cv2.cvtColor(cv2.cvtColor(cv2.resize(cv2.imread(root1+i),(160,120)),cv2.COLOR_BGR2YUV),cv2.COLOR_BGR2RGB)
            #img = cv2.resize(cv2.imread(root1+i),imgSize)
            label = Image.open(root2+l).convert('I')
            label = labResize(label)
            label.save(root2+l)
            cv2.imwrite(root1+i,img)


import curses
import json
import textwrap
from telegramData import *
from VSplit import *
from HSplit import *
from ContactList import *
from ChatWindow import *
from InputWindow import *
from curses import ascii

mWin=curses.initscr()
curses.noecho()
curses.cbreak()
mWin.keypad(True)
curses.start_color()

curses.init_pair(1,curses.COLOR_CYAN,curses.COLOR_BLACK)
curses.init_pair(2,curses.COLOR_GREEN,curses.COLOR_BLACK)

leftRight=VSplit(mWin,10,10)

topBottom=HSplit(leftRight.right,1,1)

cl=ContactList(leftRight.left)
cl.loadList(liste)
cl.redraw()

cw=ChatWindow(topBottom.top)
cw.loadChat(chat)
cw.redraw()

iw=InputWindow(topBottom.bottom)
iw.redraw()

def close(errorText=""):
	curses.nocbreak()
	mWin.keypad(False)
	curses.echo()
	curses.endwin()
	if(errorText != ""):
		print(errorText)
	exit()

inp=0;

while(True):
	inp=mWin.getch()

	if(inp==curses.KEY_UP):
		cl.prevContact()
	elif(inp==curses.KEY_DOWN):
		cl.nextContact()
	elif(inp==curses.KEY_RESIZE):
		if(not leftRight.refresh()):
			close("error: window too small")
		if(not topBottom.refresh()):
			close("error: window to small")
		cw.redraw()
		cl.redraw()
		iw.redraw()
	elif(iw.processInput(inp)):
		iw.redraw()
	else:
		break
	if(not leftRight.refresh()):
		close("error: window too small")
	if(not topBottom.refresh()):
		close("error: window to small")
	mWin.refresh()

close(str(inp))


import fasttext
import pandas as pd, numpy as np
from sklearn.metrics import accuracy_score, precision_score
from sklearn.model_selection import train_test_split
'''
train = pd.read_csv('../data/train_set.csv')
test = pd.read_csv('../data/test_set.csv')

def func(x):
    return x.word_seg + " __label__" + str(x['class'])

train['word_seg'] = train.apply(func, axis = 1)

Y = train['class'].values
#fasttext_train = train.loc[:, ['word_seg']]

X_train, X_val, Y_train, Y_val = train_test_split(train, Y, test_size = 0.2)
fasttext_train = X_train.loc[:, ['word_seg']]
fasttext_val = X_val.loc[:, ['word_seg']]

fasttext_train.to_csv('tuning/fasttext_trainset.txt', index = None, header = None)
'''

clf = fasttext.supervised(input_file = 'tuning/fasttext_trainset.txt', output = 'tuning/fasttext_model', label_prefix = '__label__',
                         bucket = 2000000, word_ngrams = 1, ws = 20, epoch=40, silent = 0)

train_data = X_train.word_seg.values.tolist()
val_data = X_val.word_seg.values.tolist()

res = clf.predict(train_data)
y_pred = []
for i in res:
    y_pred.append(int(i[0]))
train_acc = accuracy_score(Y_train, y_pred)

res = clf.predict(val_data)
y_pred = []
for i in res:
    y_pred.append(int(i[0]))
val_acc = accuracy_score(Y_val, y_pred)

print(train_acc, val_acc)

clf = fasttext.supervised(input_file = 'fasttext_trainset.txt', output = 'fasttext_model', label_prefix = '__label__',
                         bucket = 2000000, word_ngrams = 2, ws = 20, epoch=40, silent = 0)

#test_data = test.word_seg.values.tolist()
test = pd.read_csv('../data/test_set.csv')
test_data = test.word_seg.values.tolist()

res = clf.predict(test_data)
y_pred = []
for i in res:
    y_pred.append(int(i[0]))

test_pred = pd.DataFrame(columns = ['id', 'class'])
test_pred.id = test.id.values
test_pred['class'] = (y_pred)
test_pred.to_csv('../sub/sub_fasttext_baseline.csv',index=None)


import arrow
from src.ec2 import EC2

class Snapshots(EC2):

    def list(self, Filters=[]):
        try:
            self.snaps = list(self.ec2.snapshots.filter(OwnerIds=['self'],Filters=Filters))
        except Exception as e:
            print('error: failed to list snapshots')
            print(e)
            self.snaps = []
        return self

    def expired(self, days_old=15):
        self.delete_snaps = []
        delete_time = self.get_delete_time(days_old)
        for snap in self.snaps:
            if snap.start_time < delete_time:
                self.delete_snaps.append(snap)
                # print('expired snapshot: %s, %s' % (snap.id, snap.start_time))
        print()
        print("---------------------------------------------")
        print(len(self.delete_snaps), " expired snapshots.")
        print("---------------------------------------------")
        print()
        return self

    def delete(self, DryRun=True):
        for snap in self.delete_snaps:
            try:
                snap.delete(DryRun)
                print('success: deleted snapshot (dryrun=%s) : %s ' % (DryRun, snap.id))
            except Exception as e:
                print('error: failed to delete snapshot %s: %s' % (snap.id, e))


#recursions in two type:-1.iterative approach
#                        -2.recursive approach
#1 .....
"""
i=0
def iter_app(a):
        for i in range(a):
           fac=1
           fac=fac*(i+1)
           return fac
           i+=1

number=int(input("enter the num:"))
print(iter_app(number))
"""
#2 ....
from math import factorial

def rec_1(n):
    if(n==1):
        return 1
    else:
        return n * factorial(n-1)
number= int(input("enter the number:"))
print(rec_1(number))


from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse

bot_router = APIRouter()
from config.settings import templates
from config.logger import logging

@bot_router.get("/policy", response_class=HTMLResponse)
async def add_user_workouts(request: Request):
    # logging.debug(f"Что в запросе? {await request.body()}")
    return templates.TemplateResponse(
        name="policy.html",
        context={"request": request}
    )

@bot_router.get("/")
async def root():
    return {"message": "Hello World"}


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
    path('slider', views.about),
    path('old', views.old),
    path('2_21', views.page2_21),
    path('3_3', views.page3_3),
    path('3_4', views.page3_4),
    path('3_7', views.page3_7)
    ]


count = 0
def n_queens(n, matrix, row):
    global count
    if row == n:
        print(matrix)
        count += 1
        return

    for i in range(n):
        if is_safe(row, i, matrix):
            matrix[row][i] = 1
            n_queens(n, matrix, row+1)
        matrix[row][i] = 0

def is_safe(row, col, matrix):
    i = row-1
    j = col
    while i >= 0:
        if matrix[i][j] == 1:
            return False
        i -= 1

    i = row-1
    j = col-1
    while j >= 0 and i >= 0:
        if matrix[i][j] == 1:
            return False
        i -= 1
        j -= 1

    i = row-1
    j = col+1
    while j < len(matrix) and i >= 0:
        if matrix[i][j] == 1:
            return False
        i -= 1
        j += 1

    return True

n = int(input())
matrix = [[0] * n for i in range(n)]
n_queens(n, matrix, 0)
print(count)


#!/usr/bin/env python
import _strptime
from github import Github
from github.GithubException import UnknownObjectException
import os
import sys
import traceback
import time

MAX_PR_WALKING=int(os.environ.get('MAX_PR_WALKING', '5'))

print('Running {}'.format(' '.join(sys.argv)))

github = Github(os.environ['GITHUB_OAUTH_USER'], os.environ['GITHUB_OAUTH_TOKEN'])
repo = github.get_repo(os.environ['TRAVIS_REPO_SLUG'])

release_name = os.environ['PROJECT_VERSION']
release_tag = 'v' + release_name
release_body = os.environ['TRAVIS_COMMIT_MESSAGE']
commit_id = os.environ['TRAVIS_COMMIT']

draft_release = os.environ.get('GITHUB_DRAFT_RELEASE', '0')
pre_release = os.environ.get('GITHUB_PRERELEASE', '1')

is_draft_release = draft_release == '1' or draft_release.lower() == 'true'
is_prerelease = pre_release == '1' or pre_release.lower() == 'true'

print('release_tag={}, commit_id={}'.format(release_tag, commit_id))

pr = None

pr_number = os.environ.get('TRAVIS_PULL_REQUEST', 'false')
if pr_number and pr_number != 'false':
    try:
        pr = repo.get_pull(int(pr_number))
    except ValueError:
        pass

def find_pr():
    all_pulls = [pr for pr in repo.get_pulls(state='closed', sort='updated')]
    all_pulls.reverse()
    for index, merged_pr in enumerate(all_pulls):
        if index >= MAX_PR_WALKING:
            break
        if merged_pr.merge_commit_sha == commit_id:
            return merged_pr
    return None

if not pr:
    retry = 0
    while not pr and retry < 5:
        time.sleep(retry * 10)
        try:
            pr = find_pr()
            break
        except:
            traceback.print_exc()
            retry += 1

if pr:
    release_body='''## {}
{}

### Commit message:
```
{}
```
'''.format(pr.title, pr.body, release_body)

release = None
try:
    release = repo.get_release(release_tag)
except UnknownObjectException:
    release = repo.create_git_release(release_tag, release_name, release_body, draft=is_draft_release, prerelease=is_prerelease, target_commitish=commit_id)

print('Release created/updated: ' + release_tag)
for asset in sys.argv[1:]:
    print(' - deploying asset: ' + asset)
    release.upload_asset(asset)


# -*- coding: utf-8 -*-

from math import *

def convert(a):
  if abs(a) > 360 :
    a = a%360
  if a < 0 :
    a += 360
  return a

def m_vektor(p1, p2):
  m = (p2[0]-p1[0])**2+(p2[1]-p1[1])**2
  return m**0.5

def mv(v):
  return (v[0]**2+v[1]**2)**0.5

def c_vektor(p1, p2):
  x = p1[0]+(p2[0]-p1[0])/2
  y = p1[1]+(p2[1]-p1[1])/2
  return x, y

def ln_vektor(p1, p2, ln):
  x1, y1 = p1
  x2, y2 = p2
  ab = m_vektor(p1, p2)
  k = ln/ab
  x, y = x2-x1, y2-y1
  return x*k, y*k

def sum_v_in_point(x1,y1,x2,y2):
  return x2+(x1-x2)/2,y2+(y1-y2)/2

def c_okr(r,a=360):
  return pi*r/180*a
def s_okr(r,a=360):
  return pi*r**2/360*a

def init_lists():
  global sin_list,cos_list,tan_list
  sin_list = [sct(a,1) for a in range(361)]
  cos_list = [sct(a,2) for a in range(361)]
  tan_list = [sct(a,3) for a in range(361)]
  tan_list[90] = tan_list[270] = None

def sct(a, typ=1):
  rad = pi*a/180
  if typ == 1 :
    if a in (180, 360):
      return 0
    return sin(rad)
  elif typ == 2 :
    if a in (90, 270):
      return 0
    return cos(rad)
  elif typ == 3 :
    if a in (180, 360):
      return 0
    return tan(rad)
  elif typ == 4 :
    tn = sct(a, 3)
    if tn == 0 :
      return None
    return 1/sct(a, 3)

def asct(x, typ=1):
  if   typ == 1 :
    sct_list = sin_list
  elif typ == 2 :
    sct_list = cos_list
  elif typ == 3 :
    sct_list = tan_list
  if x in sct_list :
    a = sct_list.index(x)
    if a > 180 :
      a = 360-a
    return a
  for a in range(360):
    if sct_list[a] < x < sct_list[a+1] :
      if a > 180 :
        a = 360 - a
      return a

def gip(a,b,c=None):
  if not c :
    return (a**2+b**2)**0.5
  if not a :
    return (c**2-b**2)**0.5
  if not b :
    return (c**2-a**2)**0.5

def v_to_func_from_x(v,x):
  return x*v[1]/v[0]
def v_to_func_from_y(v,y):
  return y*v[0]/v[1]

def move(angle,ln):
  x, y = ln*sct(angle, 2), ln*sct(angle)
  return [x, y]

def angle_to_point(pos1, pos2):
  x1, y1 = pos1
  x2, y2 = pos2
  ln = m_vektor(pos1, pos2)
  sin = abs(y2-y1)/ln
  a = asct(sin)
  if y1 < y2 :
    a = -a
  if 0 < a < 90 and x1 > x2 :
    a = 180-a
  return convert(a)

def get_func(p1, p2, type=1):
  angle = angle_to_point(p1, p2)
  if angle in (90, 270):
    if type == 1:
      return None
    else:
      return lambda x:  p1[0]
  elif angle in (0, 180):
    if type == 1:
      return lambda x : p1[1]
    else:
      return None
  a = float(p2[1]-p1[1])/float(p2[0]-p1[0])
  b = p1[1]-a*p1[0]
  if type == 1 :
    return lambda x : a*x+b
  else:
    return lambda y : (y-b)/a

def point_in_lines(p1, p2, p3, p4):
  if p1[0] == p2[0]:
    x = p1[0]
    y = get_func(p3, p4)(x)
    return x, y
  elif p3[0] == p4[0]:
    x = p3[0]
    y = get_func(p1, p2)(x)
    return x, y
  else:
    a1 = (p2[1]-p1[1])/(p2[0]-p1[0])
    b1 = p1[1]-a1*p1[0]
    a2 = (p4[1]-p3[1])/(p4[0]-p3[0])
    b2 = p3[1]-a2*p3[0]
    if a1 == a2:
      return None
    x = (b2-b1)/(a1-a2)
    y = a1*x+b1
    return x, y

# an = 2Rsin(180/n)
# r = Rcos(180/n)
# a = 2Rsin(pi/n) = 2rtg(pi/n)

global sin_list, cos_list, tan_list
init_lists()


#精装
from common.myunit import StartEnd
from businessView.hardcover import Hardcover
import unittest
import logging

class TestHardcover(StartEnd):
    # @unittest.skip('test_todayrecomend')
    def test_todayrecomend(self):
        logging.info('======test_todayrecomend=====')
        h = Hardcover(self.driver)
        try:
            self.assertTrue(h.todayrecomend())
        except BaseException as error:
            self.add_img()
            raise error

    # @unittest.skip('test_heavyfine')
    def test_heavyfine(self):
        logging.info('======test_heavyfine=====')
        h = Hardcover(self.driver)
        try:
            self.assertTrue(h.heavyfine())
        except BaseException as error:
            self.add_img()
            raise error

    # @unittest.skip('test_newalbum')
    def test_newalbum(self):
        logging.info('======test_newalbum=====')
        h = Hardcover(self.driver)
        try:
            self.assertTrue(h.newalbum())
        except BaseException as error:
            self.add_img()
            raise error

    # @unittest.skip('test_buyalbums')
    def test_buyalbums(self):
        logging.info('======test_buyalbums=====')
        h = Hardcover(self.driver)
        try:
            self.assertTrue(h.buyalbums())
        except BaseException as error:
            self.add_img()
            raise error

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


import os
import numpy as np
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import label_binarize
import os
import sys

arg_dir = sys.argv[1]

path_directory='/home/SharedData/Ashvini/ablation'

def read_numpy_concat_mean(path):

	k=os.listdir(path)
	count=0
	k.sort()
	for ele in k:
		#print(ele)

		current_file=np.load(path+ele)
		#np.mean(curren_file)
		#np.std(curren_file)

		element=current_file

		if(ele=='14_0017.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(74,120))+list(range(389,436))+list(range(863,910))+list(range(930,1000)))
			#print(element.shape)

		if(ele=='14_0018.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(271,319))+list(range(722,763)))
			#print(element.shape)

		if(ele=='14_0019.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(292,340)))
			#print(element.shape)

		if(ele=='14_0022.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(560,624))+list(range(813,1006)))
			#print(element.shape)

		if(ele=='14_0032.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(727,739)))
			#print(element.shape)

		if(np.sum(element)>0):

			element=(element-np.min(element))/(np.max(element)-np.min(element))

			#element=element-np.mean(element)

			#element=(element-np.mean(element))/(np.std(element-np.mean(element)))

		else:

			element = np.zeros(element.shape)

		#print('Element Shape :: ',element.shape)

		if(count==0):
			p=element
			count=count+1
		else:
			p=np.concatenate((p,element))

		#print(p.shape)

	return p

def read_numpy_gt(path):

	k=os.listdir(path)
	count=0
	k.sort()
	for ele in k:
		#print(ele)
		current_file=np.load(path+ele)

		#np.mean(curren_file)
		#np.std(curren_file)

		element=current_file #(current_file-np.mean(current_file))/np.std(current_file)

		if(ele=='14_0017.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(74,120))+list(range(389,436))+list(range(863,910))+list(range(930,1000)))
			#print(element.shape)

		if(ele=='14_0018.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(271,319))+list(range(722,763)))
			#print(element.shape)

		if(ele=='14_0019.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(292,340)))
			#print(element.shape)

		if(ele=='14_0022.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(560,624))+list(range(813,1006)))
			#print(element.shape)

		if(ele=='14_0032.npy'):
			#print('IT IS TRUE')
			element=np.delete(element, list(range(727,739)))
			#print(element.shape)

		if(count==0):
			p=element
			count=count+1
		else:
			#print('pshape :: ',p.shape)
			#print('element shape :: ',element.shape)
			p=np.concatenate((p.reshape(p.shape[0],1),element.reshape(element.shape[0],1)))

		#print(p.shape)

	return p

path=path_directory+'/accv20/test/final_mse/'+str(arg_dir)+'/'
gt_path=path_directory+'/ground_truth/'

concat_all_video=read_numpy_concat_mean(path)

###########################Concat_all_video is already normalised between 0 and 1##########
concat_all_video[concat_all_video<0]=0

x=np.int32(concat_all_video>0) ########If mse is 0, then 0 otherwise 1, kind of binarisisng
x[x==0]=1

final_scores=concat_all_video/x
final_scores[np.isnan(final_scores)]=0

concat = concat_all_video.reshape((concat_all_video.shape[0],1))

gt_all_video=read_numpy_gt(gt_path)

y = label_binarize(np.int32(gt_all_video),classes=[0,1])
final_scores=final_scores.reshape((final_scores.shape[0],1))

auc_value=roc_auc_score(y, final_scores)
print('auc_score : {}'.format(auc_value))

save = np.array((auc_value))
#np.save('AUC_Epoch_'+epoch_num+'.npy',save)


# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exceptions import DropItem
import re

class ListExtractionPipeline(object):
    def process_item(self, item, spider):
        for key in item.keys():
            if (item[key]):
                if (type(item[key]) is list):
                    length = len(item[key])
                    if (length > 0):
                        if key not in item.__class__.LIST_KEYS:
                            item[key] = item[key][0]
                        spider.crawler.stats.inc_value(key + '_list_len_' + str(length))
                    else:
                        item[key] = None
                        spider.crawler.stats.inc_value(key + '_empty_list')
                else:
                    spider.crawler.stats.inc_value(key + '_not_list')
            else:
                item[key] = None
                spider.crawler.stats.inc_value(key + '_no_value')
        return item

class NamePipeline(object):
    def process_item(self, item, spider):
        if (item['name'] is None):
            raise DropItem("Missing name")
        return item

class SeenNamePipeline(object):
    def __init__(self):
        self.name_seen = set()

    def process_item(self, item, spider):
        if (item['name'] in self.name_seen):
            raise DropItem("Duplicate name %s" % item)
        self.name_seen.add(item['name'])
        return item

class TelephoneSanitizePipeline(object):
    def process_item(self, item, spider):
        item = self.helper(item, 'telephone', spider)
        item = self.helper(item, 'fax', spider)
        return item

    def helper(self, item, field, spider):
        if (field in item) and (item[field] is not None):
            telephone = item[field]
            phone_list = telephone.split(',')
            if len(phone_list) == 1:
                phone_list = phone_list[0].split('/')
            phone_result = []
            for phone in phone_list:
                phone = phone.replace('(852)', '')
                phone = phone.replace('852-', '')
                phone = re.sub(r"\D", "", phone)
                if len(phone) != 8:
                    spider.crawler.stats.inc_value('invalid_' + field)
                phone_result.append(phone)
            item[field] = phone_result
        return item


import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

class ImageWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(ImageWidget, self).__init__(parent)

        # Interpret image data as row-major instead of col-major
        pg.setConfigOptions(imageAxisOrder='row-major')

        pg.mkQApp()
        self.win = pg.GraphicsLayoutWidget()
        self.win.setWindowTitle('pyqtgraph example: Image Analysis')

        # A plot1 area (ViewBox + axes) for displaying the image
        #self.plot1 = self.win.addItem() # addPlot()

        self.vb = pg.ViewBox(enableMouse=True, enableMenu=True)

        # Item for displaying image data
        self.item = pg.ImageItem()
        self.vb.addItem(self.item)
        self.win.addItem(self.vb, row=0, col=1)

        #self.vb.setAspectLocked()
        #self.vb.setRange(xRange=(0, 10), yRange=(0, 10))

        # Item for x-axis
        self.win.nextRow()
        self.xaxis = MyStringAxis(orientation="bottom", linkView=self.vb)
        self.xaxis.setLabel(text="x axis", units="arb.")
        self.yaxis = pg.AxisItem(orientation="left", linkView=self.vb)
        self.yaxis.setLabel(text="y axis", units="arb.")
        self.win.addItem(self.xaxis, row=1, col=1)
        self.win.addItem(self.yaxis, row=0, col=0)

        # Custom ROI for selecting an image region
        self.ROI = pg.ROI([-8, 14], [6, 5])
        self.ROI.addScaleHandle([0.5, 1], [0.5, 0.5])
        self.ROI.addScaleHandle([0, 0.5], [0.5, 0.5])
        self.vb.addItem(self.ROI)
        self.ROI.setZValue(10)  # make sure ROI is drawn above image

        # Isocurve drawing
        self.iso = pg.IsocurveItem(level=0.8, pen='g')
        self.iso.setParentItem(self.item)
        self.iso.setZValue(5)

        # Contrast/color control
        self.hist = pg.HistogramLUTItem()
        self.hist.setImageItem(self.item)
        self.win.addItem(self.hist, row=0, col=2)

        # Draggable line for setting isocurve level
        self.isoLine = pg.InfiniteLine(angle=0, movable=True, pen='g')
        self.hist.vb.addItem(self.isoLine)
        self.hist.vb.setMouseEnabled(y=False) # makes user interaction a little easier
        self.isoLine.setValue(0.8)
        self.isoLine.setZValue(1000) # bring iso line above contrast controls

        # Another plot1 area for displaying ROI data
        self.plot2 = self.win.addPlot(row=2, col=0, colspan=2)
        self.plot2.setMaximumHeight(250)
        self.win.resize(800, 800)
        self.win.show()

        # Generate image self.data
        self.data = np.random.normal(size=(200, 100))
        self.data[20:80, 20:80] += 2.
        self.data = pg.gaussianFilter(self.data, (3, 3))
        self.data += np.random.normal(size=(200, 100)) * 0.1
        self.item.setImage(self.data)
        self.hist.setLevels(self.data.min(), self.data.max())

        # build isocurves from smoothed self.data
        self.iso.setData(pg.gaussianFilter(self.data, (2, 2)))

        # set position and scale of image
        #self.item.scale(0.2, 0.2)
        #self.item.translate(-50, 0)

        ticks = np.arange(self.data.shape[1])
        tickDictMajor = []
        for i, tick in enumerate(ticks):
            tickDictMajor.append(str(i**2))
        self.xaxis.set_tick_dict(dict(enumerate(tickDictMajor)))

        #strings = self.xaxis.tickValues()

        # zoom to fit imageo
        #self.vb.autoRange()

        self.ROI.sigRegionChanged.connect(self.updatePlot)
        self.updatePlot()

        self.isoLine.sigDragged.connect(self.updateIsocurve)

    # Callbacks for handling user interaction
    def updatePlot(self):
        selected = self.ROI.getArrayRegion(self.data, self.item)
        self.plot2.plot(selected.mean(axis=0), clear=True)

    def updateIsocurve(self):
        self.iso.setLevel(self.isoLine.value())

class MyStringAxis(pg.AxisItem):
    def __init__(self, *args, **kwargs):
        pg.AxisItem.__init__(self, *args, **kwargs)
        self.x_values = None
        self.x_strings = None

    def set_tick_dict(self, xdict):

        self.x_values = np.asarray(list(xdict.keys())) + 0.5
        self.x_strings = np.asarray(list(xdict.values()))

    def tickStrings(self, values, scale, spacing):
        strings = []
        for v in values:
            # vs is the original tick value
            vs = v * scale
            # if we have vs in our values, show the string
            # otherwise show nothing
            if vs in self.x_values:
                # Find the string with x_values closest to vs
                vstr = self.x_strings[np.abs(self.x_values - vs).argmin()]
            else:
                vstr = ""
            strings.append(vstr)
        return strings

    def tickSpacing(self, minVal, maxVal, size, offset=0.5):
        """Return values describing the desired spacing and offset of ticks.

        This method is called whenever the axis needs to be redrawn and is a
        good method to override in subclasses that require control over tick locations.

        The return value must be a list of tuples, one for each set of ticks::

            [
                (major tick spacing, offset),
                (minor tick spacing, offset),
                (sub-minor tick spacing, offset),
                ...
            ]
        """
        # First check for override tick spacing
        if self._tickSpacing is not None:
            return self._tickSpacing

        dif = abs(maxVal - minVal)
        if dif == 0:
            return []

        ## decide optimal minor tick spacing in pixels (this is just aesthetics)
        optimalTickCount = max(2., np.log(size))

        ## optimal minor tick spacing
        optimalSpacing = dif / optimalTickCount

        ## the largest power-of-10 spacing which is smaller than optimal
        p10unit = 10 ** np.floor(np.log10(optimalSpacing))

        ## Determine major/minor tick spacings which flank the optimal spacing.
        intervals = np.array([1., 2., 10., 20., 100.]) * p10unit
        minorIndex = 0
        while intervals[minorIndex + 1] <= optimalSpacing:
            minorIndex += 1

        levels = [
            (intervals[minorIndex + 2], offset),
            (intervals[minorIndex + 1], offset),
            # (intervals[minorIndex], offset)    ## Pretty, but eats up CPU
        ]

        if self.style['maxTickLevel'] >= 2:
            ## decide whether to include the last level of ticks
            minSpacing = min(size / 20., 30.)
            maxTickCount = size / minSpacing
            if dif / intervals[minorIndex] <= maxTickCount:
                levels.append((intervals[minorIndex], offset))

        return levels

## Start Qt event loop unless running in interactive mode or using     pyside.
if __name__ == '__main__':

    app = QtGui.QApplication([])
    app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))

    image_widget = ImageWidget()

    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore,     'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()


from issueless.models import db, Notification, Project, User

def test_avatar(app):
    user = User.query.filter_by(email='test1@gmail.com').first()
    assert user.avatar() == (
        'https://www.gravatar.com/avatar/245cf079454dc9a3374a7c076de247cc?d='
        'identicon&s=68'
    )

def test_add_project(app):
    user = User.query.get(1)
    user.add_project(
        Project(title='test_title_4', description='test_description_4'), 'Admin'
    )
    db.session.commit()

    new_project = Project.query.get(4)
    assert new_project.title == 'test_title_4'
    assert new_project.description == 'test_description_4'

    user_project = user.user_projects.filter_by(project_id=4).first()
    role = user_project.role
    assert role.name == 'Admin'

def test_add_notification(app):
    user = User.query.get(2)

    old_timestamp = Notification.query.get(2).timestamp
    user.add_notification('invitation', {}, 3)
    assert Notification.query.get(2) is None
    notification = Notification.query.get(3)
    assert notification is not None
    assert notification.timestamp > old_timestamp

    for i in range(2, 51):
        user.add_notification('test', {})
    assert user.notifications.count() == 50
    user.add_notification('test', {})
    assert user.notifications.count() == 50
    assert Notification.query.get(2) is None


import ansidocs.main as ansidocs
import argparse
from os import path
import logging

logger = logging.getLogger(__name__)

def parseArgs():
    parser = argparse.ArgumentParser()
    parser.add_argument('--verbose', '-v', dest="verbose", action='count', default=0,
                        help="Increase program verbosity, use multiple 'v' for more logging")
    parser.add_argument('--quiet', '-q', dest="quiet", action='store_true',
                        help="Only display errors")

    parser.add_argument("-c", "--config-dir", dest="config_dir",
                        help="Path to the config dir",
                        default=path.join(path.expanduser('~'), '.ansidocs'))

    subparsers = parser.add_subparsers(title='subcommands',
                                       description='valid subcommands',
                                       help='additional help')
    gen_parser = subparsers.add_parser("generate", aliases=["gen"], help="Generate or update an existing readme")
    gen_parser.add_argument("-d", "--directory", dest="directory", default=".",
                            help="Directory where readme should be generated")
    gen_parser.add_argument("-l", "--layout", dest="force_layout", required=False,
                            help="Force a layout for the project specified. Default is to let the program auto " +
                            "determine the layout")

    test_parser = subparsers.add_parser("test", help="Test if the existing readme matches the expected readme")

    conf_parser = subparsers.add_parser("config", help="Create or refresh your config directory")
    conf_parser.add_argument("-r", "--refresh-configs", dest="refresh_configs",
                             help="If flag is set, config dir will be removed and recreated with defaults",
                             action='store_true')

    gen_parser.set_defaults(func=gen)
    conf_parser.set_defaults(func=conf)
    test_parser.set_defaults(func=test)

    return parser.parse_args()

def determine_log_level(verbose: int, starting_level: str = "WARNING"):
    # https://docs.python.org/3/library/logging.html#levels
    verbose_level_int = verbose * 10
    calculated_level = logging.getLevelName(starting_level) - verbose_level_int
    if calculated_level < 10:
        calculated_level = 10   # this is the minimum we support, debug
    return logging.getLevelName(calculated_level)

def main():
    args = parseArgs()
    if args.quiet:
        # override any other logging configs
        logging.basicConfig(level=logging.ERROR)
    args.func(args)

def gen(args):
    logging.basicConfig(level=determine_log_level(args.verbose))
    ansidocs.generate(
        directory=args.directory,
        config_dir=args.config_dir,
        force_layout=args.force_layout
    )

def conf(args):
    logging.basicConfig(level=determine_log_level(args.verbose, starting_level="INFO"))
    ansidocs.init_config_dir(
        config_dir=args.config_dir,
        refresh_configs=args.refresh_configs
    )

def test():
    pass