#    (C) Copyright 2018 Anthony D. Dutoi
#
#    This file is part of Qode.
#
#    Qode is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Qode is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Qode.  If not, see <http://www.gnu.org/licenses/>.
#
# This Module Loads Molecular Eigenvalues and Paired Coupling Matrix Values into corresponding blocks
# for Harmonic Oscillator Calculations

# from Applications.reorder_play.test import load_H
# from Applications.reorder_play.maps import map_main
from attic.oscillator_ccsd.old_qode.generate_ho_config import  diag_rec_v_matrix
from qode.many_body.local_operator.local_operator import cc_operator, init_K, init_blocks
import numpy as np

def v_list_find_idx(i, j, j_max):
	if i >= j:
		raise AssertionError
	idx = 0
	for x in range(i):
		idx += j_max-(x+1)-1
	idx += j - 1
	return idx

block_dict ={	'Ex': ['OCC','VRT'],
				'Dx': ['VRT','OCC'],
				'Fo': ['OCC','OCC'],
				'Fv': ['VRT','VRT'],
				'ExEx': ['OCC','VRT','OCC','VRT'],
				'ExFo': ['OCC','VRT','OCC','OCC'],
				'ExFv': ['OCC','VRT','VRT','VRT'],
				'ExDx': ['OCC','VRT','VRT','OCC'],
				'FoFo': ['OCC','OCC','OCC','OCC'],
				'FvFv': ['VRT','VRT','VRT','VRT'],
				'FoDx': ['OCC','OCC','VRT','OCC'],
				'FvDx': ['VRT','VRT','VRT','OCC'],
				'FoFv': ['OCC','OCC','VRT','VRT'],
				'DxDx': ['VRT','OCC','VRT','OCC'],
				'ExExDx': ['OCC','VRT','OCC','VRT','VRT','OCC'],
				'ExFoFo': ['OCC','VRT','OCC','OCC','OCC','OCC'],
				'ExFvFv': ['OCC','VRT','VRT','VRT','VRT','VRT'],
				'ExFoDx': ['OCC','VRT','OCC','OCC','VRT','OCC'],
				'ExFvDx': ['OCC','VRT','VRT','VRT','VRT','OCC'],
				'ExFoFv': ['OCC','VRT','OCC','OCC','VRT','VRT'] }

# def two_idx(i,j):
# 	abs_idx = 0
# 	for x in range(i):
# 		abs_idx += x + 1
# 	abs_idx += j
# 	return abs_idx

# def three_idx(i,j,k):
# 	abs_idx = 0
# 	for x in range(i):
# 		for y in range(x+1):
# 			abs_idx += y + 1
# 	for y in range(j):
# 		abs_idx += y + 1
# 	abs_idx += k
# 	return abs_idx

'''
n_mol = 4
for i in range(n_mol):
	for j in range(i+1, n_mol):
		print("Mol", i,j, "=>", v_list_find_idx(i,j,n_mol))

for m in range(n_mol):
	for n in range(m+1):
		print("Mol", m,n,'->',two_idx(m,n))

for m in range(n_mol):
	for n in range(m+1):
		for k in range(n+1):
			print("Mol",m,n,k,'-->',three_idx(m,n,k))
'''

'''
		self.K      = None
		self.Ex     = None
		self.Fo     = None
		self.Fv     = None
		self.Dx     = None
		self.ExEx   = None   # diag = zero
		self.ExFo   = None   # diag = zero
		self.ExFv   = None   # diag = zero
		self.ExDx   = None
		self.FoFo   = None
		self.FvFv   = None
		self.FoFv   = None   # diag = zero
		# self.FvFo   = None   # diag = zero --> removed in the latest version (Aug 29, 2016)
		self.FoDx   = None   # diag = zero
		self.FvDx   = None   # diag = zero
		self.DxDx   = None
		self.ExFoFo = None
		self.ExFvFv = None
		self.ExFoFv = None
		# self.ExFvFo = None # --> removed in the latest version (Aug 29, 2016)
		self.ExFoDx = None
		self.ExFvDx = None
		self.ExExDx = None '''

def init_H_op(cc_obj, rec):
	cc_obj.K,  cc_obj.K_starters, cc_obj.K_int_dim, cc_obj.K_dim   = init_K()
	cc_obj.Ex, cc_obj.Ex_starters, cc_obj.Ex_int_dim, cc_obj.Ex_dim   = init_blocks(rec, block_dict['Ex'])
	cc_obj.Fo, cc_obj.Fo_starters, cc_obj.Fo_int_dim, cc_obj.Fo_dim   = init_blocks(rec, block_dict['Fo'])
	cc_obj.Fv, cc_obj.Fv_starters, cc_obj.Fv_int_dim, cc_obj.Fv_dim   = init_blocks(rec, block_dict['Fv'])
	cc_obj.Dx, cc_obj.Dx_starters, cc_obj.Dx_int_dim, cc_obj.Dx_dim   = init_blocks(rec, block_dict['Dx'])
	cc_obj.ExEx, cc_obj.ExEx_starters, cc_obj.ExEx_int_dim, cc_obj.ExEx_dim = init_blocks(rec, block_dict['ExEx'])
	cc_obj.ExFo, cc_obj.ExFo_starters, cc_obj.ExFo_int_dim, cc_obj.ExFo_dim = init_blocks(rec, block_dict['ExFo'])
	cc_obj.ExFv, cc_obj.ExFv_starters, cc_obj.ExFv_int_dim, cc_obj.ExFv_dim = init_blocks(rec, block_dict['ExFv'])
	cc_obj.ExDx, cc_obj.ExDx_starters, cc_obj.ExDx_int_dim, cc_obj.ExDx_dim = init_blocks(rec, block_dict['ExDx'])
	cc_obj.FoFo, cc_obj.FoFo_starters, cc_obj.FoFo_int_dim, cc_obj.FoFo_dim = init_blocks(rec, block_dict['FoFo'])
	cc_obj.FvFv, cc_obj.FvFv_starters, cc_obj.FvFv_int_dim, cc_obj.FvFv_dim = init_blocks(rec, block_dict['FvFv'])
	cc_obj.FoDx, cc_obj.FoDx_starters, cc_obj.FoDx_int_dim, cc_obj.FoDx_dim = init_blocks(rec, block_dict['FoDx'])
	cc_obj.FvDx, cc_obj.FvDx_starters, cc_obj.FvDx_int_dim, cc_obj.FvDx_dim = init_blocks(rec, block_dict['FvDx'])
	cc_obj.FoFv, cc_obj.FoFv_starters, cc_obj.FoFv_int_dim, cc_obj.FoFv_dim = init_blocks(rec, block_dict['FoFv'])
	cc_obj.DxDx, cc_obj.DxDx_starters, cc_obj.DxDx_int_dim, cc_obj.DxDx_dim = init_blocks(rec, block_dict['DxDx'])
	return cc_obj

def load_K(H_obj, all_mol_eigenvalues):
	for i in range(len(all_mol_eigenvalues)):
		H_obj.K[0] += all_mol_eigenvalues[i][0]
	return H_obj

def load_eigen_values_into_H(H_obj, rec, all_mol_eigenvalues):
	num_mol = len(rec)
	Nv = [ num-1 for num in rec ]
	#
	# Fo Parts:  <0|h|0>
	#
	for i in range(num_mol):
		H_obj.Fo[i] = -1.0*all_mol_eigenvalues[i][0]
	#
	# Fv Parts: <a|h|a>
	#
	for n in range(num_mol):
		for a in range(Nv[n]):
			H_obj.Fv[ H_obj.Fv_starters[n] + a * (Nv[n]+1) ] += all_mol_eigenvalues[n][a+1] # This is CORRECT!!
	# Ex Parts: All Zero
	# Dx Parts: All Zero
	return H_obj

def load_v_matrice_into_H(H_obj, rec, v_mat_list):
	num_mol = len(rec)
	Nv = [ num-1 for num in rec ]
	V_mat = diag_rec_v_matrix(rec, v_mat_list)
	#
	#
	# ExEx[i,a,j,b]  m<n: <ij|V|ab>; m>n: <ji|V|ba>
	for m in range(num_mol):
		for n in range(num_mol):
			if m > n:
				for a in range(Nv[m]):
					for b in range(Nv[n]):
						# H_obj.ExEx[ H_obj.ExEx_starters[m*num_mol+n] + a*Nv[n] + b ] +=  v_mat_list[ v_list_find_idx(n,m,num_mol) ][0][(b+1)*rec[m] + a+1]
						H_obj.ExEx[ H_obj.ExEx_starters[m*num_mol+n] + a*Nv[n] + b ] +=  V_mat( n, m, 0, 0, b+1, a+1 )

	# ExFo: ExFo[i,a,j,k]  m<n: <ij|V|ak>; m>n: <ji|V|ka>
	for m in range(num_mol):
		for n in range(num_mol):
			if m < n:
				for a in range(Nv[m]):
					# H_obj.ExFo[ H_obj.ExFo_starters[m*num_mol+n] + a ] +=  -v_mat_list[ v_list_find_idx(m,n,num_mol) ][0][a+1]
					H_obj.ExFo[ H_obj.ExFo_starters[m*num_mol+n] + a ] +=  -V_mat( m, n, 0, 0, a+1, 0 )
			elif m > n:
				for a in range(Nv[m]):
					# H_obj.ExFo[ H_obj.ExFo_starters[m*num_mol+n] + a ] +=  -v_mat_list[ v_list_find_idx(n,m,num_mol) ][0][a+1]
					H_obj.ExFo[ H_obj.ExFo_starters[m*num_mol+n] + a ] +=  -V_mat( n, m, 0, 0, 0, a+1 )

	# ExFv: ExFv[i,a,b,c]  m<n: <ib|V|ac>;  m>n: <bi|V|ca>
	for m in range(num_mol):
		for n in range(num_mol):
			if m < n:
				for a in range(Nv[m]):
					for b in range(Nv[n]):
						for c in range(Nv[n]):
							# H_obj.ExFv[ H_obj.ExFv_starters[m*num_mol+n] + a*Nv[n]**2 + b*Nv[n] + c ] +=  v_mat_list[ v_list_find_idx(m,n,num_mol) ][ b+1 ][ (a+1)*rec[n]+(c+1) ]
							H_obj.ExFv[ H_obj.ExFv_starters[m*num_mol+n] + a*Nv[n]**2 + b*Nv[n] + c ] += V_mat( m, n, 0, b+1, a+1, c+1 )
			if m > n:
				for a in range(Nv[m]):
					for b in range(Nv[n]):
						for c in range(Nv[n]):
							# H_obj.ExFv[ H_obj.ExFv_starters[m*num_mol+n] + a*Nv[n]**2 + b*Nv[n] + c ] +=  v_mat_list[ v_list_find_idx(n,m,num_mol) ][ b+1 ][ (c+1)*rec[m]+(a+1)]
							H_obj.ExFv[ H_obj.ExFv_starters[m*num_mol+n] + a*Nv[n]**2 + b*Nv[n] + c ] += V_mat( n, m, b+1, 0, c+1, a+1 )

	# ExDx ExDx[i,a,b,j]  <ib|V|aj>   <bi|V|ja>
	for m in range(num_mol):
		for n in range(num_mol):
			if m < n:
				for a in range(Nv[m]):
					for b in range(Nv[n]):
						# print("ExDx[%d,%d,%d,%d] = %lf" %(0,a,b,0, -v_mat_list[ v_list_find_idx(m,n,num_mol) ][ b+1 ][ a+1 ]))
						# H_obj.ExDx[ H_obj.ExDx_starters[m*num_mol+n] + a*Nv[n] + b ] +=  -v_mat_list[ v_list_find_idx(m,n,num_mol) ][ b+1 ][ a+1 ]
						H_obj.ExDx[ H_obj.ExDx_starters[m*num_mol+n] + a*Nv[n] + b ] +=  -V_mat( m, n, 0, b+1, a+1, 0 )
			elif m > n:
				for a in range(Nv[m]):
					for b in range(Nv[n]):
						# print("ExDx[%d,%d,%d,%d] = %lf" %(0,a,b,0, -v_mat_list[ v_list_find_idx(n,m,num_mol) ][ b+1 ][ a+1 ]))
						# H_obj.ExDx[ H_obj.ExDx_starters[m*num_mol+n] + a*Nv[n] + b ] +=  -v_mat_list[ v_list_find_idx(n,m,num_mol) ][ b+1 ][ a+1 ]
						H_obj.ExDx[ H_obj.ExDx_starters[m*num_mol+n] + a*Nv[n] + b ] +=  -V_mat( n, m, b+1, 0, 0, a+1 )

	# FoFo: FoFo[i,j,k,l]  <ik|V|jl>   <ki|V|lj>
	for m in range(num_mol):
		for n in range(num_mol):
			if m > n:
				# H_obj.FoFo[ H_obj.FoFo_starters[m*num_mol+n] ] +=  v_mat_list[ v_list_find_idx(n,m,num_mol) ][0][0]
				H_obj.FoFo[ H_obj.FoFo_starters[m*num_mol+n] ] +=  V_mat( n, m, 0, 0, 0, 0 )

	# FvFv: FvFv[a,b,c,d]   <ac|V|bd>   <ca|V|db>
	for m in range(num_mol):
		for n in range(num_mol):
			if m > n:
				for a in range(Nv[m]):
					for b in range(Nv[m]):
						for c in range(Nv[n]):
							for d in range(Nv[n]):
								# H_obj.FvFv[ H_obj.FvFvs_starters[m*num_mol+n] +  a*Nv[m]*Nv[n]**2 + b*Nv[n]**2 + c*Nv[n] + d ] += v_mat_list[ v_list_find_idx(n,m,num_mol) ][(c+1)*rec[m] + a+1 ][ (d+1)*rec[m]+ b+1 ]
								H_obj.FvFv[ H_obj.FvFv_starters[m*num_mol+n] +  a*Nv[m]*Nv[n]**2 + b*Nv[n]**2 + c*Nv[n] + d ] += V_mat( n, m, c+1, a+1, d+1, b+1 )

	# FoDx: FoDx[i,j,a, k]   <ia|V|jk>  <ai|V|kj>
	for m in range(num_mol):
		for n in range(num_mol):
			if m < n:
				for a in range(Nv[n]):
					# H_obj.FoDx[ H_obj.FoDx_starters[m*num_mol+n] + a ] += v_mat_list[ v_list_find_idx(m,n,num_mol) ][ a+1 ][ 0 ]
					H_obj.FoDx[ H_obj.FoDx_starters[m*num_mol+n] + a ] += V_mat( m, n, 0, a+1, 0, 0 )
			elif m > n:
				for a in range(Nv[n]):
					# H_obj.FoDx[ H_obj.FoDx_starters[m*num_mol+n] + a ] += v_mat_list[ v_list_find_idx(n,m,num_mol) ][ a+1 ][ 0 ]
					H_obj.FoDx[ H_obj.FoDx_starters[m*num_mol+n] + a ] += V_mat( n, m, a+1, 0, 0, 0 )

	# FvDx: FvDx[a,b,c,i]  <ac|V|bi>  <ca|V|ib>
	for m in range(num_mol):
		for n in range(num_mol):
			if m < n:
				for a in range(Nv[m]):
					for b in range(Nv[m]):
						for c in range(Nv[n]):
							# H_obj.FvDx[ H_obj.FvDx_starters[m*num_mol+n] + a*Nv[m]*Nv[n] + b*Nv[n] + c ] += -v_mat_list[ v_list_find_idx(m,n,num_mol) ][ (a+1)*rec[n] + (c+1) ][ b+1 ]
							H_obj.FvDx[ H_obj.FvDx_starters[m*num_mol+n] + a*Nv[m]*Nv[n] + b*Nv[n] + c ] += -V_mat( m, n, a+1, c+1, b+1, 0 )
			elif m > n:
				for a in range(Nv[m]):
					for b in range(Nv[m]):
						for c in range(Nv[n]):
							# H_obj.FvDx[ H_obj.FvDx_starters[m*num_mol+n] + a*Nv[m]*Nv[n] + b*Nv[n] + c ] += -v_mat_list[ v_list_find_idx(n,m,num_mol) ][ (c+1)*rec[m] + (a+1) ][ b+1 ]
							H_obj.FvDx[ H_obj.FvDx_starters[m*num_mol+n] + a*Nv[m]*Nv[n] + b*Nv[n] + c ] += -V_mat( n, m, c+1, a+1, 0, b+1 )

	# FoFv:  FoFv[i,j,a,b]  <ia|V|jb>  <ai|V|bj>
	for m in range(num_mol):
		for n in range(num_mol):
			if m < n:
				for a in range(Nv[n]):
					for b in range(Nv[n]):
						# H_obj.FoFv[ H_obj.FoFv_starters[m*num_mol+n] + a*Nv[n] + b ] +=  -v_mat_list[ v_list_find_idx(m,n,num_mol) ][ a+1 ][ b+1 ]
						H_obj.FoFv[ H_obj.FoFv_starters[m*num_mol+n] + a*Nv[n] + b ] += -V_mat( m, n, 0, a+1, 0, b+1 )
			elif m > n:
				for a in range(Nv[n]):
					for b in range(Nv[n]):
						# H_obj.FoFv[ H_obj.FoFv_starters[m*num_mol+n] + a*Nv[n] + b ] +=  -v_mat_list[ v_list_find_idx(n,m,num_mol) ][ a+1 ][ b+1 ]
						H_obj.FoFv[ H_obj.FoFv_starters[m*num_mol+n] + a*Nv[n] + b ] += -V_mat( n, m, a+1, 0, b+1, 0 )

	# DxDx: DxDx[a,i,b,j]  <ab|V|ij>  <ba|V|ji>
	for m in range(num_mol):
		for n in range(num_mol):
			if m > n:
				for a in range(Nv[m]):
					for b in range(Nv[n]):
						# H_obj.DxDx[ H_obj.DxDx_starters[m*num_mol+n] + a*Nv[n] + b ] +=  v_mat_list[ v_list_find_idx(n,m,num_mol) ][(b+1)*rec[m] + a+1][0]
						H_obj.DxDx[ H_obj.DxDx_starters[m*num_mol+n] + a*Nv[n] + b ] += V_mat( n, m, b+1, a+1, 0, 0 )

	return H_obj

class cc_fast_obj(object):
	"""operator class holding H, X, T, or Omega components"""
	def __init__(self):
		self.K      = None
		self.Ex     = None
		self.Fo     = None
		self.Fv     = None
		self.Dx     = None
		self.ExEx   = None
		self.ExFo   = None
		self.ExFv   = None
		self.ExDx   = None
		self.FoFo   = None
		self.FvFv   = None
		self.FoFv   = None
		self.FoDx   = None
		self.FvDx   = None
		self.DxDx   = None
		self.ExFoFo = None
		self.ExFvFv = None
		self.ExFoFv = None
		self.ExFoDx = None
		self.ExFvDx = None
		self.ExExDx = None

def print_amps(cc_obj):
	cc_obj_dict = cc_obj.__dict__
	for key in sorted(cc_obj_dict.keys()):
		if '_' not in key and len(key) <5:
			print(key)
			print(cc_obj_dict[key])

def get_fast_op(fast_obj, rec_num_states ):
	No = len(rec_num_states)
	Nv = 0
	for n in rec_num_states:
		Nv += n-1
	fast_obj.K      = np.zeros(1)
	fast_obj.Ex     = np.zeros((No*Nv))
	fast_obj.Fo     = np.zeros((No*No))
	fast_obj.Fv     = np.zeros((Nv*Nv))
	fast_obj.Dx     = np.zeros((Nv*No))
	fast_obj.ExEx   = np.zeros(((No*Nv)**2))
	fast_obj.ExFo   = np.zeros((Nv*No**3))
	fast_obj.ExFv   = np.zeros((No*Nv**3))
	fast_obj.ExDx   = np.zeros(((No*Nv)**2))
	fast_obj.FoFo   = np.zeros((No**4))
	fast_obj.FvFv   = np.zeros((Nv**4))
	fast_obj.FoDx   = np.zeros((Nv*No**3))
	fast_obj.FvDx   = np.zeros((No*Nv**3))
	fast_obj.DxDx   = np.zeros(((No*Nv)**2))
	fast_obj.FoFv   = np.zeros(((No*Nv)**2))
	# fast_obj.ExFoFo = np.zeros((No**5*Nv))
	# fast_obj.ExFvFv = np.zeros((Nv**5*No))
	# fast_obj.ExFoDx = np.zeros(((No*Nv)**2*No**2))
	# fast_obj.ExFvDx = np.zeros(((No*Nv)**2*Nv**2))
	# fast_obj.ExExDx = np.zeros(((No*Nv)**3))

	return fast_obj

def load_values_into_blocks(rec_num_states, all_mol_eigenvalues, v_mat_list):
	H = init_H_op(cc_operator(), rec_num_states)
	H = load_K(H, all_mol_eigenvalues)
	H = load_eigen_values_into_H(H, rec_num_states, all_mol_eigenvalues)
	H = load_v_matrice_into_H(H, rec_num_states, v_mat_list)

	# H_fast = get_fast_op(cc_fast_obj(), rec_num_states)
	# H_fast = load_H( H,  H_fast, rec_num_states, all_mol_eigenvalues, v_mat_list )
	# H_fast = map_main(rec_num_states, H, H_fast)

	# print_amps(H)

	# print("\n\n\n------------------------------------------------------------------------------")
	# print_amps(H_fast)

	# amp_dict = H_fast.__dict__
	# for key in amp_dict.keys():
	# 	if amp_dict[key] != None:
	# 		np.savetxt('local_'+key+'.txt', amp_dict[key])
	# 	else:
	# 		f= open('local_'+key+'.txt', 'w')
	# 		f.write("None")
	# 		f.close()

	# print("\n\n\n------------------------------------------------------------------------------")
	# raise AssertionError
	return H

'''
if __name__ == "__main__":
	n = 4
	n_states = 4
	rec = [n_states for i in range(n)]
	all_mol_eigenvalues = [ [j+1.0 for j in range(n_states)] for i in range(n)]
	v_mat_list = []
	for i in range(n):
		for j in range(i+1,n):
			v_mat_list += [ np.arange(1, n_states**4+1, 1,np.float64).reshape((n_states**2, n_states**2)) ]

	# print(rec, all_mol_eigenvalues)
	H = load_values_into_blocks(rec, all_mol_eigenvalues, v_mat_list)

	for i in range(n):
		print("H.Fo[%d] = %.1f" %(i,H.Fo[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.Fv)):
		print("H.Fv[%d] = %.1f" %(i,H.Fv[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.ExEx)):
		print("H.ExEx[%d] = %.1f" %(i,H.ExEx[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.ExFo)):
		print("H.ExFo[%d] = %.1f" %(i,H.ExFo[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.ExFv)):
		print("H.ExFv[%d] = %.1f" %(i,H.ExFv[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.ExDx)):
		print("H.ExDx[%d] = %.1f" %(i,H.ExDx[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.FoFo)):
		print("H.FoFo[%d] = %.1f" %(i,H.FoFo[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.FvFv)):
		print("H.FvFv[%d] = %.1f" %(i,H.FvFv[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.FoDx)):
		print("H.FoDx[%d] = %.1f" %(i,H.FoDx[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.FvDx)):
		print("H.FvDx[%d] = %.1f" %(i,H.FvDx[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
	for i in range(len(H.FoFv)):
		print("H.FoFv[%d] = %.1f" %(i,H.FoFv[i]), end="\t")
		if i % 10 == 0:
			print(' ')
	print(" ")
'''


# Geo-sort proxies
# Code mostly stolen from cvmfs-server's cvmfs_geo.py (which dwd also wrote).
#  Depends on data files and GeoIP library from cvmfs-server package.

import math
import string
import re
import bisect
import socket
import time
import maxminddb

gireader = maxminddb.open_database("/var/lib/cvmfs-server/geo/GeoLite2-City.mmdb")

proxygirs = {}
lookup_ttl = 60*5       # 5 minutes

def getgeoiprecord(addr):
    gir = gireader.get(addr)
    if gir is not None:
        gir = gir['location']
    return gir

# function came from http://www.johndcook.com/python_longitude_latitude.html
def distance_on_unit_sphere(lat1, long1, lat2, long2):

    if (lat1 == lat2) and (long1 == long2):
        return 0.0

    # Convert latitude and longitude to
    # spherical coordinates in radians.
    degrees_to_radians = math.pi/180.0

    # phi = 90 - latitude
    phi1 = (90.0 - lat1)*degrees_to_radians
    phi2 = (90.0 - lat2)*degrees_to_radians

    # theta = longitude
    theta1 = long1*degrees_to_radians
    theta2 = long2*degrees_to_radians

    # Compute spherical distance from spherical coordinates.

    # For two locations in spherical coordinates
    # (1, theta, phi) and (1, theta, phi)
    # cosine( arc length ) =
    #    sin phi sin phi' cos(theta-theta') + cos phi cos phi'
    # distance = rho * arc length

    cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
           math.cos(phi1)*math.cos(phi2))
    arc = math.acos( cos )

    # Remember to multiply arc by the radius of the earth
    # in your favorite set of units to get length.
    return arc

def sort_proxies(rem_addr, proxies):

    gir_rem = getgeoiprecord(rem_addr)
    if gir_rem is None:
        return [[], 'remote addr not found in database']

    idx = 0
    arcs = []
    orderedproxies = []

    onegood = False
    now = int(time.time())
    for proxy in proxies:
        colon = proxy.find(':')
        if colon == -1:
            name = proxy
            proxy += ':3128'
        else:
            name = proxy[0:colon]
        if (proxy not in proxygirs) or (proxygirs[proxy][1] < (now - lookup_ttl)):
            # Look up names periodically in case their IP values have changed
            #    even though it's unlikely their geo info has changed
            ai = ()
            try:
                ai = socket.getaddrinfo(name,80,0,0,socket.IPPROTO_TCP)
            except:
                pass

            # Try IPv4 first since that geo info is currently more reliable,
            #     and most machines today are dual stack if they have IPv6
            gir_proxy = None
            for info in ai:
                # look for IPv4 address first
                if info[0] == socket.AF_INET:
                    gir_proxy = getgeoiprecord(info[4][0])
                    break
            if gir_proxy == None:
                # look for an IPv6 address if no IPv4 record found
                for info in ai:
                    if info[0] == socket.AF_INET6:
                        gir_proxy = getgeoiprecord(info[4][0])
                        break

            if (gir_proxy is None) and (proxy in proxygirs):
                # reuse old value, there may have been a temporary DNS problem
                gir_proxy = proxygirs[proxy][0]
            proxygirs[proxy] = [gir_proxy, now]
        else:
            gir_proxy = proxygirs[proxy][0]

        if gir_proxy is None:
            # put it on the end of the list
            arc = float("inf")
        else:
            onegood = True
            arc = distance_on_unit_sphere(gir_rem['latitude'],
                                          gir_rem['longitude'],
                                          gir_proxy['latitude'],
                                          gir_proxy['longitude'])
            #print "distance between " + \
            #    rem_addr + ' (' + str(gir_rem['latitude']) + ',' + str(gir_rem['longitude']) + ')' \
            #    + " and " + \
            #    proxy + ' (' + str(gir_proxy['latitude']) + ',' + str(gir_proxy['longitude']) + ')' + \
            #    " is " + str(arc)

        i = bisect.bisect(arcs, arc)
        arcs[i:i] = [ arc ]
        orderedproxies[i:i] = [ proxy ]
        idx += 1

    if not onegood:
        # return an error if all the proxy names were bad
        return [[], 'no proxy addr found in database']

    return [orderedproxies, None]


import argparse
import json
import os
import pickle
import sys
import sagemaker_containers
import pandas as pd
import torch
import torch.optim as optim
import torch.utils.data
import numpy as np

from model import LSTMClassifier

def model_fn(model_dir):
    """Load the PyTorch model from the `model_dir` directory."""
    print("Loading model.")

    # First, load the parameters used to create the model.
    model_info = {}
    model_info_path = os.path.join(model_dir, 'model_info.pth')
    with open(model_info_path, 'rb') as f:
        model_info = torch.load(f)

    print("model_info: {}".format(model_info))

    # Determine the device and construct the model.
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = LSTMClassifier(model_info['embedding_dim'], model_info['hidden_dim'], model_info['vocab_size'])

    # Load the stored model parameters.
    model_path = os.path.join(model_dir, 'model.pth')
    with open(model_path, 'rb') as f:
        model.load_state_dict(torch.load(f))

    # Load the saved word_dict.
    word_dict_path = os.path.join(model_dir, 'word_dict.pkl')
    with open(word_dict_path, 'rb') as f:
        model.word_dict = pickle.load(f)

    model.to(device).eval()

    print("Done loading model.")
    return model

def _get_data_loader(batch_size, data_dir, data_filename):
    print("Get train data loader.")

    train_data = pd.read_csv(os.path.join(data_dir, data_filename), header=None, names=None)

    train_y = torch.from_numpy(train_data[[0]].values).float().squeeze()
    train_X = torch.from_numpy(train_data.drop([0], axis=1).values).long()

    train_ds = torch.utils.data.TensorDataset(train_X, train_y)

    return torch.utils.data.DataLoader(train_ds, batch_size=batch_size)

def train(model, train_loader, validation_loader, epochs, patience, optimizer, loss_fn, device):
    """
    This is the training method that is called by the PyTorch training script. The parameters
    passed are as follows:
    model        - The PyTorch model that we wish to train.
    train_loader - The PyTorch DataLoader that should be used during training.
    validation_loader - The PyTorch DataLoader that should be used during validation.
    epochs       - The total number of epochs to train for.
    optimizer    - The optimizer to use during training.
    loss_fn      - The loss function used for training.
    device       - Where the model and data should be loaded (gpu or cpu).
    """

    patience_counter = 0
    min_val_loss = np.Inf

    for epoch in range(1, epochs + 1):
        model.train()
        train_loss = 0
        train_correct = 0
        total_size = 0
        for batch in train_loader:
            batch_X, batch_y = batch

            batch_X = batch_X.to(device)
            batch_y = batch_y.to(device)

            # Taken from my Digit Recognition Kaggle Notebook
            # https://www.kaggle.com/nicapotato/pytorch-resnet-kanada
            model.zero_grad()  # sets gradients to 0 before loss calc. You will do this likely every step.
            output = model(batch_X)  # Pass batch to model

            loss = loss_fn(output, batch_y)  # calc and grab the loss value
            loss.backward()  # apply this loss backwards thru the network's parameters
            optimizer.step()  # attempt to optimize weights to account for loss/gradients

            train_correct += (output.round() == batch_y).sum().item()
            train_loss += loss.data.item()

        # Evaluation with the validation set
        model.eval() # eval mode
        val_loss = 0
        val_correct = 0

        with torch.no_grad():
            # First Validation Set
            for batch in validation_loader:
                batch_X, batch_y = batch

                batch_X = batch_X.to(device)
                batch_y = batch_y.to(device)

                val_output = model(batch_X) # get predictions
                vloss = loss_fn(val_output, batch_y) # calculate the loss

                val_correct += (val_output.round() == batch_y).sum().item()
                val_loss += vloss.item()

        final_val_loss = val_loss/len(validation_loader)
        print(
"Epoch: {} - Training BCELoss: {:.4f}, Accuracy: {:.1f}% - \
Validation BCELoss: {:.4f} - Accuracy: {:.1f}%"
            .format(epoch,
                    train_loss / len(train_loader),
                    ((train_correct / len(train_loader.dataset))*100),
                    final_val_loss,
                    (val_correct / len(validation_loader.dataset))*100))

        if min_val_loss > round(final_val_loss,4) :
            min_val_loss = round(final_val_loss,4)
            patience_counter = 0
#             torch.save(model, os.path.join(model_path, 'best_model.pth'))
#             best_model = model.state_dict()
        else:
            patience_counter += 1

        if patience_counter > patience:
            print(f"No Improvemend in {patience} rounds, Early Stopping..")
            break
    print("Load Best Model..")
#     model = model.load_state_dict(best_model)
#     model = torch.load(os.path.join(model_path, 'best_model.pth'))
    return model

if __name__ == '__main__':
    # All of the model parameters and training parameters are sent as arguments when the script
    # is executed. Here we set up an argument parser to easily access the parameters.

    parser = argparse.ArgumentParser()

    # Training Parameters
    parser.add_argument('--batch-size', type=int, default=512, metavar='N',
                        help='input batch size for training (default: 512)')
    parser.add_argument('--epochs', type=int, default=10, metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')

    # Model Parameters
    parser.add_argument('--embedding_dim', type=int, default=32, metavar='N',
                        help='size of the word embeddings (default: 32)')
    parser.add_argument('--hidden_dim', type=int, default=100, metavar='N',
                        help='size of the hidden dimension (default: 100)')
    parser.add_argument('--vocab_size', type=int, default=5000, metavar='N',
                        help='size of the vocabulary (default: 5000)')

    # SageMaker Parameters
    parser.add_argument('--hosts', type=list, default=json.loads(os.environ['SM_HOSTS']))
    parser.add_argument('--current-host', type=str, default=os.environ['SM_CURRENT_HOST'])
    parser.add_argument('--model-dir', type=str, default=os.environ['SM_MODEL_DIR'])
    parser.add_argument('--data-dir', type=str, default=os.environ['SM_CHANNEL_TRAINING'])
    parser.add_argument('--num-gpus', type=int, default=os.environ['SM_NUM_GPUS'])

    args = parser.parse_args()

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print("Using device {}.".format(device))

    torch.manual_seed(args.seed)

    # Load the training data.
    train_loader = _get_data_loader(args.batch_size, args.data_dir, "train.csv")
    validation_loader = _get_data_loader(args.batch_size, args.data_dir, "validation.csv")

    # Build the model.
    model = LSTMClassifier(args.embedding_dim, args.hidden_dim, args.vocab_size).to(device)

    with open(os.path.join(args.data_dir, "word_dict.pkl"), "rb") as f:
        model.word_dict = pickle.load(f)

    print("Model loaded with embedding_dim {}, hidden_dim {}, vocab_size {}.".format(
        args.embedding_dim, args.hidden_dim, args.vocab_size
    ))

    # Train the model.
    optimizer = optim.Adam(model.parameters())
    loss_fn = torch.nn.BCELoss()

    model = train(model=model,
                  train_loader=train_loader,
                  validation_loader=validation_loader,
                  epochs=args.epochs,
                  patience=5,
                  optimizer=optimizer,
                  loss_fn=loss_fn,
                  device=device)

    # Save the parameters used to construct the model
    model_info_path = os.path.join(args.model_dir, 'model_info.pth')
    with open(model_info_path, 'wb') as f:
        model_info = {
            'embedding_dim': args.embedding_dim,
            'hidden_dim': args.hidden_dim,
            'vocab_size': args.vocab_size,
        }
        torch.save(model_info, f)

	# Save the word_dict
    word_dict_path = os.path.join(args.model_dir, 'word_dict.pkl')
    with open(word_dict_path, 'wb') as f:
        pickle.dump(model.word_dict, f)

	# Save the model parameters
    model_path = os.path.join(args.model_dir, 'model.pth')
    with open(model_path, 'wb') as f:
        torch.save(model.cpu().state_dict(), f)


#!/usr/bin/python

import os
import shutil
from glob import glob
from subprocess import call, check_output

OUTPUT_DIR_NAME = 'jars'

def call_unsafe(*args, **kwargs):
    kwargs['shell'] = True
    call(*args, **kwargs)

call_unsafe('./gradlew clean javadocRelease jarRelease')

try:
    os.mkdir(OUTPUT_DIR_NAME)
except OSError:
    pass

os.chdir(OUTPUT_DIR_NAME)
call_unsafe('rm *.jar')
call_unsafe('cp ../sdk/build/libs/*.jar .')

commit = check_output(['git', 'rev-parse', 'HEAD'])[:7]

for src in glob('*.jar'):
    name, ext = os.path.splitext(src)
    dest = name + '-' + commit + ext
    shutil.move(src, dest)

call_unsafe('open .')


import postgap.Globals
from postgap.DataModel import *
GRCH37_ENSEMBL_REST_SERVER = "http://grch37.rest.ensembl.org"
known_genes = {}
def get_snp_locations(rsIDs):
    hash = dict()
    with open(postgap.Globals.GWAS_SUMMARY_STATS_FILE) as f:
        for line in f:
            Chromosome, Position, rsID, Effect_allele, Non_Effect_allele, Beta, SE, Pvalue, z_score, MAF = line.strip().split('\t')
            hash[rsID] = dict()
            hash[rsID]['seq_region_name'] = Chromosome
            hash[rsID]['start'] = Position
            hash[rsID]['end'] = Position

    results = []
    for rsID in rsIDs:
        mapping = hash[rsID]
        snp = SNP(
            rsID=rsID,
            chrom=mapping['seq_region_name'],
            pos=(int(mapping['start']) + int(mapping['end'])) / 2,
            approximated_zscore=None
        )
        results.append(snp)
    return results

def get_ensembl_gene(ensembl_id, ENSEMBL_REST_SERVER=GRCH37_ENSEMBL_REST_SERVER):
    """

            Get gene details from name
            * string
            Returntype: Gene

    """
    key = (ensembl_id, ENSEMBL_REST_SERVER)
    if key not in known_genes:
        known_genes[key] = Gene(
            name=ensembl_id,
            id=ensembl_id,
            chrom=None,
            tss=None,
            biotype=None
        )
    return known_genes[key]


from layers.exceptions import EarlyResponseException, BeforeRecordError
from .base import BaseMessageHandler, BaseRequestHandler
from .messenger import MessengerRequestHandler
from .telegram import TelegramRequestHandler

def handle(request):
    _class = None

    if request["resource"].startswith("/telegram"):
        _class = TelegramRequestHandler

    if request["resource"].startswith("/facebook-messenger"):
        _class = MessengerRequestHandler

    try:
        _class().handle(request)
    except EarlyResponseException as e:
        return {"statusCode": e.status, "body": e.body}
    except BeforeRecordError as e:
        return {"statusCode": e.status}

    return {"statusCode": 200}


import torch

from slr_model import Encoder, Decoder, Seq2Seq

def main():
    device = torch.device("cuda")
    encoder = Encoder(lstm_hidden_size=512)
    decoder = Decoder(output_dim=500, emb_dim=256, enc_hid_dim=512, dec_hid_dim=512, dropout=0.5)
    model = Seq2Seq(encoder=encoder, decoder=decoder, device=device).cuda()
    model.eval()
    imgs = torch.randn(16, 3, 8, 128, 128).cuda()
    target = torch.LongTensor(16, 8).random_(0, 500).cuda()
    dummy_input = imgs, target
    torch.onnx.export(model, dummy_input, 'slr.onnx', verbose=True)

if __name__ == '__main__':
    main()


# Authors: Robert Luke <mail@robertluke.net>
#
# License: BSD (3-clause)

import pandas as pd
import statsmodels.formula.api as smf

from ...simulation import simulate_nirs_raw
from ...experimental_design import make_first_level_design_matrix
from ...statistics import run_GLM, statsmodels_to_results
from ...utils._io import glm_to_tidy

def test_statsmodel_to_df():

    amplitude = 1.432

    df_cha = pd.DataFrame()
    for n in range(5):

        raw = simulate_nirs_raw(sfreq=3., amplitude=amplitude,
                                sig_dur=300., stim_dur=5.,
                                isi_min=15., isi_max=45.)
        design_matrix = make_first_level_design_matrix(raw, stim_dur=5.0)
        glm_est = run_GLM(raw, design_matrix)
        cha = glm_to_tidy(raw, glm_est, design_matrix)
        cha["ID"] = '%02d' % n
        df_cha = df_cha.append(cha)
    df_cha["theta"] = df_cha["theta"] * 1.0e6
    roi_model = smf.mixedlm("theta ~ -1 + Condition", df_cha,
                            groups=df_cha["ID"]).fit(method='nm')
    df = statsmodels_to_results(roi_model)
    assert type(df) == pd.DataFrame
    assert df["Coef."]["Condition[A]"] == amplitude
    assert df["Significant"]["Condition[A]"]
    assert df.shape == (8, 8)

    roi_model = smf.rlm("theta ~ -1 + Condition", df_cha,
                        groups=df_cha["ID"]).fit()
    df = statsmodels_to_results(roi_model)
    assert type(df) == pd.DataFrame
    assert df["Coef."]["Condition[A]"] == amplitude
    assert df["Significant"]["Condition[A]"]
    assert df.shape == (8, 8)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('settings', '0005_auto_20150316_1208'),
    ]

    operations = [
        migrations.AlterField(
            model_name='field',
            name='value_type',
            field=models.CharField(max_length=50, verbose_name='\u0422\u0438\u043f \u0434\u0430\u043d\u0438\u0445', choices=[(b'string', b'string'), (b'int', b'int'), (b'bool', b'bool')]),
            preserve_default=True,
        ),
    ]


import warnings
warnings.filterwarnings('ignore')
from PIL import Image
import json
import time
import numpy as np
import matplotlib.pyplot as plt
import sys
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
tfds.disable_progress_bar()

import os
import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
logging.getLogger('tensorflow').setLevel(logging.FATAL)

def process_image(img):
    img = tf.convert_to_tensor(img)
    img = tf.image.resize(img, (224, 224), method = 'nearest')
    img = tf.cast(img, tf.float32)
    img /= 255
    img = img.numpy()
    return img

def predict(image_path, model, top_k):
    img = Image.open(image_path)
    img = np.asarray(img)
    img_proc = process_image(img)
    img_proc = np.expand_dims(img_proc, axis=0)
    p = model.predict(img_proc)
    classes = np.argpartition(p[0], -top_k)[-top_k:]
    r=classes[np.argsort(p[0][classes])][::-1]
    return (r+1).astype(str), p[0][r]

def main():
    print('Using:')
    print('\t\u2022 TensorFlow version:', tf.__version__)
    print('\t\u2022 tf.keras version:', tf.keras.__version__)
    print('\t\u2022 Running on GPU' if tf.test.is_gpu_available() else '\t\u2022 GPU device not found. Running on CPU')

    args = sys.argv[1:]
    class_names = {}
    k = 1
    path_json = ''

    if len(args) < 2 or len(args) > 6:
        print('Invalid number of arg :(')
        exit()

    img_path = args[0]
    print('Loading model......')
    model = tf.keras.models.load_model(args[1], custom_objects={'KerasLayer':hub.KerasLayer})
    print('Model loaded.')

    if len(args) > 2:
        n = len(args[2:])
        a = np.array(args[2:]).reshape(n//2,2)
        for r in a:
            if r[0] not in ('--category_names', '--top_k'):
                print('Unrecognize arg')
                exit()
            if r[0] == '--top_k':
                k = int(r[1])
            if r[0] == '--category_names':
                path_json = r[1]
    if len(path_json) > 4:
        with open(path_json, 'r') as f:
            class_names = json.load(f)

    c, p = predict(img_path, model,k)
    if len(class_names.keys()) > 0:
        c = [class_names[x] for x in c]

    print('K-Perdictions sorted by likelyhood:')
    print(c)
    print('Associed probabilities are:')
    print(p)

if __name__ == '__main__':
    main()


#!/Users/Admin/anaconda3/envs/py36-anaconda/bin/python
import sys
import os

sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))

from asyspider.spider import Spider, Proxy, DBProxy, headers_raw_to_dict
import re
from pyquery import PyQuery as pq
import logging
from tools import log
from pprint import pprint as pp
from pprint import pformat
import argparse
import time
from urllib.parse import urljoin
from tinydb import TinyDB, Query
import os

db = TinyDB(os.path.join(os.path.dirname(__file__), './db.json'))
articles = db.table('articles')
query = Query()

logger = logging.getLogger('demo')
HOST = 'http://www.huntcoco.com/index.php'

class Crawler(Spider):
    status_code = (200, 301, 302)
    platform = 'desktop'
    max_tasks = 15
    sleep_time = None
    timeout = 30
    retries = 10
    check_crawled_urls = True
    update_cookies = True
    min_content_length = 1
    proxies_set = set()
    ProxyClass = Proxy

    async def on_start(self):
        await self.login()
        url = 'http://www.huntcoco.com/index.php'
        r = await self.async_crawl(url)
        doc = r.content.decode()
        dom = pq(doc)
        self.parse_all_pages(dom)

    def parse_all_pages(self, dom):
        urls = [urljoin(HOST, el.attr.href) for el in dom('a').items() if
                el.attr.href and HOST in urljoin(HOST, el.attr.href)]
        for url in urls:
            self.add_task(self.insert_to_db, url)

    async def insert_to_db(self, url):
        await self.login()
        r = await self.async_crawl(url)
        if not r:
            return
        doc = r.content.decode()
        dom = pq(doc)
        self.parse_all_pages(dom)

        title = dom('td > span').text()
        content = dom('body').text()
        ret = dict(
            url=url,
            title=title,
            content=content
        )
        # articles.insert(ret)

    async def login(self):
        url = 'http://www.huntcoco.com/member/account_login1.php'
        r = await self.async_crawl(url, method='POST', headers="""
        Accept: */*
        Accept-Encoding: gzip, deflate
        Accept-Language: zh-TW,zh;q=0.9,en;q=0.8,zh-CN;q=0.7,en-US;q=0.6
        Cache-Control: no-cache
        Connection: keep-alive
        Content-Length: 46
        Content-Type: application/x-www-form-urlencoded; charset=UTF-8
        Host: www.huntcoco.com
        Origin: http://www.huntcoco.com
        Pragma: no-cache
        Referer: http://www.huntcoco.com/index.php?goto=41
        User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
        X-Requested-With: XMLHttpRequest
        """, data=headers_raw_to_dict("""
        a: nomoneynolove20130610@gmail.com
        b: ji394su3
        """))

if __name__ == '__main__':
    log.initlog('DEMO', level=logging.DEBUG, debug=True)
    c = Crawler()
    c.run()


from pxr import Usd, Sdf, UsdGeom, Gf
from enum import IntEnum
import omni.services.client as OmniServicesClient
import omni.kit
import omni.client as OmniClient
import omni.ui as ui
import tempfile
import carb
import asyncio
import json
import webbrowser

class LevelOfDetail(IntEnum):
    LOW = 0,
    MEDIUM = 1,
    HIGH = 2

class ItemEngineeringConnector:
    _item_host = "https://item.engineering"
    _item_itemtool_url = "DEde/tools/engineeringtool"
    _item_geometry_info_endpoint = "dqart/0:DEde/project_utilities/get_geometry_info"
    _item_object_pool_data_endpoint = "objectPoolData"

    _projects_path = "c:/temp/item_engineering_tool"
    _parts_path = "c:/temp/item_engineering_tool/parts"
    #_projects_path = "omniverse://ovnuc/Projects/BMW_MIRROR/ConnectedSystems/item_engineering_tool"
    #_parts_path = "omniverse://ovnuc/Projects/BMW_MIRROR/ConnectedSystems/item_engineering_tool/parts"

    def __init__(self):
        self._omni_client = OmniServicesClient.AsyncClient(self._item_host)

    def _open_or_create_stage(self, path, clear_exist=True):
        layer = Sdf.Layer.FindOrOpen(path)
        if not layer:
            layer = Sdf.Layer.CreateNew(path)
        elif clear_exist:
            layer.Clear()

        if layer:
            return Usd.Stage.Open(layer)
        else:
            return None

    def refresh_parts(self):
        self._ov_parts = []
        result, entries = OmniClient.list(self._parts_path)
        for entry in entries:
            self._ov_parts.append(entry.relative_path)
        print (f"parts refreshed, found: {len(self._ov_parts)}")

    def _show_waiting_popup_convert(self):
        if not self._waiting_popup_convert:
            self._waiting_popup_convert = ProgressPopup("Converting...", status_text="Preparing...")

        self._waiting_popup_convert.status_text = "Preparing..."
        self._waiting_popup_convert.progress = 0.0
        self._waiting_popup_convert.show()

    async def download_blob(self, temp_dir, geo_model, usd_filename):
        blob_url = f'/objectPoolData/{geo_model}'
        print(f"    download {blob_url}")
        blob = await self._omni_client.get(blob_url)

        temp_blob_path =f"{temp_dir.name}/blob.obj"
        blobfile = open(temp_blob_path, "wb")
        blobfile.write(blob)
        blobfile.close()

        print(f"written to temp: {temp_blob_path}")

        converter_manager = omni.kit.asset_converter.get_instance()
        context = omni.kit.asset_converter.AssetConverterContext()
        context.ignore_materials = True
        output_path = f"{self._parts_path}/{usd_filename}"

        print("convert...")
        print("target:" + output_path)
        def convert_progress_callback(progress, total):
            print(f"convert progress: {float(progress) / total}")
        def material_loader(descr):
            print(f"material loader {descr}" )
        task = converter_manager.create_converter_task(temp_blob_path, output_path, convert_progress_callback, context, material_loader)
        success =  await task.wait_until_finished()
        print(f"success: {success}")
        if success:
            self._ov_parts.append(usd_filename)
        else:
            detailed_status_code = task.get_status()
            detailed_status_error_string = task.get_detailed_error()
            print(f"error converting: {detailed_status_code} {detailed_status_error_string}...")
        return output_path

    async def _create_lod_stage(self, project_id, lod):
        print("create lod stage ...")
        lod_stage_path = f"{self._projects_path}/{project_id}/lod{lod}.usd"
        rel_parts_path = "../parts"
        lod_stage = self._open_or_create_stage(lod_stage_path)
        lod_world = lod_stage.DefinePrim("/World", "Xform")
        UsdGeom.SetStageUpAxis(lod_stage, UsdGeom.Tokens.y)
        lod_stage.SetDefaultPrim(lod_world)
        #-----------------------------------------
        with_product_info = 1
        with_conveyor_info = 1
        with_material_info = 1
        url = f'{self._item_geometry_info_endpoint}/{project_id}/{lod}/{with_product_info}/{with_conveyor_info}/{with_material_info}'
        print("REQUEST->" + url)
        doc = await self._omni_client.get(url)

        #webbrowser.open(f"{self.item_host}/{url}")

        #print(json.dumps(doc))
        blobfile = open("c:\\temp\\out.jsn", "w")
        blobfile.write(json.dumps(doc))
        blobfile.close()
        p_p_obj = doc['p']
        p_obj = p_p_obj['objects']
        pidx = 0
        temp_dir = tempfile.TemporaryDirectory()
        for part_id in p_obj.keys():

            part_obj = p_obj[part_id]
            part_group_id = part_obj['g_id']
            part_product_name = part_obj['name']
            part_article_number = part_obj.get('art', "")
            part_roller_conveyor = part_obj.get('roller_conveyor')
            part_length = part_obj.get('length')

            idx = 0
            pidx = pidx + 1
            part_g_obj = part_obj['g']
            print(f'PART {part_id} {pidx}/{ len(p_obj)} (geos:{len(part_g_obj)}): {part_product_name}')

            for geo_obj in part_g_obj:
                idx = idx + 1

                geo_model = geo_obj['m']
                geo_scale = geo_obj['s']
                geo_position = geo_obj['p']
                geo_rotation = geo_obj['r']
                print(f'  geo {geo_model}')
                self.prim = None
                prim_path = f"/World/g_{part_group_id}/a_{part_article_number}_{pidx}/m_{idx}"
                if geo_model.endswith(".obj"):
                    print("   obj:")
                    usd_filename = f"g_{geo_model.replace('/','_')}_.usd"
                    if usd_filename in self._ov_parts:
                        print("     using library part")
                        output_path = f"{self._parts_path}/{usd_filename}"
                    else:
                        print("     downloading")
                        output_path = await self.download_blob(temp_dir, geo_model, usd_filename)
                    print("     d1")
                    model = lod_stage.DefinePrim(prim_path, "")
                    model.SetInstanceable(False)
                    print("     d2")
                    model_part = lod_stage.DefinePrim(prim_path+"/part", "")
                    print(f"     d3 {usd_filename}")
                    model_part.GetReferences().AddReference(f"{rel_parts_path}/{usd_filename}")
                    print("     d4")

                    UsdGeom.Xformable(model_part).ClearXformOpOrder ()
                    UsdGeom.Xformable(model_part).AddTranslateOp().Set(Gf.Vec3f(geo_position["x"], geo_position["y"], geo_position["z"]))
                    UsdGeom.Xformable(model_part).AddRotateXYZOp().Set(Gf.Vec3f(geo_rotation["x"], geo_rotation["y"], geo_rotation["z"]))
                    UsdGeom.Xformable(model_part).AddScaleOp().Set(Gf.Vec3f(geo_scale["x"], geo_scale["y"], geo_scale["z"]))
                    print("    obj done.")
                elif geo_model == "cube":
                    print(prim_path)
                    cube = lod_stage.DefinePrim(prim_path, "Cube")
                    cube.GetAttribute("size").Set(2.0)

                    UsdGeom.Xformable(cube).ClearXformOpOrder ()
                    UsdGeom.Xformable(cube).AddTranslateOp().Set(Gf.Vec3f(geo_position["x"], geo_position["y"], geo_position["z"]))
                    UsdGeom.Xformable(cube).AddRotateXYZOp().Set(Gf.Vec3f(geo_rotation["x"], geo_rotation["y"], geo_rotation["z"]))
                    UsdGeom.Xformable(cube).AddScaleOp().Set(Gf.Vec3f(geo_scale["x"], geo_scale["y"], geo_scale["z"]))

                elif geo_model == "cylinder":
                    cylinder = lod_stage.DefinePrim(prim_path, "Cylinder")
                    cylinder.GetAttribute("radius").Set(1.0)
                    cylinder.GetAttribute("axis").Set("Y")
                    cylinder.GetAttribute("height").Set(1.0)

                    UsdGeom.Xformable(cylinder).ClearXformOpOrder ()
                    UsdGeom.Xformable(cylinder).AddTranslateOp().Set(Gf.Vec3f(geo_position["x"], geo_position["y"], geo_position["z"]))
                    UsdGeom.Xformable(cylinder).AddRotateXYZOp().Set(Gf.Vec3f(geo_rotation["x"], geo_rotation["y"], geo_rotation["z"]))
                    UsdGeom.Xformable(cylinder).AddScaleOp().Set(Gf.Vec3f(geo_scale["x"], geo_scale["y"], geo_scale["z"]))
                else :
                    print(f"unknown geo type {geo_model}")

            print("done.")

        lod_stage.Save()
        #print(lod_stage.GetRootLayer().ExportToString())
        return f"{project_id}/lod{lod}.usd"

    async def _create_main_stage(self, project_id):
        stage_path = f"{self._projects_path}/{project_id}.usd"
        stage = self._open_or_create_stage(stage_path)
        UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.y)
        if stage is None:
            print("error creating stage")
            return None
        world_prim = stage.DefinePrim("/World", "Xform")
        stage.SetDefaultPrim(world_prim)
        item_prim = stage.DefinePrim(f"/World/item_{project_id}", "")
        vset = item_prim.GetVariantSets().AddVariantSet('LOD')
        # Create variant options.
        vset.AddVariant('Low')
        vset.AddVariant('Medium')
        vset.AddVariant('High')

        vset.SetVariantSelection('Low')
        with vset.GetVariantEditContext():
            low_lod_path = await self._create_lod_stage(project_id, LevelOfDetail.LOW)
            item_prim.GetReferences().AddReference(low_lod_path)

        vset.SetVariantSelection('Medium')
        with vset.GetVariantEditContext():
            medium_lod_path = await self._create_lod_stage(project_id, LevelOfDetail.MEDIUM)
            item_prim.GetReferences().AddReference(medium_lod_path)
        vset.SetVariantSelection('High')
        with vset.GetVariantEditContext():
            high_lod_path = await self._create_lod_stage(project_id, LevelOfDetail.HIGH)
            item_prim.GetReferences().AddReference(high_lod_path)

        stage.Save()
        #print(stage.GetRootLayer().ExportToString())
        print(f"written.{stage_path}")
        return stage_path

    def import_project(self, project_id):
        print("\n\n IMPORT =================")

        self.refresh_parts()

        #loop = asyncio.get_event_loop()
        #try:
        #    result = loop.run_until_complete(asyncio.ensure_future(self._create_main_stage(project_id)))
        #    print(f"GOTI IT {result} ")
        #finally:
        #    loop.close()
        loop = asyncio.get_event_loop()
        task = loop.create_task(self._create_main_stage(project_id)) # just some task
        r = loop.run_until_complete(task) # wait for it (outside of a coroutine)
        #r = asyncio.ensure_future(self._create_main_stage(project_id))

        print("ENSIRED")
        print(r)
        print("=================\n\n")
        return r


#! /usr/bin/env python2.7

"""shck - network generator script
==============================================================================
 BA15_wlan_1
=============================================================================

*****************************************************************************
  Software:   shck Python Script
  Authors:    guadamau@students.zhaw.ch | leibupro@students.zhaw.ch
  Date:       03.2015

  Purpose:    Generate and send specific network load

  Desc:       Creates specific load described in the german thesis
              "Ermittlung der Performance von Netzwerkfunktionen am Beispiel
              von PRP" by Mauro Guadagnini and Prosper Leibundgut.

              Uses scapy (http://www.secdev.org/projects/scapy/index.html)

*****************************************************************************
"""

#
#  constants
#
ETH_OVERHEAD = 14
IP_OVERHEAD = 20
TCP_OVERHEAD = 32
UDP_OVERHEAD = 8
PRP_OVERHEAD = 6

#
# global variables
#
sizetype = 'MIN'                    # set default sizetype to MIN
payload_size = 46               # set default framesize to 46 bytes (MIN sizetype)

target = '127.0.0.1'                # set default IP-taget to localhost
interface = 'eth0'                  # set default network interface to eth0
mtu = int(1500)                     # set default mtu to 1500 bytes
                                    # (Maximum frame size decrement by 6 bytes)
transmission_type = 'UDP'           # set default transmission type to UDP
prp_enabled = False                 # disable PRP-support

count_frames = int(1)               # send 1 frame by default
unlimited_count = False             # don't send unlimited count of frames per default

srvmode = False                     # start per default in client mode

portnumber_tcp = int(52015)             # set default portnumber for TCP to 52015
portnumber_udp = int(52014)             # set default portnumber for UDP to 52015

datafile = ''                       # global variable for datafile (payload of packets)

#
# Import scapy and other modules
# Check if run by root and exit if not
#
try:
    import os, socket, sys, getopt, uuid

    from socket import error as socket_error
    import errno

    from scapy.all import *

except ImportError:
    print("Use python 2.7 or install scapy http://www.secdev.org/projects/scapy")
    sys.exit(0)

if not os.geteuid()==0:
    sys.exit("ERROR:Only root can run this script\nUse sudo or run as root")

#
# functions
#
def printheader():
    sys.stdout.write('####################################### shck - generate and send network load\n#######################################\n')

printheader()

#
# function definitions
#
def addpayload( element, payloadcontent ):
    """Adds data to payload of a specific element
    """
    if payloadcontent != '':
        element.payload=payloadcontent

def getpayloadfromfile( datafile ):
    """returns the first 1500 bytes of a specific file
    """
    data = ''
    with open(datafile, "rb") as f:
        byte = f.read(1)
        while (byte != "" and sys.getsizeof(data) <= 3000):
            # Do stuff with byte.
            data += byte
            if not byte:
                break
            byte = f.read(1)

    return data

def generateeth( data ):
    """Generates an ethernet frame (Type value is 0x2015)

    Parses the MAC-address of the NIC and writes it as source
    into the ethernet frame.

    Writes target MAC-address (needs to have the format "XX:XX:XX:XX:XX:XX")
    as destination into the ethernet frame.

    Adds prepared payload (by cutpayload(data)) in the needed size to the frame.
    """
    src=str(hex(uuid.getnode()))
    srcmac = ''
    if str(src[1])=='x':
        src = src[2:]
        srcmac += '00:'
    srcmac += str(src[0])
    srcmac += str(src[1])
    srcmac += ':' + str(src[2])
    srcmac += str(src[3])
    srcmac += ':' + str(src[4])
    srcmac += str(src[5])
    srcmac += ':' + str(src[6])
    srcmac += str(src[7])
    srcmac += ':' + str(src[8])
    srcmac += str(src[9])
    eth=Ether(dst=target,src=srcmac,type=0x2015)
    data=cutpayload(data)
    addpayload(eth, data)
    return eth

def cutpayload( data ):
    """Returns the first x bytes from the content of the argument variable

    The size of the return value matches the payload of an ethernet frame or
    TCP- or UDP-packet so that the final packet/frame will have the needed size.
    """
    global payload_size

    if transmission_type == 'ETH':
        size = int(payload_size)
    elif transmission_type == 'TCP':
        size = int(payload_size)-int(IP_OVERHEAD)-int(TCP_OVERHEAD)-int(ETH_OVERHEAD)
    elif transmission_type == 'UDP':
        size = int(payload_size)-int(IP_OVERHEAD)-int(UDP_OVERHEAD)

    if size <= 0:
        return ''
    else:
        return data[0:int(size)]

def calcsizeofrandompayload(current_randomsize):
    if transmission_type == 'ETH':
        size = int(current_randomsize)
    elif transmission_type == 'TCP':
        size = int(current_randomsize)-int(IP_OVERHEAD)-int(TCP_OVERHEAD)-int(ETH_OVERHEAD)
    elif transmission_type == 'UDP':
        size = int(current_randomsize)-int(IP_OVERHEAD)-int(UDP_OVERHEAD)-int(ETH_OVERHEAD)

    if size <= 0:
        return 0
    else:
        return int(size)

def file_len(fname):
    with open("custom_framesizes.txt", "r") as f:
        for i, l in enumerate(f):
            pass
    return i + 1

def generate_package( data ):
    """returns the final content that needs to be send by the sockets.

    The TCP- and UDP-socket wrap all needed headers around the return value,
    whereas the RAW-socket needs to have the complete ethernet frame.

    Also checks if the destination argument matches
    the specified transmission type (MAC for Ethernet / IP for TCP and UDP)
    """
    if transmission_type == 'ETH':
        if not ':' in str(target):
            sys.stdout.write('need a MAC-address as destination when sending Ethernet frames')
            sys.exit()
        else:
            packet = generateeth( data )
            return str(packet)
    elif (transmission_type == 'TCP' or transmission_type == 'UDP'):
        if (sizetype == 'MIN' and transmission_type == 'TCP'):
        # Empty TCP-packet is 66 bytes
        # Cant send an empty packet -> Payload 1 Byte
            return "\0"
        if not '.' in str(target):
            sys.stdout.write('need an IP-address as destination when sending ' + str(transmission_type) + '-packets')
            sys.exit()
        return cutpayload(data)
    else:
        sys.stdout.write('ERROR: Wrong transmission_type in generate_package()\n')
        sys.exit()

def sendpacketout( packet, data ):
    """Creates the needed socket and sends the prepared data x times (specified per -n parameter) over it

    For the CUSTOM-sizetype, the function reads a prepared textfile which contains a specific
    size in bytes per line, prepares and sends the frame/packet per line.
    """
    try:
        countend = int(count_frames)

        if transmission_type == 'ETH':
            s = socket.socket(socket.PF_PACKET, socket.SOCK_RAW)
            s.bind((interface,0x2015))

        elif transmission_type == 'UDP':
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect((target, portnumber_udp))

        elif transmission_type == 'TCP':
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((target, portnumber_tcp))

        ss = StreamSocket(s)
        sendpacketonsocket = ss.send

        if sizetype == 'CUSTOM':
            global payload_size
            count = 0
            packet = generate_package(data)
            packet = str(packet)
            if unlimited_count == False:
                frametogen = count_frames
            else:
                frametogen = 1000000

            filelength = file_len("custom_framesizes.txt")
            with open("custom_framesizes.txt", "r") as randomsizes:
                head = [next(randomsizes) for x in xrange(0,filelength)]
            cursor = 0
            while (count < count_frames):

                if (count==count_frames and unlimited_count == False):
                    break

                if (int(cursor)%frametogen==0 or int(cursor)%(len(head))==0):
                    cursor = 0

                if int(head[cursor]) > (mtu+ETH_OVERHEAD):
                    head[cursor] = (mtu+ETH_OVERHEAD)
                packettosend = Raw(packet[0:calcsizeofrandompayload(head[cursor])])
                packettosendlength = len(packettosend)
                bytecount = sendpacketonsocket(packettosend)
                if (unlimited_count == False) and (bytecount == packettosendlength):
                    count += 1
                    cursor += 1
                elif (unlimited_count == True) and (bytecount == packettosendlength):
                    cursor += 1
        else:
            count = 0
            packet = Raw(packet)
            while count < countend:
                bytecount = sendpacketonsocket(packet)
                if int(bytecount) == len(packet):
                    count += 1

            while unlimited_count == True:
                ss.send(packet)

        s.close()
    except socket_error as serr:
        if serr.errno != errno.ECONNREFUSED:
            raise serr
        else:
            print('ERROR (Connection refused by target):\nshck needs to run in Server-Mode (-S) with -t ' + transmission_type + '\nand on the same port (-p PORTNUMBER) on the target host!\nTry again after starting shck in Server-Mode on the target.')
            sys.exit()

def sendpacket( data ):
    """starts the sending-method (sendpacketout())
    """
    import time

    global payload_size

    if sizetype == 'MIN':
        payload_size = int(46)
        packet = generate_package(data)
        sendpacketout(packet, data)
    elif sizetype == 'MAX':
        payload_size = int(mtu)
        if prp_enabled == True and mtu == 1500:
            payload_size -= PRP_OVERHEAD
        packet = generate_package(data)
        sendpacketout(packet, data)
    elif sizetype == 'CUSTOM':
        payload_size = int(mtu)
        if prp_enabled == True:
            payload_size -= PRP_OVERHEAD
        packet = generate_package(data)
        sendpacketout(packet, data)

def server():
    """server-mode: listens on the specified portnumber (TCP or UDP)
    """
    HOST = ''
    PORT_UDP = portnumber_udp
    PORT_TCP = portnumber_tcp
    if transmission_type == 'TCP':
        while 1:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                s.bind((HOST, PORT_TCP))
                s.listen(1)
                conn, addr = s.accept()
                print('Connected by ' + str(addr))
                count = 0
                while 1:
                    data = conn.recv(4096)
                    if not data: break
                    count += 1
                    print ('\rRecieved messages: ' + str(count) + ''),
                    #conn.close()
                    s.close()
            except socket_error as serr:
                if serr.errno !=  errno.ECONNRESET:
                    raise serr
                else:
                    print('Connection reset by peer, ready for new connection...')
    elif transmission_type == 'UDP':
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind((HOST, PORT_UDP))
        count = 0
        while 1:
            count += int(1)
            data, addr = s.recvfrom(4096)
            print ('\rRecieved messages: ' + str(count) + ''),
    else:
        print('INVALID TRANSMISSION_TYPE! Choose TCP or UDP!\n')

def printhelp():
    from subprocess import call
    call(["less", "README"])

#
# main
#
def main(argv):
    """Parse arguments
    """

    try:
        opts, args = getopt.getopt(argv,"s:t:d:f:m:i:n:p:PSh")
    except getopt.GetoptError as err:
        print(err)
        printhelp()
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            printhelp()
            sys.exit()
        elif opt in ("-s"):
            global sizetype
            if (arg == 'MIN' or arg == 'CUSTOM' or arg == 'MAX'):
                sizetype = arg
            else:
                print('No valid sizetype selected (MIN, CUSTOM, MAX)')
                sys.exit()
        elif opt in ("-t"):
            global transmission_type
            if (arg == 'ETH' or arg == 'TCP' or arg == 'UDP'):
                transmission_type = arg
            else:
                print('No valid transmission type selected (ETH, TCP, UDP)')
                sys.exit()
        elif opt in ("-f"):
            global datafile
            datafile = arg
        elif opt in ("-d"):
            global target
            target = arg
        elif opt in ("-m"):
            global mtu
            mtu = int(arg)
        elif opt in ("-i"):
            global interface
            interface = arg
        elif opt in ("-n"):
            global count_frames
            global unlimited_count
            if int(arg) == 0:
                unlimited_count = True
            else:
                count_frames = int(arg)
        elif opt in ("-P"):
            global prp_enabled
            prp_enabled = True
        elif opt in ("-p"):
            global portnumber_udp
            global portnumber_tcp
            portnumber_udp = int(arg)
            portnumber_tcp = int(arg)
        elif opt in ("-S"):
            global srvmode
            srvmode = True

    if (datafile == '' and srvmode == False):   # display help if invalid parameters are set
        printhelp()
        sys.exit()
    elif (srvmode == True):
        print('PID of shck: ' + str(os.getpid()) + 'Running in SERVER MODE' + '\n-TRANSMISSION_TYPE: ' + str(transmission_type) + '\n\nListening...')
        server()                                # start server-mode

    else:
        print('PID of shck: ' + str(os.getpid()) + 'Load characteristics:\nDestination: ' + str(target) + '\n-SIZETYPE: ' + str(sizetype) + '\n-TRANSMISSION_TYPE: ' + str(transmission_type) + '\n-PRP-mode enabled: ' + str(prp_enabled) + '\n-Interface: ' + str(interface) + '\n-FILE: ' + str(datafile))
        if (unlimited_count == True):
            print('-Frame / Packet count: unlimited (Stop by pressing Ctrl+C)')
        else:
            print('-Frame / Packet count: ' + str(count_frames))
        print('\nSending load...')

        payload=getpayloadfromfile( datafile )  # get payload from file
        sendpacket( payload )                   # send frames with specified payload

        print('Done\nshck is finished\n')
    sys.exit()

#
# start main and catch KeyboardInterrupt and sys.exit()
#
try:
    main(sys.argv[1:])
except (KeyboardInterrupt, SystemExit):
    sys.stdout.write("######################################\n Goodbye!\n this traffic generation was brought to you by shck \n######################################\n")
    sys.exit()
except:
    raise


#!/usr/bin/python

"""
Program that takes the output from bedtools genomecov and removes 2-micron regions
and also adds a unique name to each bedGraph file.

Then creates the bigWig file utilizing the binary provided by UCSC.

references:
-----------
Reference to covert decimal to roman
https://stackoverflow.com/questions/28777219/basic-program-to-convert-integer-to-roman-numerals

genomeCoverage : https://bedtools.readthedocs.io/en/latest/content/tools/genomecov.html
To create the bedgraph style output, you can do stand specific, fragment size and stuff

"""

import argparse
from collections import OrderedDict

import pandas as pd

def write_roman(num):

    roman = OrderedDict()
    roman[1000] = "M"
    roman[900] = "CM"
    roman[500] = "D"
    roman[400] = "CD"
    roman[100] = "C"
    roman[90] = "XC"
    roman[50] = "L"
    roman[40] = "XL"
    roman[10] = "X"
    roman[9] = "IX"
    roman[5] = "V"
    roman[4] = "IV"
    roman[1] = "I"

    def roman_num(num):
        for r in roman.keys():
            x, y = divmod(num, r)
            yield roman[r] * x
            num -= (r * x)
            if num > 0:
                roman_num(num)
            else:
                break

    return "".join([a for a in roman_num(num)])

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument(
        'genomeCoverageBed', help='bedtools genomeCoverageBed output file using (-bga) (strand) options')
    parser.add_argument(
        'strand', help="Strand information takes + or - , by default assumes + ")
    args = parser.parse_args()

    data = []  # used to store the file contents as a list of lists
    filecontents = []
    header = []  # contains 2-microns and other wrongly formated lines

    # reading the genomeCoverageBed output file
    openfile = open(args.genomeCoverageBed, 'r').readlines()

    # reading each line and converting the chromosome numbers to ROMAN equivalents
    for line in openfile:
        if line.startswith("chr"):
            temp = line.strip().split("\t")

            # checking the strand information
            if args.strand == '-':
                #  checking if it is chrM
                if temp[0][3:] != 'M':
                    # getting the chr number and converting to romans
                    romanValue = write_roman(int(temp[0][3:]))
                    temp[0] = "chr" + str(romanValue)
                    if int(temp[3]) > 0:
                        temp[3] = str(-int(temp[3]))
                    # print temp
                    filecontents.append(temp)
                else:
                    filecontents.append(temp)
            # In the case of reverse strand
            else:
                if temp[0][3:] != 'M':
                    # getting the chr number and converting to romans
                    romanValue = write_roman(int(temp[0][3:]))
                    temp[0] = "chr" + str(romanValue)
                    # print temp
                    filecontents.append(temp)
                else:
                    filecontents.append(temp)

        # removing 2-microns, which is not recognized by the UCSC browser
        else:
            if line.startswith("2-micron") is False:
                header.append(line)

    data = pd.DataFrame(filecontents, columns=(
        'chr', 'start', 'end', 'genome_coverage'))
    print(data[0:10])
    data.to_csv('processed.bedGraph', sep='\t', header=False, index=False)


"""
add the end clause
"""

__builtins__.end = None

def test(x):
    if x > 1:
        print('a')
    else:
        print('b')
    end
end

def main():
    test(1)
    print("I can use end in python")
end

main()
print(dir(__builtins__))


# coding=utf-8
# Q18:
# A hapax legomenon (often abbreviated to hapax) is a word which occurs only once in either the written record
# of a language, the works of an author, or in a single text. Define a function that given the file name
# of a text will return all its hapaxes. Make sure your program ignores capitalization.

import operator
from collections import Counter

def hapax(filename):
    f = open(filename)
    try:
        counter = Counter(f.read().split())
    except (Exception,) as e:
        print(e)
    else:
        for k, v in sorted(counter.iteritems(), key=operator.itemgetter(0)):
            if v == 1 and len(k) > 3:
                print(k)

    finally:
        f.close()

hapax(__file__)


import bisect

class Solution:
    def findBestValue(self, arr, target: int) -> int:
        arr.sort()
        n = len(arr)
        pre = arr[::]
        for i in range(1, n):
            pre[i] = arr[i] + pre[i - 1]
        pre.append(0)
        res = {}
        l, r = 0, arr[-1]
        while l < r:
            m = (l + r) // 2
            idx = bisect.bisect_right(arr, m)
            val = pre[idx - 1] + (n - idx) * m
            if val >= target:
                r = m
            else:
                l = m + 1
            x = abs(val - target)
            if x not in res:
                res[x] = [m]
            else:
                res[x].append(m)
        l, r = 0, arr[-1]
        while l < r:
            m = (l + r + 1) // 2
            idx = bisect.bisect_right(arr, m)
            val = pre[idx - 1] + (n - idx) * m
            if val >= target:
                r = m - 1
            else:
                l = m
            x = abs(val - target)
            if x not in res:
                res[x] = [m]
            else:
                res[x].append(m)
        return min(res[min(res.keys())])

s = Solution()
print(s.findBestValue([2, 3, 5], 10))
print(s.findBestValue([3, 4, 9], 10))
print(s.findBestValue([20204, 21296, 25176, 27249, 60864], 56803))


from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from Pages.page_base import BasePage
from ui_framework_core import UIFramework

class VideoAnalysisPage(BasePage):

    def __init__(self, fw):
        super().__init__(fw)

    def click_start_record(self):
        self.fw.driver.find_element_by_xpath("//i[@class='icon-video-camera']").click()

    # def click_share_on_alert(self):
    #     Switching to Alert
    #
    #     share_window = self.fw.driver.window_handles[2]
    #     self.fw.driver.switch_to_window(share_window)
    #
    #     actions = ActionChains(self.fw.driver)
    #     actions.key_down(Keys.RETURN)
    #     actions.key_up(Keys.RETURN)
    #
    #     self.fw.driver.switch_to.alert.accept()

    def click_loop(self):
        # nav = self.fw.driver.find_element_by_xpath("//nav[@class='control side']")
        # hover = ActionChains(self.fw.driver).move_to_element(nav)
        # hover.perform()
        self.fw.driver.find_element_by_xpath("//i[@class='icon-loop']").click()

    def click_play(self):
        self.fw.driver.find_element_by_xpath("//div[@class='main']/button[@class='toggle item rounded-corners'][3]/i[@class='icon-play']").click()

    def draw_dot(self):
        self.fw.driver.find_element_by_xpath("//i[@class='icon-circle-10x10 ic-5px']").click()
        canvas = self.fw.driver.find_element_by_xpath("//section[@class='panel active']")
        ydot1 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,600,500).click()
        ydot2 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,800,500).click()
        ydot3 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,900,500).click()
        ydot1.perform()
        ydot2.perform()
        ydot3.perform()

        self.color_red()
        rdot1 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,450,450).click()
        rdot2 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,550,550).click()
        rdot3 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,650,650).click()
        rdot4 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,250,250).click()
        rdot5 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,350,350).click()
        rdot1.perform()
        rdot2.perform()
        rdot3.perform()
        rdot4.perform()
        rdot5.perform()

        self.color_purple()
        pdot1 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,600,600).click()
        pdot2 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,300,300).click()
        pdot3 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,400,400).click()
        pdot4 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,500,500).click()
        pdot1.perform()
        pdot2.perform()
        pdot3.perform()
        pdot4.perform()

        self.color_green()
        gdot1 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,250,500).click()
        gdot2 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,250,600).click()
        gdot3 = ActionChains(self.fw.driver).move_to_element_with_offset(canvas,250,400).click()
    #    ActionChains(self.fw.driver).drag_and_drop_by_offset(canvas,150,250).click()
        gdot1.perform()
        gdot2.perform()
        gdot3.perform()

    def color_red(self):
        self.fw.driver.find_element_by_xpath("//i[@class='icon-palette']").click()
        self.fw.driver.find_element_by_xpath("//div[@class='color-items active']/button[@data-original-title='Red']").click()

    def color_green(self):
        self.fw.driver.find_element_by_xpath("//i[@class='icon-palette']").click()
        self.fw.driver.find_element_by_xpath("//div[@class='color-items active']/button[@data-original-title='Green']").click()

    def color_purple(self):
        self.fw.driver.find_element_by_xpath("//i[@class='icon-palette']").click()
        self.fw.driver.find_element_by_xpath("//div[@class='color-items active']//button[@data-original-title='Purple']").click()

    def click_stop_record(self):
        self.fw.driver.find_element_by_xpath("//button[@class='toggle circle']/i[@class='icon-stop']").click()

    def click_save_to_library(self):
        self.fw.driver.find_element_by_xpath("//button[@class='btn btn-info save-to-library-btn']").click()

    def enter_file_name(self):
        self.fw.driver.find_element_by_xpath("//input[@id='analysis-name']").clear()
        self.fw.driver.find_element_by_xpath("//input[@id='analysis-name']").send_keys("Automated Video Analysis")

    def enter_file_description(self):
        self.fw.driver.find_element_by_xpath("//input[@id='analysis-description']").send_keys("This video was analyzed via Automation.")

    def click_save(self):
        self.fw.driver.find_element_by_xpath("//button[@class='btn btn-info continue-to-save']").click()

    def view_in_library(self):
        self.fw.driver.find_element_by_xpath("//button[@class='btn btn-info view-in-library']").click()

    def verify_analyzed_video_saved(self):
        #Switch to new media details page
        media_details_window = self.fw.driver.window_handles[0]
        self.fw.driver.switch_to_window(media_details_window)
        #Verify correct file name
        name = self.fw.driver.find_element_by_xpath("//div[@class='container']/h2")
        print("Checking file name: ", name.text)
        self.fw.testcase.assert_Equal(name.text, "Automated Video Analysis.webm")
        #Verify correct description
        description = self.fw.driver.find_element_by_xpath("//div[@class='col-xs-12']/p")
        print("Checking file description: ", description.text)
        self.fw.testcase.assert_Equal(description.text, "This video was analyzed via Automation.")


# -*- coding: utf-8 -*-
"""
Created on Thu Aug 16 11:15:54 2018

@author: Shanshan

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

"""

class solution:
    def twoSum(self,nums,target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        dic = {}
        for num_inx,num in enumerate(nums):
            if target-num in dic:
                return dic[target-num],num_inx
            dic[num] = num_inx


'''
Delete Middle Node: Implement an algorithm to delete a node in the middle (i.e., any node but
the first and last node, not necessarily the exact middle) of a singly linked list, given only access to
that node.

EXAMPLE
lnput:the node c from the linked lista->b->c->d->e->f
Result: nothing is returned, but the new linked list looks like a->b->d->e- >f
'''
'''
So we know that the node that we are given is the middle node and that we
can only have access to that node.

What we need to solve is what does it mean to delete the node? It means we no
longer want the value in the current node, we want the value of the next node to be pointed to
and the current node to longer exist.

So what if we keep the current node but overwrite its current value with the next node's attributes
and then delete that node? We technically don't delete the current node but we effectively accomplish
what we really want to happen: the current value of the node to disappear and for the next node's value
to be pointed to by the prev node.

The drawback to this solution is what if the given node is the last node, but Im assuming that
that wont happen. But if we want to handle that situation then if the true goal is deleting the middle
node then we could just exit the function and dont delete anything
'''

from linked_list import LinkedList

def delete_middle_node(node):
    node.data = node.next.data
    node.next = node.next.next

ll = LinkedList()
ll.insert(1)
ll.insert(2)
ll.insert(3)
ll.insert(4)
ll.insert(5)
delete_middle_node(ll.head.next.next)
assert ll.get_as_a_list() == [1,2,4,5]


import pandas as pd
import numpy as np
from . import logger as logger
import psycopg2

class DatabaseIO():

    def __init__(self, conf):

        self.devLog = logger.getDevLog(__name__)
        self.jobLog = logger.getJobLog()

        self.conf = conf

    def writeDataToDB(self, df, tableName, eng, if_exists,
                      emptyStingToNaN=True):

        if emptyStingToNaN:
            df.replace('', np.nan, inplace=True)

        df.to_sql(tableName,
                  eng,
                  if_exists=if_exists,
                  index=False)

    def readDataFromDB(self, tableName, conn, cols='*'):

        return pd.read_sql('SELECT ' + cols + ' FROM ' + tableName, con=conn)

    def customSql(self, sql, datastore):
        dbCursor = datastore.cursor()
        dbCursor.execute(sql)
        datastore.commit()

        try:
            data = dbCursor.fetchall()
            columns = [column[0] for column in dbCursor.description]
            df = pd.DataFrame(data)
            logger.describeDataFrame(df)
            if len(df) > 0:
                df.columns = columns
        except psycopg2.ProgrammingError:
            df = None

        return df


from sys import stdin,stdout

def main():
    hwb = [int(x) for x in stdin.readline().split()]
    height = hwb[0]
    width = hwb[1]
    bricks = hwb[2]
    arr = [int(x) for x in stdin.readline().split()]
    wall = height * width
    temp = 0
    rowtemp = 0
    row = False
    htemp = 1
    if sum(arr) < wall:
        print("NO")
    else:
        for x in arr:
            if temp < wall:
                temp += arr[x]
                rowtemp += arr[x]
            if temp >= (width * htemp): #rowtemp >= width
                if rowtemp == width:
                    rowtemp = 0
                    if htemp == height:
                        break
                    htemp += 1
                else:
                    row = True
                    break
        if temp == wall and row == False:
            print("YES")
        else:
            print("NO")

if __name__ == "__main__":
    main()


def crear_tabla_candidato_apreciacion(dynamodb):
    '''
    Crear una tabla en AWS DynamoDB a partir de un archivo JSON.
    :param dynamodb:
    :return:
    '''
    tabla = dynamodb.create_table(
        TableName='Candidato_Apreciacion',
        KeySchema=[
            {
                'AttributeName': 'idcandidato',
                'KeyType': 'HASH'  # Partition key
            },
            {
                'AttributeName': 'idcliente_idpuestolaboral',
                'KeyType': 'RANGE'  # Sort key
            }
        ],
        AttributeDefinitions=[
            {
                'AttributeName': 'idcandidato',
                'AttributeType': 'N'
            },
            {
                'AttributeName': 'idcliente_idpuestolaboral',
                'AttributeType': 'S'
            }
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 10,
            'WriteCapacityUnits': 10
        }
    )
    return tabla