#!/usr/bin/python
#coding:utf-8
#@ZHOU_YING
#2018-12-11
#alle,threshold,Amp,AP_width,spike_half_width统计

import numpy as np
import os
import matplotlib.pyplot as plt
import pandas as pd
import efel
from collections import defaultdict

def save_threshold(sweep_id):
	threshold_array=threshold1[sweep_id]['AP_begin_voltage']
	threshold_array=np.append(threshold_array,[])
	index1=len(threshold_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		threshold.loc[sweep_id+idx1,spike_index]=threshold_array[n]
	#threshold.to_csv('threshold.csv',index=False)
	return

def save_AP_amp(sweep_id):
	AP_amp_array=AP_amp1[sweep_id]['AP_amplitude']
	AP_amp_array=np.append(AP_amp_array,[])
	index1=len(AP_amp_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_amp.loc[sweep_id+idx2,spike_index]=AP_amp_array[n]
	#AP_amp.to_csv('AP_amp.csv',index=False)
	return

def save_AP_width(sweep_id):
	AP_width_array=AP_width1[sweep_id]['AP_width']
	AP_width_array=np.append(AP_width_array,[])
	index1=len(AP_width_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_width.loc[sweep_id+idx3,spike_index]=AP_width_array[n]
	#AP_width.to_csv('AP_width.csv',index=False)
	return

def save_spike_half_width(sweep_id):
	spike_half_width_array=spike_half_width1[sweep_id]['spike_half_width']
	spike_half_width_array=np.append(spike_half_width_array,[])
	index1=len(spike_half_width_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		spike_half_width.loc[sweep_id+idx4,spike_index]=spike_half_width_array[n]
	#spike_half_width.to_csv('spike_half_width.csv',index=False)
	return

def save_AP_amplitude_change(sweep_id):
	AP_amplitude_change_array=AP_amplitude_change1[sweep_id]['AP_amplitude_change']
	AP_amplitude_change_array=np.append(AP_amplitude_change_array,[])
	index1=len(AP_amplitude_change_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_amplitude_change.loc[sweep_id+idx5,spike_index]=AP_amplitude_change_array[n]
	return

def save_AP_rise_time(sweep_id):
	AP_rise_time_array=AP_rise_time1[sweep_id]['AP_rise_time']
	AP_rise_time_array=np.append(AP_rise_time_array,[])
	index1=len(AP_rise_time_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_rise_time.loc[sweep_id+idx6,spike_index]=AP_rise_time_array[n]
	return

def save_AP_fall_time(sweep_id):
	AP_fall_time_array=AP_fall_time1[sweep_id]['AP_fall_time']
	AP_fall_time_array=np.append(AP_fall_time_array,[])
	index1=len(AP_fall_time_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_fall_time.loc[sweep_id+idx7,spike_index]=AP_fall_time_array[n]
	return

def save_AP_rise_rate(sweep_id):
	AP_rise_rate_array=AP_rise_rate1[sweep_id]['AP_rise_rate']
	AP_rise_rate_array=np.append(AP_rise_rate_array,[])
	index1=len(AP_rise_rate_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_rise_rate.loc[sweep_id+idx8,spike_index]=AP_rise_rate_array[n]
	return

def save_AP_fall_rate(sweep_id):
	AP_fall_rate_array=AP_fall_rate1[sweep_id]['AP_fall_rate']
	AP_fall_rate_array=np.append(AP_fall_rate_array,[])
	index1=len(AP_fall_rate_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_fall_rate.loc[sweep_id+idx9,spike_index]=AP_fall_rate_array[n]
	return

def save_AP_rise_rate_change(sweep_id):
	AP_rise_rate_change_array=AP_rise_rate_change1[sweep_id]['AP_rise_rate_change']
	AP_rise_rate_change_array=np.append(AP_rise_rate_change_array,[])
	index1=len(AP_rise_rate_change_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_rise_rate_change.loc[sweep_id+idx10,spike_index]=AP_rise_rate_change_array[n]
	return

def save_AP_fall_rate_change(sweep_id):
	AP_fall_rate_change_array=AP_fall_rate_change1[sweep_id]['AP_fall_rate_change']
	AP_fall_rate_change_array=np.append(AP_fall_rate_change_array,[])
	index1=len(AP_fall_rate_change_array)
	for n in range(0,index1):
		spike_index='spike'+'_'+str(n+1)
		AP_fall_rate_change.loc[sweep_id+idx11,spike_index]=AP_fall_rate_change_array[n]
	return

path='/mnt/f/temp/allen_rawdata/'
os.chdir(path)
filelist=pd.read_csv('filelist.csv',header=None)
sample_rate=pd.read_csv('/mnt/f/temp/allen/info.csv',header=None,usecols=[3],skiprows=1)

#threshold=pd.DataFrame()
#AP_amp=pd.DataFrame()
#AP_width=pd.DataFrame()
#spike_half_width=pd.DataFrame()
AP_amplitude_change=pd.DataFrame()

AP_rise_time=pd.DataFrame()
AP_fall_time=pd.DataFrame()
AP_rise_rate=pd.DataFrame()
AP_fall_rate=pd.DataFrame()
AP_rise_rate_change=pd.DataFrame()
AP_fall_rate_change=pd.DataFrame()

for i in range(0,len(filelist)):
	filename=filelist.loc[i,0]
	#feature['Id'].append(filename.split(".")[0])
	data=np.loadtxt(filename,skiprows=1)#*1000 #导入数据
	t=1000/float(sample_rate.loc[i,3])
	time=np.arange(0,len(data))*t#构建时间向量
	stimulus=pd.DataFrame(data[:,1])
	V=pd.DataFrame(data[:,0])
	length=len(data[0,:])
	index=length/2
	hyper_num=0
	if sample_rate.loc[i,3]==200000:
		stim_index=300000
	else:
		stim_index=80000
	stim=np.array([data[stim_index,1]])
	#将data中的stim和V数据分开
	for j in range(1,index):
		num_stim=2*j+1
		num_v=2*j
		stimulus.loc[:,j]=pd.DataFrame(data[:,num_stim])
		V.loc[:,j]=pd.DataFrame(data[:,num_v])
		stim_i=data[stim_index,num_stim]
		unstim_i=data[0,num_stim]
		stim=np.append(stim,data[stim_index,num_stim])
		if stim_i<unstim_i:
			hyper_num=hyper_num+1
	#构建域上和域下的sweep列表
	sweep_num=V.columns.size
	spike_sweeps=[]
	subthreshold_sweeps=[]
	sweeps=[]
	hyper_sweeps=[]
	for k in range(0,sweep_num):
		sweep={}
		sweep['T']=time
		sweep['V']=V.loc[:,k]
		sweep['stim_start'] = [1020]
		sweep['stim_end'] = [2020]
		spikecount = efel.getFeatureValues([sweep], ['Spikecount'])
		sweeps[k:k+1]=[sweep]
		if k<=hyper_num:
			l=len(hyper_sweeps)
			hyper_sweeps[l:l+1]=[sweep]
		else:
			if spikecount[0]['Spikecount'][0]==0:
				a=len(subthreshold_sweeps)
				subthreshold_sweeps[a:a+1]=[sweep]
			else:
				b=len(spike_sweeps)
				spike_sweeps[b:b+1]=[sweep]
	rheobase_sweep=[spike_sweeps[0]]

	#threshold1=efel.getFeatureValues(spike_sweeps,['AP_begin_voltage'])
	#AP_amp1=efel.getFeatureValues(spike_sweeps,['AP_amplitude'])
	#AP_width1=efel.getFeatureValues(spike_sweeps,['AP_width'])
	#spike_half_width1=efel.getFeatureValues(spike_sweeps,['spike_half_width'])
	AP_amplitude_change1=efel.getFeatureValues(spike_sweeps,['AP_amplitude_change'])

	#升/降支参数
	AP_rise_time1=efel.getFeatureValues(spike_sweeps,['AP_rise_time'])
	AP_fall_time1=efel.getFeatureValues(spike_sweeps,['AP_fall_time'])
	AP_rise_rate1=efel.getFeatureValues(spike_sweeps,['AP_rise_rate'])
	AP_fall_rate1=efel.getFeatureValues(spike_sweeps,['AP_fall_rate'])
	AP_rise_rate_change1=efel.getFeatureValues(spike_sweeps,['AP_rise_rate_change'])
	AP_fall_rate_change1=efel.getFeatureValues(spike_sweeps,['AP_fall_rate_change'])

	#idx1=len(threshold)
	#idx2=len(AP_amp)
	#idx3=len(AP_width)
	#idx4=len(spike_half_width)
	idx5=len(AP_amplitude_change)

	idx6=len(AP_rise_time)
	idx7=len(AP_fall_time)
	idx8=len(AP_rise_rate)
	idx9=len(AP_fall_rate)
	idx10=len(AP_rise_rate_change)
	idx11=len(AP_fall_rate_change)

	for sweep_id in range(0,len(spike_sweeps)):
		#threshold.loc[sweep_id+idx1,'Id']=filename.split(".")[0]
		#threshold.loc[sweep_id+idx1,'sweep_id']=sweep_id+1

		#AP_amp.loc[sweep_id+idx2,'Id']=filename.split(".")[0]
		#AP_amp.loc[sweep_id+idx2,'sweep_id']=sweep_id+1

		#AP_width.loc[sweep_id+idx3,'Id']=filename.split(".")[0]
		#AP_width.loc[sweep_id+idx3,'sweep_id']=sweep_id+1

		#spike_half_width.loc[sweep_id+idx4,'Id']=filename.split(".")[0]
		#spike_half_width.loc[sweep_id+idx4,'sweep_id']=sweep_id+1

		AP_amplitude_change.loc[sweep_id+idx5,'Id']=filename.split(".")[0]
		AP_amplitude_change.loc[sweep_id+idx5,'sweep_id']=sweep_id+1

		AP_rise_time.loc[sweep_id+idx6,'Id']=filename.split(".")[0]
		AP_rise_time.loc[sweep_id+idx6,'sweep_id']=sweep_id+1

		AP_rise_rate.loc[sweep_id+idx7,'Id']=filename.split(".")[0]
		AP_rise_rate.loc[sweep_id+idx7,'sweep_id']=sweep_id+1

		AP_fall_rate.loc[sweep_id+idx8,'Id']=filename.split(".")[0]
		AP_fall_rate.loc[sweep_id+idx8,'sweep_id']=sweep_id+1

		AP_fall_time.loc[sweep_id+idx9,'Id']=filename.split(".")[0]
		AP_fall_time.loc[sweep_id+idx9,'sweep_id']=sweep_id+1

		AP_rise_rate_change.loc[sweep_id+idx10,'Id']=filename.split(".")[0]
		AP_rise_rate_change.loc[sweep_id+idx10,'sweep_id']=sweep_id+1

		AP_fall_rate_change.loc[sweep_id+idx11,'Id']=filename.split(".")[0]
		AP_fall_rate_change.loc[sweep_id+idx11,'sweep_id']=sweep_id+1

		#save_threshold(sweep_id)
		#save_AP_amp(sweep_id)
		#save_AP_width(sweep_id)
		#save_spike_half_width(sweep_id)
		save_AP_amplitude_change(sweep_id)

		save_AP_rise_time(sweep_id)
		save_AP_fall_time(sweep_id)
		save_AP_rise_rate(sweep_id)
		save_AP_fall_rate(sweep_id)
		save_AP_rise_rate_change(sweep_id)
		save_AP_fall_rate_change(sweep_id)

	#threshold.to_csv('threshold.csv',index=False)
	#AP_amp.to_csv('AP_amp.csv',index=False)
	#AP_width.to_csv('AP_width.csv',index=False)
	#spike_half_width.to_csv('spike_half_width.csv',index=False)
	AP_amplitude_change.to_csv('AP_amplitude_change.csv',index=False)

	AP_rise_time.to_csv('AP_rise_time.csv',index=False)
	AP_fall_time.to_csv('AP_fall_time.csv',index=False)
	AP_rise_rate.to_csv('AP_rise_rate.csv',index=False)
	AP_fall_rate.to_csv('AP_fall_rate.csv',index=False)
	AP_rise_rate_change.to_csv('AP_rise_rate_change.csv',index=False)
	AP_fall_rate_change.to_csv('AP_fall_rate_change.csv',index=False)


#!/usr/bin/env python
__author__ = 'Albino'

"""
将 第 0015 题中的 city.xls 文件中的内容写到 city.xml 文件中
"""

import xlrd
import xml.dom.minidom as md

def get_xls_data(filename):
     book = xlrd.open_workbook(filename)
     sheet = book.sheet_by_index(0)
     content = {}
     for i in range(sheet.nrows):
         content[i+1] = sheet.row_values(i)[1:]
     return content

def write_to_xml(xlscontent):
    xmlfile = md.Document()
    root = xmlfile.createElement('root')
    citys = xmlfile.createElement('citys')
    xmlfile.appendChild(root)
    root.appendChild(citys)
    comment = xmlfile.createComment('城市信息')
    citys.appendChild(comment)
    xmlcontent = xmlfile.createTextNode(str(xlscontent))
    citys.appendChild(xmlcontent)
    with open('citys.xml', 'wb') as f:
        f.write(xmlfile.toprettyxml(encoding='utf-8'))

if __name__ == '__main__':
    write_to_xml(get_xls_data('city.xls'))


# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root
# for license information.

from __future__ import absolute_import, print_function, unicode_literals

import pytest

from ptvsd.common import log
from ptvsd.common.compat import reload

from ptvsd.server import cli, options

@pytest.mark.parametrize("target_kind", ["file", "module", "code"])
@pytest.mark.parametrize("client", ["", "client"])
@pytest.mark.parametrize("wait", ["", "wait"])
@pytest.mark.parametrize("subprocesses", ["", "subprocesses"])
@pytest.mark.parametrize("extra", ["", "extra"])
def test_targets(target_kind, client, wait, subprocesses, extra):
    args = ["--host", "localhost", "--port", "8888"]

    if client:
        args += ["--client"]

    if wait:
        args += ["--wait"]

    if not subprocesses:
        args += ["--no-subprocesses"]

    if target_kind == "file":
        target = "spam.py"
        args += [target]
    elif target_kind == "module":
        target = "spam"
        args += ["-m", target]
    elif target_kind == "code":
        target = "123"
        args += ["-c", target]
    else:
        pytest.fail(target_kind)

    if extra:
        extra = [
            "ham",
            "--client",
            "--wait",
            "-y",
            "spam",
            "--",
            "--host",
            "--port",
            "-c",
            "--something",
            "-m",
        ]
        args += extra
    else:
        extra = []

    log.debug("args = {0!r}", args)
    reload(options)
    rest = list(cli.parse(args))
    assert rest == extra

    expected_options = {
        "target_kind": target_kind,
        "target": target,
        "host": "localhost",
        "port": 8888,
        "wait": bool(wait),
        "multiprocess": bool(subprocesses),
    }
    actual_options = {name: vars(options)[name] for name in expected_options}
    assert expected_options == actual_options

def test_unsupported_arg():
    reload(options)
    with pytest.raises(Exception):
        cli.parse(["--port", "8888", "--xyz", "123", "spam.py"])

def test_host_required():
    reload(options)
    with pytest.raises(Exception):
        cli.parse(["--port", "8888", "-m", "spam"])

def test_host_empty():
    reload(options)
    cli.parse(["--host", "", "--port", "8888", "spam.py"])
    assert options.host == ""

def test_port_default():
    reload(options)
    cli.parse(["--host", "localhost", "spam.py"])
    assert options.port == 5678


from bottle import hook, request, post
from index import mysqlObject

from returnstatus import Status

__author__ = 'Chris'

class atmsAPI():

	@post('/api/v1.0/atms/load')
	def GetATMs(userID=None):
		userID = userID or request.forms.get('user')

		returnData = mysqlObject.Query('SELECT id, `Area` FROM ATMLocations WHERE (ChildID = %s OR ParentID = %s)', [userID,userID])

		#Just loop through all the data, grab the location, return it with a key in the table.
		exposedData = {}
		for option in returnData:
			areaData= {
				'location':option[1],
			}
			exposedData[option[0]] = areaData

		return Status.ReturnOutputData(0,exposedData)

	@post('/api/v1.0/atms/create')
	def CreateLocation():
		userID = request.forms.get('user')
		target = request.forms.get('target')
		location = request.forms.get('location') #In this context, location is a string. There can be unlimited amounts of these,
		#Although they each get generated their own ID.

		location.strip() #Strip out any potential blank data just in case.

		if not location or location.isspace():
			return Status.ReturnOutputData(5,{'Error':'Invalid location specified.'})

		returnData = mysqlObject.Query('SELECT COUNT(id) FROM ATMLocations WHERE (ChildID = %s)', [target])

		#Just validate they don't already have 5 or more areas...
		if int(returnData[0][0]) >= 5:
			return Status.ReturnOutputData(5,{'Error':'Child already has too many areas.'})

		#Otherwise dump it straight in
		mysqlObject.Query('INSERT INTO ATMLocations (Area,ChildID,ParentID) VALUES (%s,%s,%s);', [location,target,userID])
		return atmsAPI.GetATMs(target)

	@post('/api/v1.0/atms/delete')
	def DeleteLocation():
		userID = request.forms.get('user')
		location = request.forms.get('location')

		#Just validate we actually get data returned
		validate = mysqlObject.Query('SELECT id FROM ATMLocations WHERE ParentID = %s AND id = %s', [userID, location])
		if len(validate) == 0:
			return Status.ReturnOutputData(5,{'Error':'Area is not valid for target.'})

		#Passed the check? Lets just delete then.
		deleteQuery = mysqlObject.Query('DELETE FROM ATMLocations WHERE ParentID = %s AND id = %s', [userID, location])
		return Status.ReturnOutputData(0, {'success':True})


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 子图
fig = plt.figure(figsize=(10,6), facecolor='gray')

ax1 = fig.add_subplot(2,2,1) # 切换到 两行两列 第一张图
plt.plot(np.random.rand(50).cumsum(), 'k--')

ax1 = fig.add_subplot(2,2,2) # 切换到 两行两列 第二张图
plt.plot(np.random.rand(50).cumsum(), 'b--')

plt.show()

fig,axes = plt.subplots(2, 3, figsize=(10,4),
	sharex=True, sharey=True # 共享坐标轴
	)

ax1 = axes[0,1]
ts = pd.Series(np.random.randn(1000).cumsum())
ax1.plot(ts)

df = pd.DataFrame(np.random.randn(100, 2))
df.plot(ax=axes[1,0])

plt.show()


# -*- coding: utf-8 -*-
# @Time    : 3/21/18 10:56 AM
# @Author  : Zhu Junwei
# @File    : download_sample.py
import os
import requests
import concurrent.futures as futures

def downloadimgs(datafile):
    cid32namedict = dict()
    cid3downloadednum = dict()
    targetpath = "/data1/36w/"
    cid32namefile = "/data1/img_list/cid32namefinal.txt"

    cid32namelines = open(cid32namefile, mode='r', encoding='UTF-8').readlines()
    for line in cid32namelines:
        words = line.split(' ')
        cid32namedict[words[0]] = words[words.__len__() - 1][0:words[words.__len__() - 1].__len__() - 1]
        cid3downloadednum[words[0]] = 0
    imgdatalines = open(datafile, mode='r', encoding='UTF-8').readlines()

    for cid3 in cid32namedict:
        folder_path = os.path.join(targetpath, cid3 + "_" + cid32namedict.get(cid3))
        if os.path.exists(folder_path) == False:
            os.mkdir(folder_path)

    successcounter = 0
    failcounter = 0
    for line in imgdatalines:
        strlist = line.split()
        skuidstr = strlist[0]
        cid3 = strlist[1]
        if cid3 not in cid32namedict:
            continue
        if cid3downloadednum[cid3] > 3000:
            continue
        imgstr = strlist[2]
        imgurl = "http://img10.360buyimg.com/n12/" + imgstr
        savepath = os.path.join(targetpath, cid3 + "_" + cid32namedict.get(cid3))
        savename = cid3 + '_' + skuidstr + '_' + imgstr.replace('/', '_')
        save_file_path = os.path.join(savepath,savename)
        print(save_file_path)
        if os.path.exists(save_file_path):
            continue
        try:
            response = requests.get(imgurl)
            with open(save_file_path, "wb") as imgfile:
                imgfile.write(response.content)
                successcounter+=1
                imgfile.close()
            cid3downloadednum[cid3] += 1
        except Exception:
            print(skuidstr + " " + imgstr + " cannot download or save successfully!")
            failcounter+=1
        if successcounter % 1000 == 0:
            print("image saved: " + str(successcounter))
    print("done")
    print("image saved: " + str(successcounter))
    print("image failed: " + str(failcounter))

if __name__ == "__main__":
    downloadimgs('/home/zjw/Desktop/new_add_pics.txt')
    # thread_num = 10
    # with futures.ProcessPoolExecutor(max_workers=thread_num) as executor:
    #     future_list = list()
    #     for i in range(thread_num):
    #         file_list = '/data1/img_list/pick_imgs{}.txt'.format(i+1)
    #
    #         future_list.append(executor.submit(downloadimgs, file_list))
    #     for future in futures.as_completed(future_list):
    #         if future.exception():
    #             print(future.exception())


#coding:utf-8
import cv2
import numpy as np

path='/home/cooper/Dataset/dataset-resized/dataset-resized/glass/glass45.jpg'

image=cv2.imread(path)
image=np.array(image,np.int32)
grayimage=np.sum(image,axis=2)/3
grayimage=np.array(grayimage,np.uint8)
image=np.array(image,np.uint8)
cv2.imwrite('3image.jpg',image)
cv2.imwrite('3grayimage.jpg',grayimage)
# cv2.waitKey(0)

k=180

h,w=image.shape[0],image.shape[1]

for k in range(3):
    for i in range(h):
        for j in range(w):
            image[i][j][k] = min(255, image[i][j][k]+20)
cv2.imwrite('3liangdu.jpg',image)
# for k in range(3):
#     for i in range(h):
#         for j in range(w):

for k in range(3):
    for i in range(h):
        for j in range(w):
            if image[i][j][k]<k:
                image[i][j][k]=max(0,image[i][j][k]//2)
            else:
                image[i][j][k]=min(255,image[i][j][k]*2)

image=np.array(image,np.uint8)
cv2.imwrite('3duibidu.jpg',image)
cv2.waitKey(0)


from tkinter import *
import pygame
from pygame.locals import *
from math import cos, sin,radians,sqrt
import collections
from hexagon import *
import numpy
from numpy import concatenate
import draw, config, game_rules
# import sys
# sys.stdout = open('stdout.txt', 'w')

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.title("Andantino - Hexagon Grid")
        self.can = Canvas(self, width=config.width, height=config.height, bg= "#60ace6")
        self.size = config.size
        self.state = 0
        self.start = 1
        self.can.pack(padx=0)
        self.hexagons = []
        self.hexagons_board = []
        self.hexagons_white = []
        self.hex_append = []
        self.list_of_neigh = []
        self.hexagons_black = []
        self.neigh_append = []
        self.hex_closest1 = []
        self.first_n = []
        self.hex_glob = []
        self.glob_inter = []
        self.first_hex = []
        self.glob_line = []
        self.initGrid(config.rows, config.cols, self.size)
        # print("number of hexes in total : " + str(len(self.hexagons)))
        self.create_hexes_board()
        draw.draw_player(self.can,self.hexagons_board,self.size)
        self.can.bind("<Button-1>", self.click_pos)
        frame = Frame()
        frame.pack()
        draw.draw_board(self.can,self.hexagons_board,self.size)
        draw.draw_neighb(self.can,self.add_first_hex(),self.size)

    def add_first_hex (self):
        for i in range(len(self.hexagons_board)):
            if self.hexagons_board[i].row == 9 and self.hexagons_board[i].col == 10:
                first_neighs = draw.neighbour(self.hexagons_board[i],self.hexagons_board)
                self.hex_glob = [self.hexagons_board[i]]
                self.hexagons_white = [self.hexagons_board[i]]
                self.first_hex = [self.hexagons_board[i]]
                self.neigh_append = first_neighs #adds first neighbours before second hex
                self.first_n = first_neighs
                draw.draw_n_first_hex(self.neigh_append,self.can)
        return first_neighs

    def initGrid(self, cols, rows, size): # prints background layer of hexes, with offset shifting layers and is of shape of rectangle
        start_x_first_hex = +0
        start_y_first_hex = +20
        """
        2d grid of hexagons
        """
        # to get inclusive range chang
        for c in range(cols):
            for r in range(rows):
                if r % 2 == 0:
                    offset = (sqrt(3)/2)*size
                else:
                    offset = 0
                h = Hexagon(r,c,self.can,
                            start_x_first_hex + (c * (size * sqrt(3))+  offset),
                            start_y_first_hex + ((r * (size * 3/2)+0.5)+0.5)  ,
                            size,
                            "#a1e2a1")
                self.hexagons.append(h)
                # self.can.create_text(50, 50, text='text')
                # h.draw_coordinate(h.x,h.y,str(r),chr(c+97))
                h.draw_coordinate(h.x,h.y,str(r),str(c))
                # h.convert_coordinate(h.x,h.y,str(r),(c))

    def create_hexes_board(self): #continuation of grid. Takes the printed hexes and aligned them in a desired manner
        row = 0
        col = 5
        inc_row = 19
        end_row = 10
        for h in range (inc_row):#for each row
            for i in range(end_row):#for each column
                for j in range(len(self.hexagons)):   #search for the right hex with specific row and col
                    if (self.hexagons[j].row == row and self.hexagons[j].col ==  col):
                        self.hexagons_board.append(self.hexagons[j])
                        # print("type of hexboard",type(self.hexagons_board))
                col += 1
            col -= (end_row )
            if h < 9:
                end_row += 1
                if (h%2 == 1):
                    col -= 1
            else:
                end_row -= 1
                if (h%2 == 0):
                    col +=1
            row += 1

    def closest_hex (self, click_pos):
        distances = []
        hex_closest1 = []
        hex_append = []
        for i in self.hexagons_board:
            # distance = (sqrt((i.x - click_pos[0])**2), sqrt((i.y - click_pos[1])**2))
            distance = sqrt((i.x+self.size - click_pos[0])**2 + (i.y+self.size*0.5 - click_pos[1])**2)
            distances.append(distance)
        #    mini = self.hexagons_board.index(min(distances))

            self.hex_closest1 = self.hexagons_board[distances.index(min(distances))]
        print("hex_closest", self.hex_closest1.row, self.hex_closest1.col)
        return self.hexagons_board[distances.index(min(distances))]

    def out_of_boundaries (self,hex_center,player):
        if not game_rules.flood_fill(hex_center,[],player,self.hexagons_board):
            print("WIN!!!!!!!!!!!!!!!!")
            print("check",[hex_center.row,hex_center.col])
            return True
        else:
            print("keep on trying")
            return False

    def click_pos (self, event):

        new1 = []
        xy = (event.x, event.y)
        hex_closest = self.closest_hex(xy)
        # if self.start == 1:
        for i in self.first_n:
            if hex_closest == i and hex_closest not in self.hex_glob:
                self.hex_glob.append(hex_closest)
                self.list_of_neigh = draw.neighbour(hex_closest,self.hexagons_board)
       #         print("list_of_neigh",self.list_of_neigh)
                for i in draw.neighbour(hex_closest,self.hexagons_board):
                    self.neigh_append.append(i)
                inter_list = self.intersection(self.neigh_append)
                self.first_n = inter_list
        #        print("intersection",self.first_n)
                if len(self.intersection(self.neigh_append)) <= 2:
                    # print("ile mam hexow",len(self.intersection(self.neigh_append)))
                    draw.sub_first_hex(self.neigh_append,self.can)

                if self.state == 1:
                    draw.draw_white(self.can,hex_closest,self.size)
                    self.hexagons_white.append(hex_closest)
                    # print("white hexes",self.hexagons_white)
                    draw.draw_neighb(self.can,self.first_n,self.size)
                    self.state = 0
                    if len(self.hexagons_white)>4:
                        for i in range(len(self.hexagons_white)):
                            game_rules.diag3_line5(self.hexagons_white,self.hexagons_white)
                            game_rules.diag2_line5(self.hexagons_white,self.hexagons_white)
                            game_rules.diag1_line5(self.hexagons_white,self.hexagons_white)
                    if len(self.hexagons_white)>5:
                        for i in range(len(self.hexagons_black)):
                            if self.out_of_boundaries(self.hexagons_black[i],self.hexagons_white):
                                break
                else:
                    draw.draw_black(self.can,hex_closest,self.size)
                    self.hexagons_black.append(hex_closest)
                    draw.draw_neighb(self.can,self.first_n,self.size)
                    self.state = 1
                    if len(self.hexagons_black)>4:
                        for i in range(len(self.hexagons_black)):
                            game_rules.diag3_line5(self.hexagons_black,self.hexagons_black)
                            game_rules.diag2_line5(self.hexagons_black,self.hexagons_black)
                            game_rules.diag1_line5(self.hexagons_black,self.hexagons_black)
                    if len(self.hexagons_black)>5:
                        for i in range(len(self.hexagons_white)):
                            if self.out_of_boundaries(self.hexagons_white[i],self.hexagons_black):
                                break

    def intersection (self,seq):
        seen = set()
        seen_add = seen.add
        # adds all elements it doesn't know yet to seen and all other to seen_twice
        hexes_neigh = set( x for x in seq if x in seen or seen_add(x) )
        return hexes_neigh

# root = Tk()
# root.update()


import glob
import sys

import elasticsearch
import elasticsearch.client
import elasticsearch.helpers

from .break_down import process_file
from .prepare_index import create_index

INDEX_NAME = "oracc"

def debug(msg):
    print(msg)

def upload_entries(es, entries):
    for entry in entries:
        entry["_index"] = INDEX_NAME
        entry["completions"] = [entry["cf"], entry["gw"]]
    elasticsearch.helpers.bulk(es, entries)

def upload_file(es, input_file):
    upload_entries(es, process_file(input_file, write_file=False))

def ICU_installed(es):
    """Check whether the ICU Analysis plugin is installed locally."""
    cc = elasticsearch.client.CatClient(es)
    return "analysis-icu" in [p["component"] for p in cc.plugins(format="json")]

if __name__ == "__main__":
    es = elasticsearch.Elasticsearch()
    if not ICU_installed(es):
        debug("ICU Analysis plugin is required but could not be found. Exiting.")
        sys.exit()

    clear_database = True

    if len(sys.argv) > 1:
        files = sys.argv[1:]
        print(files)
    else:
        files = glob.glob("neo/gloss-???.json")
    debug("Will index {}".format(",".join(files)))

    # Clear ES database if desired
    client = elasticsearch.client.IndicesClient(es)
    if clear_database:
        try:
            debug("Will delete index " + INDEX_NAME)
            client.delete(INDEX_NAME)
        except elasticsearch.exceptions.NotFoundError:
            debug("Index not found, continuing")

    # Create the index with the required settings
    create_index(es, INDEX_NAME)

    for file in files:
        # Break down into individual entries and upload to ES using the bulk API
        upload_file(es, file)


import numpy as np
from numba import jit

@jit
def nroot(x0,f,fp,tol=1e-5,miter=15):
    i=0
    xold=x0
    xnew=np.inf
    while abs(xold-xnew)>tol and i < 15:
        if i != 0:
            xold = np.copy(xnew)
        xnew = xold - f(xold)/fp(xold)
        # xold = np.copy(xnew)
        i+=1
        # print(xold)
    if abs(xold-xnew)<tol:
        return xnew
    else:
        return "fail"

@jit
def pdif(r, p1, p2, n1, n2):
    dif = p2 * (1 - (1 + r) ** (-n2)) - p1 * (((1 + r) ** n1) - 1)
    return dif

@jit
def pder(r, p1, p2, n1, n2, h=1e-10):
    der = (pdif(r + h, p1, p2, n1, n2) - pdif(r - h, p1, p2, n1, n2)) / (2 * h)
    return der

# @jit

def psolve(p1, p2, n1, n2,tol=1e-5, miter=1500):
        i = 0
        xold = .1
        xnew = - 1000 + tol
        while abs(xold - xnew) > tol and i < miter:
            if i != 0:
                xold = np.copy(xnew)
            xnew = xold - pdif(xold,p1, p2, n1, n2) / pder(xold,p1, p2, n1, n2)
            i += 1
            # xold = np.copy(xnew)
            # print(xnew)
        if abs(xold - xnew) < tol:
            return xnew
        else:
            return "fail"


from status.models import Status
from permisos.models import UsersPerfiles
from productos.models import Insumos, Productos, ProductosInsumos
from inventarios.models import Stock
from configuraciones.models import Puntos, PuntosAlmacenes, Almacenes

from controllers.DefaultValues import DefaultValues

from django.db import transaction

class StockController(DefaultValues):
    def __init__(self):
        DefaultValues.__init__(self)

        self.table_name = 'registros'
        self.table_id = 'registro_id'

    def update_stock(self, user_perfil, almacen, producto, cantidad, fecha_elaboracion=None, fecha_vencimiento=None, lote=''):
        """actualizamos el stock"""
        try:
            with transaction.atomic():
                # productos normales
                stock_filter = Stock.objects.filter(almacen_id=almacen, insumo_id=producto, fecha_elaboracion=fecha_elaboracion, fecha_vencimiento=fecha_vencimiento, lote=lote.upper())
                stock = stock_filter.first()
                if not stock:
                    # creamos el registro
                    stock = Stock.objects.create(almacen_id=almacen, insumo_id=producto, fecha_elaboracion=fecha_elaboracion,
                                                 fecha_vencimiento=fecha_vencimiento, lote=lote.upper(), user_perfil_id=user_perfil, status_id=self.status_activo)

                cantidad_update = stock.cantidad + cantidad
                stock.cantidad = cantidad_update
                stock.save()

            return True

        except Exception as ex:
            print('Error update stock: ' + str(ex))
            self.error_operation = 'Error al actualizar stock, ' + str(ex)
            return False

    def stock_producto(self, producto_id, user_perfil, almacen_id):
        """devuelve el stock del producto"""
        try:
            almacen = Almacenes.objects.get(pk=almacen_id)
            #producto = Productos.objects.get(pk=producto_id)
            producto = Insumos.objects.get(pk=producto_id)

            filtros = {}
            filtros['almacen_id'] = almacen
            filtros['cantidad__gt'] = 0
            filtros['insumo_id'] = producto
            stock_almacen = Stock.objects.filter(**filtros).order_by('fecha_vencimiento', 'fecha_elaboracion', 'lote')

            return stock_almacen

        except Exception as ex:
            self.error_operation = 'Error al recuperar stock, ' + str(ex)
            raise ValueError('Error al recuperar stock del producto, ' + str(ex))

    def stock_producto_insumo(self, producto_id, almacen_id):
        """devuelve el stock del producto con sus insumos"""
        try:
            producto = Productos.objects.get(pk=producto_id)

            productos_insumos = ProductosInsumos.objects.filter(producto_id=producto)
            lista_insumos = []
            for pro_ins in productos_insumos:
                dato_insumo = {}
                dato_insumo['insumo_id'] = pro_ins.insumo_id.insumo_id
                dato_insumo['insumo'] = pro_ins.insumo_id.insumo

                datos_stock = self.stock_producto(pro_ins.insumo_id.insumo_id, None, almacen_id)
                actual = 0
                for dato_aux in datos_stock:
                    actual = actual + int(dato_aux.cantidad)

                dato_insumo['cantidad'] = actual
                lista_insumos.append(dato_insumo)

            return lista_insumos

        except Exception as ex:
            self.error_operation = 'Error al recuperar stock, ' + str(ex)
            raise ValueError('Error al recuperar stock del producto, ' + str(ex))


import voluptuous as vol
from voluptuous import Any

DEFAULT_DISCOVERY = False
DEFAULT_DISCOVERY_PREFIX = "homeassistant"
DEFAULT_AVAILABILITY_TOPIC = "home-assistant/cover/availabilty"
DEFAULT_PAYLOAD_AVAILABLE = "online"
DEFAULT_PAYLOAD_NOT_AVAILABLE ="offline"
DEFAULT_STATE_MODE = "normally_open"
DEFAULT_INVERT_RELAY = False
DEFAULT_POLL_DELAY = 0
DEFAULT_PRESS_TIME = 0.2
DEFAULT_READ_DELAY = 0.5
DEFAULT_SWITCH_DEBOUNCE = 300

CONFIG_SCHEMA = vol.Schema(
    {
    "logging": vol.Schema(
        {
            vol.Required("log_level"): Any('DEBUG', 'INFO', 'WARNING','ERROR', 'CRITICAL'),
            vol.Required("show_timestamp"): bool
        }),
    "mqtt": vol.Schema(
        {
            vol.Required("host"): str,
            vol.Required("port"): int,
            vol.Required("user"): str,
            vol.Required("password"): str,
            vol.Optional("discovery", default = DEFAULT_DISCOVERY): Any(bool, None),
            vol.Optional("discovery_prefix", default = DEFAULT_DISCOVERY_PREFIX): Any(str, None),
            vol.Optional("availability_topic", default = DEFAULT_AVAILABILITY_TOPIC): Any(str, None),
            vol.Optional("payload_available", default = DEFAULT_PAYLOAD_AVAILABLE): Any(str,None),
            vol.Optional("payload_not_available", default = DEFAULT_PAYLOAD_NOT_AVAILABLE ): Any(str, None)

        }
    ),
    "doors": [vol.Schema(
        {
            vol.Required("id"): str,
            vol.Optional("uuid"): Any(str, None),
            vol.Optional("name"): Any(str, None),
            vol.Required("relay"): int,
            vol.Required("state"): int,
            vol.Optional("open"): int,
            vol.Optional("state_mode", default = DEFAULT_STATE_MODE): Any(None, 'normally_closed', 'normally_open'),
            vol.Optional("invert_relay", default = DEFAULT_INVERT_RELAY): bool,
            vol.Optional("state_topic"): str,
            vol.Required("command_topic"): str,
            vol.Optional("poll_delay", default=DEFAULT_POLL_DELAY): int,
            vol.Optional("press_time", default=DEFAULT_PRESS_TIME): float,
            vol.Optional("read_delay", default=DEFAULT_READ_DELAY): float,
            vol.Optional("switch_debounce", default=DEFAULT_SWITCH_DEBOUNCE): int

        }
    )]
    })


from flask import Flask, jsonify, send_from_directory

import os

app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

@app.route('/<path:path>', methods=['GET'])
def static_proxy(path):
  return send_from_directory('./', path)

@app.route('/')
def root():
  return send_from_directory('./', 'index.html')

@app.route('/api/prose', methods=['GET'])
def get_prose():

    folder = 'prose'

    articles = []

    directory = os.fsencode(folder)
    for file in os.listdir(directory):
         filename = os.fsdecode(file)
         articles.append(process_article(folder + '/' + filename))

    return jsonify(articles)

@app.route('/api/poetry', methods=['GET'])
def get_poetry():

    folder = 'poetry'

    poems = []

    directory = os.fsencode(folder)
    for file in os.listdir(directory):
         filename = os.fsdecode(file)
         poems.append(process_poem(folder + '/' + filename))

    return jsonify(poems)

def process_article(filename):
    article = {}
    firstLine = True
    curPar = ''
    with open(filename) as f:
        for line in f:
            line = line.rstrip()
            if firstLine:
                article['title'] = line
                firstLine = False
                article['body'] = []
            elif line:
                curPar += line + ' '
            else:
                article['body'].append(curPar)
                curPar = ''
    return article

def process_poem(filename):
    poem = {}
    firstLine = True
    with open(filename) as f:
        for line in f:
            line = line.rstrip()
            if firstLine:
                poem['title'] = line
                firstLine = False
                poem['body'] = [[]]
            elif line:
                poem['body'][-1].append(line)
            else:
                poem['body'].append([])
    return poem

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

@app.errorhandler(500)
def server_error(e):
  return 'An internal error occurred [main.py] %s' % e, 500

# No caching at all for API endpoints.
@app.after_request
def add_header(response):
    # response.cache_control.no_store = True
    response.headers['Cache-Control'] = 'no-store, no-cache, must-\
    revalidate, post-check=0, pre-check=0, max-age=0'
    response.headers['Pragma'] = 'no-cache'
    response.headers['Expires'] = '-1'
    return response


from gevent import monkey
monkey.patch_all()

from flask import Flask
from gevent.pywsgi import WSGIServer

def run_server():
    app = Flask(__name__)

    @app.route("/")
    def hello():
        return "Hello, World!"

    server = WSGIServer(('0.0.0.0', 5005), app)
    server.serve_forever()

if __name__ == '__main__':
    from werkzeug.serving import run_with_reloader
    run_with_reloader(run_server)


import random
import json

from automoticz.extensions import proximity
from automoticz.utils import tool
from automoticz.utils import constants

@tool.cached_with_key('beacon')
def get_default_auth_beacon_name(**kwargs) -> str:
    ''' Returns name of the beacon which property "auth" is set to
    "true".

    :return: beacon name
    '''
    api = proximity.api
    query = 'status:active'
    response = api.beacons().list(q=query).execute()
    default_beacon_name = response['beacons'][0]['beaconName']
    return default_beacon_name

@tool.cached_with_key('project_namespace')
def get_default_project_namespace(**kwargs) -> str:
    ''' Returns name of default namespace for attachments

    :return: default namespace name
    '''
    api = proximity.api
    query = {'projectId': proximity.project_id}
    resp = api.namespaces().list(**query).execute()
    default_project_namespace = resp['namespaces'][0]['namespaceName']
    return default_project_namespace.split('/')[1]

@tool.cached_with_key(constants.CACHE_PIN_KEY)
def get_pin(**kwargs) -> str:
    ''' Checks if for beacon with given name pin attachment
    is set.

    :param beacon_name: name of the beacon
    :param namespace: namespace
    '''
    api = proximity.api
    beacon_name = get_default_auth_beacon_name(cached=True)
    namespace = get_default_project_namespace(cached=True)
    namespaced_type = '{}/{}'.format(namespace, constants.PIN_ATTACHMENT_KEY)
    query = {'beaconName': beacon_name, 'namespacedType': namespaced_type}
    resp = api.beacons().attachments().list(**query).execute()
    attachments = resp.get('attachments')
    if not attachments:
        return None
    b64_data = attachments[0]['data']
    data = json.loads(tool.base64_to_str(b64_data))
    return data[constants.PIN_ATTACHMENT_KEY]

def is_pin_valid(pin: str) -> bool:
    ''' Checks if recieved token is valid with current
    u_token attachment.

    :param pin: incomming pin
    :return: True or False
    '''
    request_pin = tool.base64_to_str(pin)
    current_pin = get_pin(cached=True)
    return request_pin == current_pin

def unset_pin() -> dict:
    ''' Unsets "pin" type attachment on authentication beacon identified
    by beacon_name.
    '''
    api = proximity.api
    beacon_name = get_default_auth_beacon_name(cached=True)
    namespace = get_default_project_namespace(cached=True)
    namespaced_type = '{}/{}'.format(namespace,constants.PIN_ATTACHMENT_KEY)
    query = {
        'beaconName': beacon_name,
        'namespacedType': namespaced_type,
    }
    resp = api.beacons().attachments().batchDelete(**query).execute()
    return resp

def set_pin(pin: str) -> dict:
    ''' Sets "pin" type attachment on authentication beacon identified
    by beacon_name.

    :param pin: unique token
    '''
    api = proximity.api
    beacon_name = get_default_auth_beacon_name(cached=True)
    namespace = get_default_project_namespace(cached=True)
    namespaced_type = '{}/{}'.format(namespace, constants.PIN_ATTACHMENT_KEY)
    if get_pin() is not None:
        unset_pin()
    query = {
        'beaconName': beacon_name,
        'projectId': proximity.project_id,
        'body': {
            'namespacedType': namespaced_type,
            'data': tool.str_to_base64(pin),
        }
    }
    resp = api.beacons().attachments().create(**query).execute()
    return resp

def generate_pin() -> dict:
    ''' Generates new pin.

    :return: pin
    '''
    app = proximity.app
    return {
        'url': app.config.PUBLIC_URL,
        constants.PIN_ATTACHMENT_KEY: str(random.randint(1, 999999999))
    }


from django import forms

from .models import Invoice

class LoginForm(forms.Form):
    pass

class InvoiceForm(forms.ModelForm):
    send_text = forms.BooleanField(label="Отправить краткий текст", required=False)
    send_full_text = forms.BooleanField(label="Отправить полный текст", required=False)
    class Meta:
        model = Invoice
        fields = ('text',
                  'full_text',
                  'image',
                  'departure_date',
                  'receive_date',
                  'send_text',
                  'send_full_text')
        widgets = {
            'departure_date': forms.DateTimeInput(attrs={'type': 'datetime', 'class': 'datetimepicker'}),
            'receive_date': forms.DateTimeInput(attrs={'type': 'datetime', 'class': 'datetimepicker'}),
        }


from django.db.models import Max, Count
from django.shortcuts import render
import itertools
from pingpong.models import Category, Group, GroupMember, Bracket, BracketSlot

def brackets_slideshow(request):
    bracket_groups = [[], [], []]
    pages = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]

    for i, category in enumerate(Category.objects.order_by('id')):
        new_brackets = list(Bracket.objects.filter(category=category).order_by('id').annotate(rounds=Max('bracketslot__level')))
        bracket_groups[pages[i]].extend(new_brackets)

    return render(request, 'brackets_slideshow.html', dict(bracket_groups=bracket_groups[2:]))

def groups_slideshow(request):
    males = Category.objects.filter(gender=0).annotate(player_count=Count('players'))

    male_members = {
        category: []
        for category in males
    }
    for member in GroupMember.objects \
                             .filter(group__category__in=males) \
                             .order_by('group__category', 'group', 'place', '-leader', 'player__surname') \
                             .prefetch_related('group', 'group__category', 'player'):
        male_members[member.group.category].append(member)

    females = Category.objects.filter(gender=1).annotate(player_count=Count('players'))
    female_members = {
        category: []
        for category in females
    }
    for member in GroupMember.objects \
        .filter(group__category__in=females) \
        .order_by('group__category', 'group', 'place', '-leader', 'player__surname') \
        .prefetch_related('group', 'group__category', 'player'):
        female_members[member.group.category].append(member)

    groups = Group.objects.select_related('category')

    single_matches = BracketSlot.objects.filter(status=1, bracket__category__gender__lt=2) \
        .select_related('player', 'bracket__category')
    single_matches = [(a, list(b)) for a, b in itertools.groupby(single_matches, lambda x: x.winner_goes_to_id)]
    single_matches = [(match, slots) for match, slots in single_matches if len(slots) == 2]

    pair_matches = BracketSlot.objects.filter(status=1, bracket__category__gender__gte=2) \
        .select_related('player', 'bracket__category')
    pair_matches = [(a, list(b)) for a, b in itertools.groupby(pair_matches, lambda x: x.winner_goes_to_id)]
    pair_matches = [(match, slots) for match, slots in pair_matches if len(slots) == 2]

    return render(request, 'groups_slideshow.html', {
        'males': male_members.items(),
        'females': female_members.items(),
        'groups': groups,
        'single_matches': single_matches,
        'pair_matches': pair_matches,
    })


import logging.config
from typing import Any
from typing import Optional
from typing import TextIO

import yaml

class Conf:
    config = {}
    default_config_path = 'configs/default_config.yaml'

    def __init__(self, stream: Optional[TextIO]) -> None:
        self.logger = logging.getLogger(f'log_analyzer.Conf')

        with open(Conf.default_config_path, 'r') as f:
            self._load_config(f, default=True)

        if stream:
            self._load_config(stream)

    def _load_config(self, stream: TextIO, default: bool = False) -> None:
        data = yaml.safe_load(stream)
        for key, value in data.items():
            if default:
                self.config[key] = value
            else:
                if key not in self.config:
                    self.logger.error(f'Unknown parameter received: {key}')
                    continue
                if self.config.get(key) == value:
                    continue
                self.logger.info(f'Applying a configuration parameter: {key}: {value}')
                self.config[key] = value

    def __getattribute__(self, name: str) -> Any:
        if name in Conf.config.keys():
            return self.config[name]
        else:
            return object.__getattribute__(self, name)


"""

Game Over / 'Restart from last save point?' pseudo-"room"

"""

import pickle, os
import pygame as pg
from .. import setup, tools
from .. import observer
from ..components import person
from .. import constants as c

class Arrow(pg.sprite.Sprite):
    """
    Arrow to select restart or saved gamed.
    """
    def __init__(self, pos_x, pos_y):
        super(Arrow, self).__init__()
        self.image = setup.GFX['smallarrow']
        self.rect = self.image.get_rect(x=pos_x, y=pos_y)
        self.index = 0
        self.pos_list = [pos_y, pos_y+34]
        self.allow_input = False
        self.observers = [observer.SoundEffects()]

    def notify(self, event):
        """
        Notify all observers of event.
        """

        for listener in self.observers:
            listener.on_notify(event)

    def update(self):
        """
        Update arrow position.
        """

        keys = setup.keys()

        if self.allow_input:
            if (
                    keys[pg.K_DOWN] and
                    not keys[pg.K_UP] and
                    self.index == 0):
                self.index = 1
                self.allow_input = False
                self.notify(c.CLICK)
            elif (keys[pg.K_UP] and
                  not keys[pg.K_DOWN] and
                  self.index == 1):
                self.index = 0
                self.allow_input = False
                self.notify(c.CLICK)

            self.rect.y = self.pos_list[self.index]

        if not keys[pg.K_DOWN] and not keys[pg.K_UP]:
            self.allow_input = True

class DeathScene(tools.State):
    """
    Scene when the player has died.
    """
    def __init__(self):
        super(DeathScene, self).__init__()

        self.name = c.DEATH_SCENE

        self.arrow = None
        self.alpha = None
        self.observers = None
        self.message_box = None
        self.background = None
        self.font = None
        self.player = None
        self.state_dict = None

        self.next = c.TOWN
        setup.mixer().set_level_song(self.name, 'shop_theme')

    def startup(self):
        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
        self.background = pg.Surface(setup.SCREEN_RECT.size)
        self.background.fill(c.BLACK_BLUE)
        self.player = person.Player('down', 1, 1, 'resting', 1)
        self.player.image = pg.transform.scale2x(self.player.image)
        self.player.rect = self.player.image.get_rect()
        self.player.rect.center = setup.SCREEN_RECT.center
        self.message_box = self.make_message_box()
        self.arrow = Arrow(300, 532)
        self.state_dict = self.make_state_dict()
        self.state = c.TRANSITION_IN
        self.alpha = 255
        if not os.path.isfile("save.p"):
            # initializes setup.game_data internally
            tools.create_game_data_dict()
            with open("save.p", "wb") as save_file:
                pickle.dump(setup.game_data(), save_file)
        self.observers = [observer.SoundEffects()]

    def notify(self, event):
        """
        Notify all observers of event.
        """
        for listener in self.observers:
            listener.on_notify(event)

    def make_message_box(self):
        """
        Make the text box informing of death.
        """
        box_image = setup.GFX['dialoguebox']
        box_rect = box_image.get_rect()
        text = 'You have died. Restart from last save point?'
        text_render = self.font.render(text, True, c.NEAR_BLACK)
        text_rect = text_render.get_rect(centerx=box_rect.centerx,
                                         y=30)
        text2 = 'Yes'
        text2_render = self.font.render(text2, True, c.NEAR_BLACK)
        text2_rect = text2_render.get_rect(centerx=box_rect.centerx,
                                           y=70)

        text3 = 'No'
        text3_render = self.font.render(text3, True, c.NEAR_BLACK)
        text3_rect = text3_render.get_rect(centerx=box_rect.centerx,
                                           y=105)

        temp_surf = pg.Surface(box_rect.size)
        temp_surf.set_colorkey(c.BLACK)
        temp_surf.blit(box_image, box_rect)
        temp_surf.blit(text_render, text_rect)
        temp_surf.blit(text2_render, text2_rect)
        temp_surf.blit(text3_render, text3_rect)

        box_sprite = pg.sprite.Sprite()
        box_sprite.image = temp_surf
        box_sprite.rect = temp_surf.get_rect(bottom=608)

        return box_sprite

    def update(self):
        """
        Update scene.
        """
        update_level = self.state_dict[self.state]
        update_level()
        self.draw_level()

    def normal_update(self):
        self.arrow.update()
        self.check_for_input()

    def check_for_input(self):
        """
        Check if player wants to restart from last save point
        or just start from the beginning of the game.
        """

        keys = setup.keys()

        if keys[pg.K_SPACE]:
            if self.arrow.index == 0:
                self.next = c.TOWN
                with open("save.p", "rb") as save_file:
                    setup.register_game_data(pickle.load(save_file))
            elif self.arrow.index == 1:
                self.next = c.MAIN_MENU
            self.state = c.TRANSITION_OUT
            self.notify(c.CLICK2)

    def draw_level(self):
        """
        Draw background, player, and message box.
        """

        surface = setup.screen()

        surface.blit(self.background, (0, 0))
        surface.blit(self.player.image, self.player.rect)
        surface.blit(self.message_box.image, self.message_box.rect)
        surface.blit(self.arrow.image, self.arrow.rect)
        surface.blit(self.transition_surface, (0, 0))


# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

"""Segmentation tasks."""

from typing import Any, Dict, cast

import segmentation_models_pytorch as smp
import torch
import torch.nn as nn
from pytorch_lightning.core.lightning import LightningModule
from torch import Tensor
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.data import DataLoader
from torchmetrics import Accuracy, IoU, MetricCollection

from ..models import FCN

# https://github.com/pytorch/pytorch/issues/60979
# https://github.com/pytorch/pytorch/pull/61045
DataLoader.__module__ = "torch.utils.data"

class SemanticSegmentationTask(LightningModule):
    """LightningModule for semantic segmentation of images."""

    def config_task(self) -> None:
        """Configures the task based on kwargs parameters passed to the constructor."""
        if self.hparams["segmentation_model"] == "unet":
            self.model = smp.Unet(
                encoder_name=self.hparams["encoder_name"],
                encoder_weights=self.hparams["encoder_weights"],
                in_channels=self.hparams["in_channels"],
                classes=self.hparams["num_classes"],
            )
        elif self.hparams["segmentation_model"] == "deeplabv3+":
            self.model = smp.DeepLabV3Plus(
                encoder_name=self.hparams["encoder_name"],
                encoder_weights=self.hparams["encoder_weights"],
                in_channels=self.hparams["in_channels"],
                classes=self.hparams["num_classes"],
            )
        elif self.hparams["segmentation_model"] == "fcn":
            self.model = FCN(
                in_channels=self.hparams["in_channels"],
                classes=self.hparams["num_classes"],
                num_filters=self.hparams["num_filters"],
            )
        else:
            raise ValueError(
                f"Model type '{self.hparams['segmentation_model']}' is not valid."
            )

        if self.hparams["loss"] == "ce":
            self.loss = nn.CrossEntropyLoss(  # type: ignore[attr-defined]
                ignore_index=-1000 if self.ignore_zeros is None else 0
            )
        elif self.hparams["loss"] == "jaccard":
            self.loss = smp.losses.JaccardLoss(
                mode="multiclass", classes=self.hparams["num_classes"]
            )
        elif self.hparams["loss"] == "focal":
            self.loss = smp.losses.FocalLoss(
                "multiclass", ignore_index=self.ignore_zeros, normalized=True
            )
        else:
            raise ValueError(f"Loss type '{self.hparams['loss']}' is not valid.")

    def __init__(self, **kwargs: Any) -> None:
        """Initialize the LightningModule with a model and loss function.

        Keyword Args:
            segmentation_model: Name of the segmentation model type to use
            encoder_name: Name of the encoder model backbone to use
            encoder_weights: None or "imagenet" to use imagenet pretrained weights in
                the encoder model
            in_channels: Number of channels in input image
            num_classes: Number of semantic classes to predict
            loss: Name of the loss function
            ignore_zeros: Whether to ignore the "0" class value in the loss and metrics

        Raises:
            ValueError: if kwargs arguments are invalid
        """
        super().__init__()
        self.save_hyperparameters()  # creates `self.hparams` from kwargs

        self.ignore_zeros = None if kwargs["ignore_zeros"] else 0

        self.config_task()

        self.train_metrics = MetricCollection(
            [
                Accuracy(
                    num_classes=self.hparams["num_classes"],
                    ignore_index=self.ignore_zeros,
                ),
                IoU(
                    num_classes=self.hparams["num_classes"],
                    ignore_index=self.ignore_zeros,
                ),
            ],
            prefix="train_",
        )
        self.val_metrics = self.train_metrics.clone(prefix="val_")
        self.test_metrics = self.train_metrics.clone(prefix="test_")

    def forward(self, x: Tensor) -> Any:  # type: ignore[override]
        """Forward pass of the model.

        Args:
            x: tensor of data to run through the model

        Returns:
            output from the model
        """
        return self.model(x)

    def training_step(  # type: ignore[override]
        self, batch: Dict[str, Any], batch_idx: int
    ) -> Tensor:
        """Training step - reports average accuracy and average IoU.

        Args:
            batch: Current batch
            batch_idx: Index of current batch

        Returns:
            training loss
        """
        x = batch["image"]
        y = batch["mask"]
        y_hat = self.forward(x)
        y_hat_hard = y_hat.argmax(dim=1)

        loss = self.loss(y_hat, y)

        # by default, the train step logs every `log_every_n_steps` steps where
        # `log_every_n_steps` is a parameter to the `Trainer` object
        self.log("train_loss", loss, on_step=True, on_epoch=False)
        self.train_metrics(y_hat_hard, y)

        return cast(Tensor, loss)

    def training_epoch_end(self, outputs: Any) -> None:
        """Logs epoch level training metrics.

        Args:
            outputs: list of items returned by training_step
        """
        self.log_dict(self.train_metrics.compute())
        self.train_metrics.reset()

    def validation_step(  # type: ignore[override]
        self, batch: Dict[str, Any], batch_idx: int
    ) -> None:
        """Validation step - reports average accuracy and average IoU.

        Logs the first 10 validation samples to tensorboard as images with 3 subplots
        showing the image, mask, and predictions.

        Args:
            batch: Current batch
            batch_idx: Index of current batch
        """
        x = batch["image"]
        y = batch["mask"]
        y_hat = self.forward(x)
        y_hat_hard = y_hat.argmax(dim=1)

        loss = self.loss(y_hat, y)

        self.log("val_loss", loss, on_step=False, on_epoch=True)
        self.val_metrics(y_hat_hard, y)

    def validation_epoch_end(self, outputs: Any) -> None:
        """Logs epoch level validation metrics.

        Args:
            outputs: list of items returned by validation_step
        """
        self.log_dict(self.val_metrics.compute())
        self.val_metrics.reset()

    def test_step(  # type: ignore[override]
        self, batch: Dict[str, Any], batch_idx: int
    ) -> None:
        """Test step identical to the validation step.

        Args:
            batch: Current batch
            batch_idx: Index of current batch
        """
        x = batch["image"]
        y = batch["mask"]
        y_hat = self.forward(x)
        y_hat_hard = y_hat.argmax(dim=1)

        loss = self.loss(y_hat, y)

        # by default, the test and validation steps only log per *epoch*
        self.log("test_loss", loss, on_step=False, on_epoch=True)
        self.test_metrics(y_hat_hard, y)

    def test_epoch_end(self, outputs: Any) -> None:
        """Logs epoch level test metrics.

        Args:
            outputs: list of items returned by test_step
        """
        self.log_dict(self.test_metrics.compute())
        self.test_metrics.reset()

    def configure_optimizers(self) -> Dict[str, Any]:
        """Initialize the optimizer and learning rate scheduler.

        Returns:
            a "lr dict" according to the pytorch lightning documentation --
            https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html#configure-optimizers
        """
        optimizer = torch.optim.Adam(
            self.model.parameters(), lr=self.hparams["learning_rate"]
        )
        return {
            "optimizer": optimizer,
            "lr_scheduler": {
                "scheduler": ReduceLROnPlateau(
                    optimizer, patience=self.hparams["learning_rate_schedule_patience"]
                ),
                "monitor": "val_loss",
            },
        }


import logging
import time
import concurrent.futures
import threading

l = logging.getLogger("toll_booth")
h = logging.StreamHandler()
f = logging.Formatter("%(asctime)s: %(message)s")
h.setFormatter(f)
l.addHandler(h)
l.setLevel(logging.INFO)

class TollBooth:
    def __init__(self):
        self.register = 0.0
        self.__lock = threading.Lock()

    def process_fee(self, car, fee):
        l.info(f"Processing {car}'s fee. Current total: {self.register}")
        with self.__lock:
            new_total = self.register + fee  # Toll booth calculates a new total
            time.sleep(0.1)  # processing takes 0.1 seconds
            self.register = new_total  # New total is saved after 0.1 seconds
        # notice operations outside the critical section are still concurrent
        time.sleep(1)
        l.info(f"Done processing {car}'s fee. New total: {self.register}")

if __name__ == "__main__":
    cars = ["Red", "Blue", "Green"]
    booth = TollBooth()

    start_time = time.time()
    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
        for c in cars:
            executor.submit(booth.process_fee, c, 10.0)
    delta_time = time.time() - start_time
    print(f"It took {delta_time:.1f}s to process all the cars.")
    print(f"Total: {booth.register}")


import sys
import config
import daq
import pathlib
from datetime import datetime

def inspect_file(filename):

    """ Go through the given arch file and returns data for the index,
    it means, the name of the variable, first and last timestamp, the
    number of records and finally extreme values. """

    counter = 0
    first_timestamp = ''
    last_timestamp = ''
    # open given file
    records = daq.raw_read_from_file(filename)
    temp, var_name = records.__next__()
    for r in records:
        if len(r) == 2:
            counter += 1
            last_timestamp, value = r
            if counter == 1:
                first_timestamp = last_timestamp
    return str(filename), var_name, first_timestamp, last_timestamp, counter

def update_index():
    files = config.ARCH_PATH.glob('*.arch')
    records = [inspect_file(f) for f in files]
    for r in records:
        print(','.join(str(x) for x in r))

def load_index():
    index = []
    index_path = pathlib.Path(config.ARCH_PATH, 'index')
    with open(index_path) as index_file:
        index = index_file.read()
    index = index.split('\n')
    index = [r.split(',') for r in index]
    index = [
        {
            'file': pathlib.Path(r[0]),
            'name': r[1],
            'first_timestamp': datetime.strptime(r[2], '%Y-%m-%dT%H:%M:%S'),
            'last_timestamp': datetime.strptime(r[3], '%Y-%m-%dT%H:%M:%S'),
            'count': int(r[4])
        }
        for r in index if len(r) == 5
    ]
    return index

def filter_variable(index, variable):
    return [r for r in index if variable == r['name']]

def filter_timestamp(index, t1, t2):
    return [r for r in index if timestamp_intersection((t1, t2), (r['first_timestamp'], r['last_timestamp']))]

def timestamp_intersection(t1, t2):
    # find greater begen time
    intersection_start = t1[0] if t1[0] > t2[0] else t2[0]
    # and the smaller and time
    interscetion_end = t1[1] if t1[1] < t2[1] else t2[1]
    # and compare them
    if intersection_start < interscetion_end:
        return intersection_start, interscetion_end
    else:
        return None

if __name__ == "__main__":
    # execute only if run as a script
    if len(sys.argv) > 1:
        filename = sys.argv[1]
        result = inspect_file(filename)
        print(','.join(str(x) for x in result))
    else:
        update_index()


#!/usr/bin/python3
"""
Cities handler for all default RestFul API
"""
from api.v1.views import app_views
from flask import abort, jsonify, make_response, request
from models import storage
from models.amenity import Amenity

@app_views.route('/amenities', defaults={'amenity_id': ''},
                 methods=['GET'], strict_slashes=False)
@app_views.route('/amenities/<string:amenity_id>',
                 methods=['GET'], strict_slashes=False)
def get_amenities(amenity_id):
    """ Retrieve an amenitie object into a valid JSON """
    if amenity_id != '':
        amenity = storage.get(Amenity, amenity_id)
        if amenity is None:
            abort(404)
        return jsonify(amenity.to_dict())
    else:
        amenities = [amenity.to_dict()
                     for amenity in storage.all(Amenity).values()]
        return jsonify(amenities)

@app_views.route('amenities/<string:amenity_id>',
                 methods=['DELETE'], strict_slashes=False)
def delete_amenity(amenity_id):
    """ Delete a amenity """
    amenity = storage.get(Amenity, amenity_id)
    if amenity is None:
        abort(404)
    amenity.delete()
    storage.save()
    return make_response(jsonify({}), 200)

@app_views.route('/amenities/', methods=['POST'], strict_slashes=False)
def post_amenity():
    """ Create a amenity """
    if not request.get_json():
        return make_response(jsonify({'error': 'Not a JSON'}), 400)
    if 'name' not in request.get_json():
        return make_response(jsonify({'error': 'Missing name'}), 400)
    amenity = Amenity(**request.get_json())
    amenity.save()
    return make_response(jsonify(amenity.to_dict()), 201)

@app_views.route('/amenities/<string:amenity_id>',
                 methods=['PUT'], strict_slashes=False)
def put_amenity(amenity_id):
    """ Update a amenity """
    amenity = storage.get(Amenity, amenity_id)
    if amenity is None:
        abort(404)
    if not request.get_json():
        return make_response(jsonify({'error': 'Not a JSON'}), 400)
    for key, value in request.get_json().items():
        if key not in ['id', 'created_at', 'updated_at']:
            setattr(amenity, key, value)
    amenity.save()
    return make_response(jsonify(amenity.to_dict()), 200)


import numpy as np
from uncertainties import ufloat
from uncertainties import unumpy

dispersionsgebiet = 48.9*10**(-12)
h_bar = 6.62607 * 10**(-34)
c = 299792458
l = 643.8 * 10**(-9)
mu_B = 9.274 * 10**(-24)
B = ufloat(0.608, 0.014)

Ds, ds = np.genfromtxt('normal.txt', delimiter='\t', skip_header=1, skip_footer=0, unpack=True)

dlambda = 0.5 * ds / Ds * dispersionsgebiet

deltaE = h_bar * c / l**2 * dlambda

g_J = deltaE / (mu_B * B)

for i in range(len(Ds)):
	print(
		"%.0f" % (Ds[i]) + " & " +
		"%.0f" % (ds[i]) + " & " +
		"%.2f" % (dlambda[i] * 10**12) + " & " +
		"%.2f" % (g_J[i].nominal_value) + "\\pm" + "%.2f" % (g_J[i].std_dev) + "\\\\"
	)

mittelwert = ufloat(np.mean([v.nominal_value for v in g_J]), np.std([v.nominal_value for v in g_J]))
print(" ")
print(mittelwert)


from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
from datetime import datetime

from schema import models
from database import get_db
from crud import entrycrud
from utilities import update_recurring_entries, get_current_user

router = APIRouter(
    prefix='/entries',
    tags=['Entries']
)

# List of allowed categories
categories = ['Income', 'Housing', 'Food', 'Personal',
            'Entertainment', 'Transportation', 'Other Expense']

# Route to create a new database entry, by current user
@router.post("/", response_model=models.Entry)
def create_entry(
    entry: models.EntryBase, db: Session = Depends(get_db),
    current_user: models.User = Depends(get_current_user)
):
    return entrycrud.entry_create(db, entry, current_user.id)

# Route to edit entry (by entry id) of current user
@router.put("/{id}", response_model=models.EntryBase)
def update_entry(
    id: int, entry: models.EntryBase, db: Session = Depends(get_db),
    current_user: models.User = Depends(get_current_user)
):
    return entrycrud.entry_update(db, entry, id, current_user.id)

# Route to delete entry by entry id
@router.delete("/{id}")
def delete_entry(
    id: int, db: Session = Depends(get_db),
    current_user: models.User = Depends(get_current_user)
    ):
    return entrycrud.entry_delete(db, id, current_user.id)

# Route to get overview of month with category summaries and net
@router.get("/month/")
def month_overview(
    year: int = datetime.now().year, month: int = datetime.now().month,
    db: Session = Depends(get_db),
    current_user: models.User = Depends(get_current_user)
):
    amounts = {}
    # Iterates over all categories and adds non empty amounts to dict
    for category in categories:
        amount = 0
        entries = entrycrud.get_month_entries(current_user.id, year, month, db, category)
        if entries:
            for entry in entries:
                amount += entry.amount
            amounts[category] = amount
    # Calculate net expenditure
    amounts['Net'] = sum(x for x in amounts.values())
    return amounts

# Route to get overview of year with category summaries and net
@router.get("/year/{year}/")
def year_overview(
    year: int, db: Session = Depends(get_db),
    current_user: models.User = Depends(get_current_user)
    ):
    amounts = {}
    # Iterates over all categories and adds non empty amounts to dict
    for category in categories:
        amount = 0
        entries = entrycrud.get_year_entries(current_user.id, year, db, category)
        if entries:
            amount = sum(x[0] for x in entries)
            amounts[category] = amount
    # Calculate net expenditure
    amounts['Net'] = sum(x for x in amounts.values())
    return amounts

# Gets list of all entries in a month
@router.get("/month/{month}/", response_model=List[models.Entry])
def month_entries(
    month: int, year: int = datetime.now().year, db: Session = Depends(get_db),
    current_user: models.User = Depends(get_current_user)
):
    return entrycrud.get_month_entries(current_user.id, year, month, db)

# Gets list of all entries in a category for given month
@router.get("/month/{month}/{category}/", response_model=List[models.Entry])
def month_entries_by_category(
    category: str, month: int, year: int = datetime.now().year,
    db: Session = Depends(get_db),
    current_user: models.User = Depends(get_current_user)
):
    return entrycrud.get_month_entries(current_user.id, year, month, db, category)


import os
from urllib.request import urlopen, Request
import bs4 as bs
import random
import urllib.request
import requests
import shutil
import sys

##### RUN THE SFW DOWNLOADER FIRST!!!!! #####

while 1==1:
    dir1 = "C:/Path/To/IMG Sorter/all_data/" #Path to all data folder
    dir2 = "C:/Path/To/IMG Sorter/New_Data/" #Path to new data folder
    dir3 = "C:/Path/To/IMG Sorter/train/"    #Path to train data folder

    list1 = os.listdir(dir1)
    list2 = os.listdir(dir2)
    list3 = os.listdir(dir3)

    number_files = len(list1)
    if number_files > 800:
        sys.exit()
    else:
        name_file = str(number_files)

        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'}
        reg_url = "https://nekos.life/lewd"
        req = Request(url = reg_url, headers = headers)
        #Pinging Site For Images
        ping = urlopen(req)
        pinger = bs.BeautifulSoup(ping, 'lxml')

        source_frag2 = []

        for img in pinger.find('img').attrs['src']:
            source_frag1 = (img)
            source_frag2.append(source_frag1)

        source = ''.join(source_frag2)
        print("Downloading", source)
        #Saving File To Folder
        name = ("nsfw." + name_file + ".jpg")
        new_name = name_file + ".jpg"
        full_name1 = os.path.join(dir2, new_name)
        full_name2 = os.path.join(dir1, name)
        full_name3 = os.path.join(dir3, name)

        f = open(full_name1, 'wb')
        f.write(requests.get(source).content)
        f.close()

        f = open(full_name2, 'wb')
        f.write(requests.get(source).content)
        f.close()

        f = open(full_name3, 'wb')
        f.write(requests.get(source).content)
        f.close()


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 27 09:34:19 2019

@author: franzk
"""

import numpy as np
import matplotlib.pyplot as plt
from L1_functions import readimgpath, predict_image, get_true_image, desmear_true_image, compensate_bad_columns, get_true_image_from_compensated

# MATS data chain for raw images
# this script deals with:
# 1. Image prediction from reference image according to header
# 2. Applying bad column correction and removing offsets to get "true image"
# 3. Compensating bad columns in MATS payload OBC

image_display_adjustment = 100

# Import two reference images for the CCD in high and low signal modes
# Reference image depends on the temperature

ref_hsm_image, hsm_header = readimgpath('/home/franzk/Documents/MATS/L1_processing/data/2019-02-08 rand6/', 0, 0)
ref_lsm_image, lsm_header = readimgpath('/home/franzk/Documents/MATS/L1_processing/data/2019-02-08 rand6/', 4, 0)

recv_image, recv_header = readimgpath('/home/franzk/Documents/MATS/L1_processing/data/2019-02-08 rand6/', 1, 0)

#Step 1
# Predict the received image from reference image according to header
pred_image, pred_header = predict_image(ref_hsm_image, hsm_header, ref_lsm_image, lsm_header, recv_header)

#Step 2
# Make actual image out both received and predicted images
# These can be compared if no compression is used.

recv_true_image = get_true_image(recv_image, recv_header)
recv_true_image = desmear_true_image(recv_true_image, recv_header)

pred_true_image = get_true_image(pred_image, pred_header)

#Step 3
# Bad column compensation for MATS payload OBC
recv_comp_image = compensate_bad_columns(recv_image, recv_header)

# Step 4
# Getting "true image" from compensated one in MATS payload OBC
true_comp_image = get_true_image_from_compensated(recv_comp_image, recv_header)
true_comp_image = desmear_true_image(true_comp_image, recv_header)

mean_img = np.mean(np.mean(recv_image))

plt.imshow(recv_image, cmap='viridis', vmin=mean_img-image_display_adjustment, vmax=mean_img+image_display_adjustment,  extent=[0,75,0,511], aspect='auto')
plt.title('CCD image')
plt.xlabel('Pixels')
plt.ylabel('Pixels')
plt.show()

plt.imshow(pred_image, cmap='viridis', vmin=mean_img-image_display_adjustment, vmax=mean_img+image_display_adjustment,  extent=[0,75,0,511], aspect='auto')
plt.title('predicted image')
plt.xlabel('Pixels')
plt.ylabel('Pixels')
plt.show()

true_mean_img = np.mean(np.mean(recv_true_image))

plt.imshow(recv_true_image, cmap='viridis', vmin=true_mean_img-image_display_adjustment, vmax=true_mean_img+image_display_adjustment, extent=[0,75,0,511], aspect='auto')
plt.title('CCD true image')
plt.xlabel('Pixels')
plt.ylabel('Pixels')
plt.show()

plt.imshow(pred_true_image, cmap='viridis', vmin=true_mean_img-image_display_adjustment, vmax=true_mean_img+image_display_adjustment, extent=[0,75,0,511], aspect='auto')
plt.title('predicted true image')
plt.xlabel('Pixels')
plt.ylabel('Pixels')
plt.show()

plt.imshow(recv_comp_image, cmap='viridis', vmin=mean_img-image_display_adjustment, vmax=mean_img+image_display_adjustment, extent=[0,75,0,511], aspect='auto')
plt.title('compensated in OBC image')
plt.xlabel('Pixels')
plt.ylabel('Pixels')
plt.show()

plt.imshow(true_comp_image, cmap='viridis', vmin=true_mean_img-image_display_adjustment, vmax=true_mean_img+image_display_adjustment, extent=[0,75,0,511], aspect='auto')
plt.title('True image after compensation in OBC software')
plt.xlabel('Pixels')
plt.ylabel('Pixels')
plt.show()


import argparse
import os
import torch
import logging

import numpy as np
import torch.nn as nn

from utils import *
import utils_own
import network

import torch.optim as optim

parser = argparse.ArgumentParser(description='PyTorch CNN Training')

parser.add_argument('--save_dir', metavar='SAVE_DIR', default='./training_data/conv/', help='Save dir')
parser.add_argument('--batch', metavar='BATCH', default=256, type=int, help='Batch size to use')
parser.add_argument('--size', metavar='SIZE', default=0, type=int, help='Size of network. 1 means large and 0 means medium')
parser.add_argument('--dataset', metavar='DATASET', default='SVHN', help='Dataset to train on. Currently choose from SVHN and CIFAR10')
parser.add_argument('--full', metavar='FULL', default=0, type=int, help='If 1, train with full precision')
parser.add_argument('--binary', metavar='BINARY', default=0, type=int, help='If 1, train with dense binary')
parser.add_argument('--conv_sparsity', metavar='CONV_SPARSITY', default=0.9, type=float, help='Sparsity of the convolutional layers')
parser.add_argument('--fc_sparsity', metavar='FC_SPARSITY', default=0.9, type=float, help='Sparsity of fully-connected layers')
parser.add_argument('--permute', metavar='PERMUTE', default=-1, type=int, help='Permutation method. -1 means no permutation')
parser.add_argument('--pad', metavar='PAD', default=0, type=int, help='Padding value used')
parser.add_argument('--seed', metavar='SEED', default=0, type=int, help='Seed to use for this run')
parser.add_argument('--cpu', dest='cpu', default=False, action='store_true', help='Train using cpu')

def main():
    global args, best_prec1
    args = parser.parse_args()
    seed = args.seed
    torch.manual_seed(seed)
    np.random.seed(seed)
    torch.backends.cudnn.deterministic = True

    dataset = args.dataset
    conv_sparsity = args.conv_sparsity
    fc_sparsity = args.fc_sparsity
    permute = args.permute
    size = args.size
    batch = args.batch
    pad = args.pad
    pack = 32
    if args.full==1:
        full=True
    else:
        full=False
    if args.binary==1:
        binary=True
    else:
        binary=False
    device = 0

    trainset, testset, classes = utils_own.load_dataset(dataset)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch,
                                              shuffle=True, num_workers=2)
    testloader = torch.utils.data.DataLoader(testset, batch_size=batch,
                                             shuffle=False, num_workers=2)

    save_dir = args.save_dir
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    if args.size==1:
        net = network.CNN_large(full=full, binary=binary, conv_thres=conv_sparsity, fc_thres=fc_sparsity, align=True, pad=pad)
    elif args.size==0:
        net = network.CNN_medium(full=full, binary=binary, conv_thres=conv_sparsity, fc_thres=fc_sparsity, align=True, pad=pad)

    save_file = save_dir + '{0}_s_{1}'.format(dataset, size)

    setup_logging(save_file + '_log.txt')
    logging.info("saving to %s", save_file)

    save_file += '.pt'
    regime = getattr(net, 'regime', {0: {'optimizer': 'Adam', 'lr': 0.1,'momentum': 0.9,'weight_decay': 1e-4}})

    if not args.cpu:
        torch.cuda.empty_cache()
        net.cuda(device)

    learning_rate = 0.001
    criterion = nn.CrossEntropyLoss()
    lr_decay = np.power((2e-6/learning_rate), (1./100))

    optimizer = optim.Adam(net.parameters(), lr=learning_rate)

    ### Training section

    # Train without packing constraints
    utils_own.adjust_pack(net,1)
    for epoch in range(0, 25):
        optimizer = adjust_optimizer(optimizer, epoch, regime)
        train_loss, train_prec1, train_prec5 = utils_own.train(trainloader, net, criterion, epoch, optimizer)
        val_loss, val_prec1, val_prec5 = utils_own.validate(testloader, net, criterion, epoch, verbal=True)

    # Retrain with permutation + packing constraint
    utils_own.adjust_pack(net, pack)
    utils_own.permute_all_weights_once(net, pack=pack, mode=permute)

    for epoch in range(0, 25):
        optimizer = adjust_optimizer(optimizer, epoch, regime)
        train_loss, train_prec1, train_prec5 = utils_own.train(trainloader, net, criterion, epoch, optimizer)
        val_loss, val_prec1, val_prec5 = utils_own.validate(testloader, net, criterion, epoch, verbal=True)

    # Fix pruned packs and fine tune
    for mod in net.modules():
        if hasattr(mod, 'mask'):
            mod.mask = torch.abs(mod.weight.data)
    net.pruned = True
    best_prec1 = 0

    for epoch in range(0, 200):
        optimizer = adjust_optimizer(optimizer, epoch, regime)
        train_loss, train_prec1, train_prec5 = utils_own.train(trainloader, net, criterion, epoch, optimizer)
        val_loss, val_prec1, val_prec5 = utils_own.validate(testloader, net, criterion, epoch, verbal=False)
        # remember best prec@1 and save checkpoint
        is_best = val_prec1 > best_prec1
        best_prec1 = max(val_prec1, best_prec1)

        if is_best:
            torch.save(net, save_file)
            logging.info('\n Epoch: {0}\t'
                         'Training Loss {train_loss:.4f} \t'
                         'Training Prec {train_prec1:.3f} \t'
                         'Validation Loss {val_loss:.4f} \t'
                         'Validation Prec {val_prec1:.3f} \t'
                         .format(epoch+1, train_loss=train_loss, val_loss=val_loss,
                                 train_prec1=train_prec1, val_prec1=val_prec1))
    logging.info('\nTraining finished')

    # Extract data
    conv_count = 0
    bn2d_count = 0
    bn1d_count = 0
    fc_count = 0

    upload_dir = save_dir

    for mod in net.modules():
        if isinstance(mod, nn.Conv2d):
            print(mod)
            weight = mod.weight.data.type(torch.int16).cpu().numpy()
            conv_count += 1
            np.save(upload_dir+'conv_{0}_weight'.format(conv_count), weight)
        if isinstance(mod, nn.BatchNorm2d):
            print(mod)
            weight = mod.weight.data.cpu().numpy()
            bias = mod.bias.data.cpu().numpy()
            mean = mod.running_mean.cpu().numpy()
            var = mod.running_var.cpu().numpy()

            bn2d_count += 1
            np.save(upload_dir+'bn2d_{0}_weight'.format(bn2d_count), weight)
            np.save(upload_dir+'bn2d_{0}_bias'.format(bn2d_count), bias)
            np.save(upload_dir+'bn2d_{0}_mean'.format(bn2d_count), mean)
            np.save(upload_dir+'bn2d_{0}_var'.format(bn2d_count), var)

        if isinstance(mod, nn.Linear):
            print(mod)
            weight = mod.weight.data.type(torch.int16).cpu().numpy()
            fc_count += 1
            np.save(upload_dir+'fc_{0}_weight'.format(fc_count), weight)
        if isinstance(mod, nn.BatchNorm1d):
            print(mod)
            bn1d_count += 1
            if type(mod.weight) != type(None):
                weight = mod.weight.data.cpu().numpy()
                np.save(upload_dir+'bn1d_{0}_weight'.format(bn1d_count), weight)
            if type(mod.bias) != type(None):
                bias = mod.bias.data.cpu().numpy()
                np.save(upload_dir+'bn1d_{0}_bias'.format(bn1d_count), bias)
            mean = mod.running_mean.cpu().numpy()
            var = mod.running_var.cpu().numpy()

            np.save(upload_dir+'bn1d_{0}_mean'.format(bn1d_count), mean)
            np.save(upload_dir+'bn1d_{0}_var'.format(bn1d_count), var)

    return 0

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
plt.style.use("ggplot")

K = 2
M = 1
N = 1000

# 実際の値
mu1 = 1
sigma1 = 0.2
N1 = int(N*0.3)
mu2 = 5
sigma2 = 1
N2 = int(N*0.7)

x = np.concatenate([np.random.normal(mu1, sigma1, N1), np.random.normal(mu2, sigma2, N2)])
plt.hist(x, bins=100, normed=True)
plt.show()

# 初期値
w = np.random.uniform(0,1,K)
w /=  w.sum()
mu = [3, 10]
sigma = [1, 1]
ita = np.zeros([N, K])

# EM アルゴリズム
training_iter = 50
for epoch in range(training_iter):
    # Estep
    for k in range(K):
        ita[:, k] = w[k] * stats.norm(mu[k], sigma[k]).pdf(x)
    ita = ita/ita.sum(1)[:, np.newaxis]

    # Mstep
    w = ita.sum(0) / N
    mu = ita.T.dot(x) / ita.sum(0)
    for k in range(K):
        sigma[k] = np.sqrt(((x - mu[k]) ** 2 * ita[:, k]).sum(0) / ita[:, k].sum() * M)
    # 図示
    x_ = np.linspace(0, 8, 200)
    y0 = w[0] * stats.norm(mu[0], sigma[0]).pdf(x_)
    y1 = w[1] * stats.norm(mu[1], sigma[1]).pdf(x_)
    plt.plot(x_, y0)
    plt.plot(x_, y1)
    plt.hist(x, bins=100, normed=True)
    title = "epoch: {}".format(epoch+1)
    plt.title(title)
    # plt.savefig("data/" + title + ".png")
    plt.show()


print(__name__)
from lib.db.connect import connect_db

# Doliprane
cis = "67119691"
code = "02202"
i = 0

db_engine = connect_db()
conn = db_engine.raw_connection()
cursor = conn.cursor()

def create_cis_copy_query(table):
    return f"COPY (SELECT * FROM {table} WHERE cis = '{cis}') TO STDOUT WITH CSV HEADER"

def create_substance_copy_query(table):
    return (
        f"COPY (SELECT * FROM {table} WHERE code = '{code}') TO STDOUT WITH CSV HEADER"
    )

def create_copy_query(table):
    return f"COPY (SELECT * FROM {table}) TO STDOUT WITH CSV HEADER"

def create_file(table):
    return open(f"/tmp/{table}_subset.csv", "wb")

def create_table(table_name, fn_query):
    global i
    query = fn_query(table_name)
    file = create_file(table_name)
    cursor.copy_expert(query, file)
    i += 1

def run():
    cis_index_tables = [
        "specialite",
        "presentation",
        "erreur_med_cause",
        "erreur_med_cis_denomination",
        "erreur_med_effet_indesirable",
        "erreur_med_gravite",
        "erreur_med_initial",
        "erreur_med_lieu",
        "erreur_med_nature",
        "erreur_med_population",
        "icones",
        "specialite_atc",
        "specialite_exposition",
        "specialite_patient_age_ordei",
        "specialite_patient_sexe_ordei",
        "specialite_substance",
    ]
    substance_index_tables = [
        "substance",
        "substance_cas_age_ordei",
        "substance_cas_grave_ordei",
        "substance_cas_sexe_ordei",
        "substance_exposition",
        "substance_hlt_ordei",
        "substance_notif_ordei",
        "substance_patient_age_ordei",
        "substance_patient_sexe_ordei",
        "substance_soclong_ordei",
    ]
    no_filter_tables = ["classes_atc"]

    for table in cis_index_tables:
        create_table(table, create_cis_copy_query)

    for table in substance_index_tables:
        create_table(table, create_substance_copy_query)

    for table in no_filter_tables:
        create_table(table, create_copy_query)
    print(f"{i} files created in /tmp")


import matplotlib.pyplot as plt
import numpy as np

# x = [1,2,3,4,5,6,7,8,9]
# y = x

# fig, ax = plt.subplots()
# ax.plot(x, y)
# plt.show()

y_values = []
with open('/Users/rileybusche/Development/nmr_data_analysis/LVR_Diffusion/ph7.59/Trial1/1.txt') as data_file:
    for line in data_file:
        if 'LEFT' in line:
            print(line)
        if '#' not in line:
            y_values.append(float(line.strip()))

print(len(y_values))

fig, ax = plt.subplots()
ax.plot(list(range(len(y_values))), y_values)
plt.show()


from django.shortcuts import render
from django.http import HttpResponse
from .models import Board, Category, Good
from django.core.paginator import Paginator
from .forms import SearchForm, BoardForm
from django.shortcuts import redirect
from django.db.models import Count

# Create your views here.

def index(request, num=1):
    data = Board.objects.all()
    page = Paginator(data, 3)
    message = 'index'
    params = {
        'data' : page.get_page(num),
        'form' : SearchForm(),
        'message' : message,
    }
    return render(request, 'board/index.html', params)

def search(request, num=1):
    form = SearchForm(request.POST)
    str = request.POST['search']
    data = Board.objects.filter(title__contains=str)
    page = Paginator(data, 3)
    message = 'post'
    params = {
        'form' : form,
        'data' : page.get_page(num),
        'message' : message,
    }
    return render(request, 'board/index.html', params)

def board_main(request, num):
    board_main = Board.objects.get(id=num)
    board_good = Good.objects.filter(board_id=num, user_id=request.user.id).aggregate(Count('id'))
    params={
        'id' : num,
        'obj' : board_main,
        'message' : 'メイン'+str(num),
        'goodFlg' : board_good['id__count'],
    }
    return render(request, 'board/board_main.html', params)

def create(request):
    params={
        'form' : BoardForm(),
    }
    if (request.method == 'POST'):
        bd = Board()
        bd.category = Category.objects.filter(name=request.POST['category']).first()
        bd.title = request.POST['title']
        bd.main_text = request.POST['main_text']
        bd.user= request.user
        bd.save()
        return redirect(to='/board')
    return render(request, 'board/create.html', params)

def update(request, num):
#    board_update = Board.objects.get(id=num)
#    BoardForm.category.valid_value = Board.objects.get(id=num).category
#    BoardForm.title.__init__ = board_update.title
#    BoardForm.main_text = Board.objects.get(id=num).main_text

    if (request.method == 'POST'):
        board_update = Board.objects.get(id=num)
        board_update.category = Category.objects.filter(name=request.POST['category']).first()
        board_update.title = request.POST['title']
        board_update.main_text = request.POST['main_text']
        board_update.save()
        return redirect(to='/board')

    # data = Board.objects.filter(id=num).values('category', 'title', 'main_text')

    # params = {
    #     'title' : '変更画面',
    #     'id' : num,
    #     'form' : BoardForm(),
    #     'data' : data
    # }
    # return render(request, 'board/board_update.html', params)

def update_main(request, num):
    update_main = Board.objects.get(id=num)
    data = BoardForm(initial={
        "title":update_main.title,
        "main_text":update_main.main_text,
        "category":update_main.category
        })
    params = {
        'title' : '変更画面',
        'id' : num,
        'form' : data,
        'data' : update_main,
    }
    return render(request, 'board/board_update.html', params)

def delete(request, num):
    board_delete = Board.objects.get(id=num)
    board_delete.delete()
    return redirect(to='/board')

def goodUpdate(request, num):
    board = Board.objects.get(id=num)
    board.good_cnt += 1
    board.save()
    good = Good()
    good.board_id = num
    good.user_id = request.user.id
    good.save()
    return redirect(to='/board')