# Warning... INSTALL NUMPY FIRST

###########################
'''
# direct run
from function.framework.reloadWrapper import reloadWrapper as reload
from function.rigging.skin.nsSkinClusterIO import nsSkinClusterIO_reFunc as skinIO
reload(skinIO)
skinIO.saveSkin()
skinIO.loadSkin()
'''

###########################

#Title: skinClusterIO.py
#Author: Noah Schnapp
#LinkedIn: https://www.linkedin.com/in/wnschnapp/
#22.11.Nov.17.Thu.15_nsSkinClusterIO\version

from function.framework.reloadWrapper import reloadWrapper as reload

###########################
####### IMPORTS ###########
###########################

import maya.cmds as cmds
import maya.mel as mel
import pymel.core as pm
import maya.api.OpenMaya as OpenMayaAPI
import maya.api.OpenMayaAnim as OpenMayaAnimAPI
import maya.OpenMayaAnim as OpenMayaAnim
import maya.OpenMaya as OpenMaya
import os
import imp
import numpy as np
import json
import time

####################################################################
#...CODE START

from function.pipeline import logger
reload(logger)

class SkinIOLogger(logger.MayaLogger):
	LOGGER_NAME = "SkinIO"

SkinIOLogger.info('Run function at:{0}'.format(__name__))

class SkinClusterIO(object):

	def __init__(self):

		#...class init
		self.cDataIO 			    = DataIO()

		#...vars
		self.name                   = ''
		self.type                  	= 'skinCluster'
		self.weightsNonZero_Array   = []
		self.weights_Array          = []
		self.infMap_Array 			= []
		self.vertSplit_Array        = []
		self.inf_Array              = []
		self.skinningMethod         = 1
		self.normalizeWeights       = 1
		self.geometry               = None
		self.blendWeights           = []
		self.vtxCount               = 0
		self.envelope        		= 1
		self.skinningMethod         = 1
		self.useComponents         	= 0
		self.normalizeWeights       = 1
		self.deformUserNormals      = 1

		pass

	def get_mesh_components_from_tag_expression(self, skinPy, tag='*'):
		geo_types = ["mesh", "nurbsSurface", "nurbsCurve"]
		for t in geo_types:
			obj = skinPy.listConnections(et=True, t=t)
			if obj:
				geo = obj[0].getShape().name()
		# Get attr out of shape
		attr_out = cmds.deformableShape(geo, localShapeOutAttr=True)[0]
		# Get the output geometry data as MObject
		sel = OpenMaya.MSelectionList()
		sel.add(geo)
		dep = OpenMaya.MObject()
		sel.getDependNode(0, dep)
		fn_dep = OpenMaya.MFnDependencyNode(dep)
		plug = fn_dep.findPlug(attr_out, True)
		obj = plug.asMObject()
		# Use the MFnGeometryData class to query the components for a tag expression
		fn_geodata = OpenMaya.MFnGeometryData(obj)
		# Components MObject
		components = fn_geodata.resolveComponentTagExpression(tag)
		dagPath = OpenMaya.MDagPath.getAPathTo(dep)
		return dagPath, components

	def get_data(self, skinCluster):

		#...get PyNode skinCluster
		skinPy = pm.PyNode(skinCluster)

		#...Pre Maya 2022 or new compoent tag expression
		try:
			fnSet = OpenMaya.MFnSet(skinPy.__apimfn__().deformerSet())
			members = OpenMaya.MSelectionList()
			fnSet.getMembers(members, False)
			dagPath = OpenMaya.MDagPath()
			components = OpenMaya.MObject()
			members.getDagPath(0, dagPath, components)
		except:
			dagPath, components = self.get_mesh_components_from_tag_expression(skinPy)

		#...get mesh
		geometry = cmds.skinCluster(skinCluster, query = True, geometry = True)[0]

		#...get vtxID_Array
		vtxID_Array = list(range(0,len(cmds.ls('%s.vtx[*]'%geometry,fl=1))))

		#...get skin
		selList = OpenMayaAPI.MSelectionList()
		selList.add(mel.eval('findRelatedSkinCluster %s' % geometry))
		skinPath = selList.getDependNode(0)

		#...get mesh
		selList = OpenMayaAPI.MSelectionList()
		selList.add(geometry)
		meshPath = selList.getDagPath(0)

		#...get vtxs
		fnSkinCluster = OpenMayaAnimAPI.MFnSkinCluster(skinPath);
		fnVtxComp = OpenMayaAPI.MFnSingleIndexedComponent()
		vtxComponents = fnVtxComp.create( OpenMayaAPI.MFn.kMeshVertComponent )
		fnVtxComp.addElements(vtxID_Array);

		#...get weights/infs
		dWeights, infCount = fnSkinCluster.getWeights(meshPath, vtxComponents)
		# weights_Array = np.array(list(dWeights), dtype='float64')
		#... change this due to warning
		weights_Array = np.array(list(dWeights), dtype='object')
		inf_Array = [dp.partialPathName() for dp in fnSkinCluster.influenceObjects()]

		#...convert to weightsNonZero_Array
		weightsNonZero_Array, infMap_Array, vertSplit_Array = self.compress_weightData(weights_Array, infCount)

		#...gatherBlendWeights
		blendWeights_mArray = OpenMaya.MDoubleArray()
		skinPy.__apimfn__().getBlendWeights(dagPath, components, blendWeights_mArray)

		#...set data to self vars
		self.name 					= skinCluster
		self.weightsNonZero_Array   = np.array(weightsNonZero_Array)
		self.infMap_Array       	= np.array(infMap_Array)
		self.vertSplit_Array       	= np.array(vertSplit_Array)
		self.inf_Array              = np.array(inf_Array)
		self.geometry               = geometry
		self.blendWeights           = np.array(blendWeights_mArray)
		self.vtxCount               = len(vertSplit_Array)-1

		#...get attrs
		self.envelope         		= cmds.getAttr(skinCluster + ".envelope")
		self.skinningMethod         = cmds.getAttr(skinCluster + ".skinningMethod")
		self.useComponents       	= cmds.getAttr(skinCluster + ".useComponents")
		self.normalizeWeights       = cmds.getAttr(skinCluster + ".normalizeWeights")
		self.deformUserNormals      = cmds.getAttr(skinCluster + ".deformUserNormals")

		return True

	def set_data(self, skinCluster):

		#...get PyNode skinCluster
		skinPy = pm.PyNode(skinCluster)

		#...Pre Maya 2022 or new compoent tag expression
		try:
			fnSet = OpenMaya.MFnSet(skinPy.__apimfn__().deformerSet())
			members = OpenMaya.MSelectionList()
			fnSet.getMembers(members, False)
			dagPath = OpenMaya.MDagPath()
			components = OpenMaya.MObject()
			members.getDagPath(0, dagPath, components)
		except:
			dagPath, components = self.get_mesh_components_from_tag_expression(skinPy)

		###################################################

		#...set infs
		influencePaths = OpenMaya.MDagPathArray()
		infCount = skinPy.__apimfn__().influenceObjects(influencePaths)
		influences_Array = [influencePaths[i].partialPathName() for i in range(influencePaths.length())]

		#...change the order in set(i,i)
		influenceIndices = OpenMaya.MIntArray(infCount)
		[influenceIndices.set(i,i) for i in range(infCount)]

		###################################################

		#...construct mArrays from normal/numpy arrays
		infCount = len(influences_Array)
		weightCounter = 0
		weights_Array = []
		weights_mArray = OpenMaya.MDoubleArray()
		length = len(self.vertSplit_Array)
		for vtxId, splitStart in enumerate(self.vertSplit_Array):
			if vtxId < length-1:
				vertChunk_Array = [0]*infCount
				splitEnd = self.vertSplit_Array[vtxId+1]

				#...unpack data and replace zeros with nonzero weight vals
				for i in range(splitStart, splitEnd):
					infMap = self.infMap_Array[i]
					val = self.weightsNonZero_Array[i]
					vertChunk_Array[infMap] = val

				#...append to raw data array
				for vert in vertChunk_Array:
					weights_mArray.append(vert)

		blendWeights_mArray = OpenMaya.MDoubleArray()
		for i in self.blendWeights:
			blendWeights_mArray.append(i)

		###################################################
		#...set data
		# skinPy.setWeights(dagPath, components, influenceIndices, weights_mArray, False)
		SkinIOLogger.info('ERROR HERE WHY')
		skinPy.__apimfn__().setWeights(dagPath, components, influenceIndices, weights_mArray, False)
		SkinIOLogger.info('ERROR HERE WHY')
		#skinPy.setBlendWeights(dagPath, components, blendWeights_mArray)
		skinPy.__apimfn__().setBlendWeights(dagPath, components, blendWeights_mArray)
		###################################################
		#...set attrs of skinCluster
		cmds.setAttr('%s.envelope'%skinCluster, self.envelope)
		cmds.setAttr('%s.skinningMethod'%skinCluster, self.skinningMethod)
		cmds.setAttr('%s.useComponents'%skinCluster, self.useComponents)
		cmds.setAttr('%s.normalizeWeights'%skinCluster, self.normalizeWeights)
		cmds.setAttr('%s.deformUserNormals'%skinCluster, self.deformUserNormals)

		#...name
		cmds.rename(skinCluster, self.name)

		return True
	'''
	def save(self, node=None, dirpath=None):

		#...get selection
		if node == None:
			node = cmds.ls(sl=1)
			if node == None:
				print('ERROR: Select Something!')
				return False
			else:
				node = node[0]

		#...get skinCluster
		skinCluster = mel.eval('findRelatedSkinCluster ' + node)
		if not cmds.objExists(skinCluster):
			print('ERROR: Node has no skinCluster!')
			return False

		#...get dirpath
		if dirpath == None:
			startDir = cmds.workspace(q=True, rootDirectory=True)
			dirpath = cmds.fileDialog2(caption='Save Skinweights', dialogStyle=2, fileMode=3, startingDirectory=startDir, fileFilter='*.npy', okCaption = "Select")

		#...get filepath
		skinCluster = 'skinCluster_%s'%node
		filepath = '%s/%s.npy'%(dirpath, skinCluster)

		#...timeStart
		timeStart = time.time()

		#...get data
		self.get_data(skinCluster)

		#...timeEnd
		timeEnd = time.time()
		timeElapsed = timeEnd-timeStart

		#...print time
		print('GetData Elapsed: %s'%timeElapsed)

		#...construct data_array
		legend =            (   'legend',
								'weightsNonZero_Array',
								'vertSplit_Array',
								'infMap_Array',

								'inf_Array',
								'geometry',
								'blendWeights',
								'vtxCount',

								'name',
								'envelope',
								'skinningMethod',
								'useComponents',

								'normalizeWeights',
								'deformUserNormals',

								'type',
								)

		data =              [   legend,
								self.weightsNonZero_Array,
								self.vertSplit_Array,
								self.infMap_Array,

								self.inf_Array,
								self.geometry,
								self.blendWeights,
								self.vtxCount,

								self.name,
								self.envelope,
								self.skinningMethod,
								self.useComponents,

								self.normalizeWeights,
								self.deformUserNormals,

								self.type,
								]

		#...timeStart
		timeStart = time.time()

		#...write data
		np.save(filepath, data)

		#...timeEnd
		timeEnd = time.time()
		timeElapsed = timeEnd-timeStart

		#...print time
		print('SaveData Elapsed: %s'%timeElapsed)

		return True

	def load(self, node=None, dirpath=None):

		#...get selection
		if node == None:
			node = cmds.ls(sl=1)
			if node == None:
				print('ERROR: Select Something!')
				return False
			else:
				node = node[0]

		#...get dirpath
		if dirpath == None:
			startDir = cmds.workspace(q=True, rootDirectory=True)
			dirpath = cmds.fileDialog2(caption='Load Skinweights', dialogStyle=2, fileMode=1, startingDirectory=startDir, fileFilter='*.npy', okCaption = "Select")

		#...get filepath
		skinCluster = 'skinCluster_%s'%node
		filepath = '%s/%s.npy'%(dirpath, skinCluster)

		#...check if skinCluster exists
		if not os.path.exists(filepath):
			print('ERROR: SkinCluster for node "%s" not found on disk!'%node)
			return False

		#...unbind current skinCluster
		skinCluster = mel.eval('findRelatedSkinCluster ' + node)
		if cmds.objExists(skinCluster):
			mel.eval('skinCluster -e  -ub ' + skinCluster)

		#...timeStart
		timeStart = time.time()

		#...read data
		data = np.load(filepath, allow_pickle=True)

		#...timeEnd
		timeEnd = time.time()
		timeElapsed = timeEnd-timeStart

		#...print time
		print('ReadData Elapsed: %s'%timeElapsed)

		#...get item data from numpy array
		self.legend_Array = self.cDataIO.get_legendArrayFromData(data)
		self.weightsNonZero_Array = self.cDataIO.get_dataItem(data, 'weightsNonZero_Array', self.legend_Array)
		self.infMap_Array = self.cDataIO.get_dataItem(data, 'infMap_Array', self.legend_Array)
		self.vertSplit_Array = self.cDataIO.get_dataItem(data, 'vertSplit_Array', self.legend_Array)
		self.inf_Array = self.cDataIO.get_dataItem(data, 'inf_Array', self.legend_Array)
		self.blendWeights = self.cDataIO.get_dataItem(data, 'blendWeights', self.legend_Array)
		self.vtxCount = self.cDataIO.get_dataItem(data, 'vtxCount', self.legend_Array)
		self.geometry = self.cDataIO.get_dataItem(data, 'geometry', self.legend_Array)
		self.name = self.cDataIO.get_dataItem(data, 'name', self.legend_Array)
		self.envelope = self.cDataIO.get_dataItem(data, 'envelope', self.legend_Array)
		self.skinningMethod = self.cDataIO.get_dataItem(data, 'skinningMethod', self.legend_Array)
		self.useComponents = self.cDataIO.get_dataItem(data, 'useComponents', self.legend_Array)
		self.normalizeWeights = self.cDataIO.get_dataItem(data, 'normalizeWeights', self.legend_Array)
		self.deformUserNormals = self.cDataIO.get_dataItem(data, 'deformUserNormals', self.legend_Array)

		#...bind skin
		for inf in self.inf_Array:
			if not cmds.objExists(inf):
				cmds.select(cl=True)
				cmds.joint(n=inf)
		skinCluster = 'skinCluster_%s'%node
		skinCluster = cmds.skinCluster(self.inf_Array, node, n=skinCluster, tsb = True)[0]

		#...timeStart
		timeStart = time.time()

		#...set data
		self.set_data(skinCluster)

		#...timeEnd
		timeEnd = time.time()
		timeElapsed = timeEnd-timeStart

		#...print time
		print('SetData Elapsed: %s'%timeElapsed)

		return True
	'''
	###################################
	def compress_weightData(self, weights_Array, infCount):

		#...convert to weightsNonZero_Array
		weightsNonZero_Array = []
		infCounter = 0
		infMap_Chunk = []
		infMap_ChunkCount = 0
		vertSplit_Array = [infMap_ChunkCount]
		infMap_Array = []

		for w in weights_Array:
			if w != 0.0:
				weightsNonZero_Array.append(w)
				infMap_Chunk.append(infCounter)

			#...update inf counter
			infCounter += 1
			if infCounter ==  infCount:
				infCounter = 0

				#...update vertSplit_Array
				infMap_Array.extend(infMap_Chunk)
				infMap_ChunkCount = len(infMap_Chunk)+infMap_ChunkCount
				vertSplit_Array.append(infMap_ChunkCount)
				infMap_Chunk = []

		return weightsNonZero_Array, infMap_Array, vertSplit_Array

class DataIO(object):

	def __init__(self):

		pass

	@staticmethod
	def get_legendArrayFromData(data):

		return data[0]

	@staticmethod
	def get_dataItem(data, item, legend_Array=None):
		if item not in data[0]:
			print('ERROR: "%s" Not Found in data!'%item)
			return False
		#...no legend_Array
		if legend_Array is None:
			legend_Array = [key for key in data[0]]
		#...with legend_Array
		return data[legend_Array.index(item)]

	@staticmethod
	def set_dataItems(data, itemData_Array):

		return data

from function.pipeline import fileTools as fileTools
reload (fileTools)

def _processPath():
	dataRaw = fileTools.getAssetData()
	mayaFileName = dataRaw[0][-1]

	if 'hero' in mayaFileName:
		print ('This is hero file')
		filePath  = fileTools.desinatePath('\\RIG\\data\\skinData')

	else:
		print ('This is version file')
		filePath  = fileTools.desinatePath('\\data\\skinData')

	fileTools.currentFilePath(filePath)
	return filePath

def saveSkin():
	node = cmds.ls(sl=True)[0]

	dirpath = _processPath()
	if dirpath == None:
		print('ERROR: There are no path.')
		return	False
	######################################
	## run line by line
	## SAVE
	####################################

	# run line by line

	#... get selection
	if node == None:
		print('ERROR: Please select something!.')
		return	False

	#... get skinCluster
	skinCluster_name = mel.eval('findRelatedSkinCluster ' + node)
	if not cmds.objExists(skinCluster_name):
		print('ERROR: Node has no skinCluster!!')
		#return False

	#... get file path
	skinCluster = '%s_skinCluster' %node
	filepath = '%s/%s.npy'%(dirpath, skinCluster)

	#... time start
	timeStart = time.time()

	SkinClusterExport = SkinClusterIO()
	SkinClusterExport.get_data(skinCluster_name)

	#... time end
	timeEnd = time.time()
	timeElapsed = timeEnd-timeStart
	print('Get data Elapsed: %s'%timeElapsed)

	#...construct data_array
	legend =            (   'legend',
							'weightsNonZero_Array',
							'vertSplit_Array',
							'infMap_Array',

							'inf_Array',
							'geometry',
							'blendWeights',
							'vtxCount',

							'name',
							'envelope',
							'skinningMethod',
							'useComponents',

							'normalizeWeights',
							'deformUserNormals',

							'type',
							)

	data =              [   legend,
							SkinClusterExport.weightsNonZero_Array,
							SkinClusterExport.vertSplit_Array,
							SkinClusterExport.infMap_Array,

							SkinClusterExport.inf_Array,
							SkinClusterExport.geometry,
							SkinClusterExport.blendWeights,
							SkinClusterExport.vtxCount,

							SkinClusterExport.name,
							SkinClusterExport.envelope,
							SkinClusterExport.skinningMethod,
							SkinClusterExport.useComponents,

							SkinClusterExport.normalizeWeights,
							SkinClusterExport.deformUserNormals,

							SkinClusterExport.type,
							]

	timeStart = time.time()

	np.save(filepath, data)
	cmds.select(deselect=True)
	timeEnd = time.time()
	timeElapsed = timeEnd-timeStart
	print('Saved data Elapsed: %s'%timeElapsed)

	print('File save at: ---> %s' %filepath)

	return True

def loadSkin():

	######################################
	## run line by line
	## LOAD
	####################################

	dirpath = _processPath()
	if dirpath == None:
		print('ERROR: There are no path.')
		return	False

	node = cmds.ls(sl=True)[0]

	#... get selection
	if node == None:
		print('ERROR: Please select something!.')
		return	False
	print('selected name is ---> %s' %node)

	#...get filepath
	skinCluster_name = '%s_skinCluster'%node
	filepath = '%s/%s.npy'%(dirpath, skinCluster_name)

	filepath = os.path.normpath(filepath)

	#...check if skinCluster exists
	if not os.path.exists(filepath):
		print('ERROR: SkinCluster for node "%s" not found on disk!'%node)
		#return False

	#...unbind current skinCluster
	skinCluster = mel.eval('findRelatedSkinCluster ' + node)
	if cmds.objExists(skinCluster):
		mel.eval('skinCluster -e  -ub ' + skinCluster)

	#...timeStart
	timeStart = time.time()

	SkinIOLogger.info('This is filepath: {0}'.format(filepath))

	#...read data
	data = np.load(filepath, allow_pickle=True)

	SkinClusterImport = SkinClusterIO()

	#...get item data from numpy array
	legend_Array = SkinClusterImport.cDataIO.get_legendArrayFromData(data)
	weightsNonZero_Array = SkinClusterImport.cDataIO.get_dataItem(data, 'weightsNonZero_Array',legend_Array)
	infMap_Array = SkinClusterImport.cDataIO.get_dataItem(data, 'infMap_Array',legend_Array)
	vertSplit_Array = SkinClusterImport.cDataIO.get_dataItem(data, 'vertSplit_Array',legend_Array)
	inf_Array = SkinClusterImport.cDataIO.get_dataItem(data, 'inf_Array',legend_Array)
	blendWeights = SkinClusterImport.cDataIO.get_dataItem(data, 'blendWeights',legend_Array)
	vtxCount = SkinClusterImport.cDataIO.get_dataItem(data, 'vtxCount',legend_Array)
	geometry = SkinClusterImport.cDataIO.get_dataItem(data, 'geometry',legend_Array)
	name = SkinClusterImport.cDataIO.get_dataItem(data, 'name',legend_Array)
	envelope = SkinClusterImport.cDataIO.get_dataItem(data, 'envelope',legend_Array)
	skinningMethod = SkinClusterImport.cDataIO.get_dataItem(data, 'skinningMethod',legend_Array)
	useComponents = SkinClusterImport.cDataIO.get_dataItem(data, 'useComponents',legend_Array)
	normalizeWeights = SkinClusterImport.cDataIO.get_dataItem(data, 'normalizeWeights',legend_Array)
	deformUserNormals = SkinClusterImport.cDataIO.get_dataItem(data, 'deformUserNormals',legend_Array)

	#...bind skin
	for inf in inf_Array:
		if not cmds.objExists(inf):
			cmds.select(cl=True)
			cmds.joint(n=inf)
	skinCluster = '%s_skinCluster'%node
	skinCluster_name = cmds.skinCluster(inf_Array, node, n=skinCluster, tsb = True)[0]

	#...timeStart
	timeStart = time.time()

	#...set data
	# not work breakdown the code
	#SkinClusterImport.set_data(skinCluster)

	#...timeEnd

	#...get PyNode skinCluster

	#skinCluster_name = skinCluster
	skinPy = pm.PyNode(skinCluster_name)

	#...Pre Maya 2022 or new compoent tag expression
	try:
		fnSet = OpenMaya.MFnSet(skinPy.__apimfn__().deformerSet())
		members = OpenMaya.MSelectionList()
		fnSet.getMembers(members, False)
		dagPath = OpenMaya.MDagPath()
		components = OpenMaya.MObject()
		members.getDagPath(0, dagPath, components)
	except:
		dagPath, components = SkinClusterImport.get_mesh_components_from_tag_expression(skinPy)

	###################################################

	#...set infs
	influencePaths = OpenMaya.MDagPathArray()
	infCount = skinPy.__apimfn__().influenceObjects(influencePaths)
	influences_Array = [influencePaths[i].partialPathName() for i in range(influencePaths.length())]

	#...change the order in set(i,i)
	influenceIndices = OpenMaya.MIntArray(infCount)
	[influenceIndices.set(i,i) for i in range(infCount)]

	###################################################

	#...construct mArrays from normal/numpy arrays
	infCount = len(influences_Array)
	weightCounter = 0
	weights_Array = []
	weights_mArray = OpenMaya.MDoubleArray()
	length = len(vertSplit_Array)
	for vtxId, splitStart in enumerate(vertSplit_Array):
		if vtxId < length-1:
			vertChunk_Array = [0]*infCount
			splitEnd = vertSplit_Array[vtxId+1]

			#...unpack data and replace zeros with nonzero weight vals
			for i in range(splitStart, splitEnd):
				infMap = infMap_Array[i]
				val = weightsNonZero_Array[i]
				vertChunk_Array[infMap] = val

			#...append to raw data array
			for vert in vertChunk_Array:
				weights_mArray.append(vert)

	blendWeights_mArray = OpenMaya.MDoubleArray()
	for i in blendWeights:
		blendWeights_mArray.append(i)

	###################################################
	#...set data
	# skinPy.setWeights(dagPath, components, influenceIndices, weights_mArray, False)

	skinPy.__apimfn__().setWeights(dagPath, components, influenceIndices, weights_mArray, False)
	#skinPy.setBlendWeights(dagPath, components, blendWeights_mArray)
	skinPy.__apimfn__().setBlendWeights(dagPath, components, blendWeights_mArray)
	###################################################
	#...set attrs of skinCluster
	cmds.setAttr('%s.envelope'%skinCluster_name, envelope)
	cmds.setAttr('%s.skinningMethod'%skinCluster_name, skinningMethod)
	cmds.setAttr('%s.useComponents'%skinCluster_name, useComponents)
	cmds.setAttr('%s.normalizeWeights'%skinCluster_name, normalizeWeights)
	cmds.setAttr('%s.deformUserNormals'%skinCluster_name, deformUserNormals)

	#...timeEnd
	timeEnd = time.time()
	timeElapsed = timeEnd-timeStart
	#...print time
	print('ReadData Elapsed: %s'%timeElapsed)

	#...name
	cmds.rename(skinCluster_name, name)
	print('File load at: ---> %s' %filepath)

	cmds.select(deselect=True)
	return True

'''
saveSkin()
loadSkin()
'''


from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.shortcuts import render
from django.core.urlresolvers import reverse
from hostlist import models
from saltapi.saltAPI import saltAPI
from hostlist.models import HostList, HostAsset, IdcAsset, Maintainer
from hostlist.asset_info import *
from django.contrib.auth.decorators import login_required
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from django.core import serializers
import json

# Create your views here.
def idc_list(request):
    """
    list all idc
    """
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    idc_list = IdcAsset.objects.all()
    p = Paginator(idc_list, 7, request=request)
    idc_list = p.page(page)

    return render(request, 'idc_list.html', locals())

def host_list(request):
    '''
    list all host
    '''
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    all_host = HostList.objects.all()
    idc_list = IdcAsset.objects.all()
    user_list = Maintainer.objects.all()

    idc_id = request.GET.get('idc_id')
    user_id = request.GET.get('user_id')
    hostname = request.POST.get('hostname')
    if idc_id:
        all_host = HostList.objects.filter(idc = idc_id)
    elif user_id:
        all_host = HostList.objects.filter(maintainer = user_id)
    elif hostname:
        all_host = HostList.objects.filter(ip__hostname__icontains = hostname)
    else:
        all_host = all_host.order_by('ip')
    p = Paginator(all_host, 7, request=request)
    all_host = p.page(page)

    return render(request, 'host_list.html', locals())

def get_hostasset(request):
    """
    get all hostasset
    """
    if request.method == 'GET':
        action = request.get_full_path().split('=')[1]
        if action == 'refresh':
            sapi = saltAPI()
            accepted, denied, unaccept, rejected = sapi.list_all_key()
            tgt = [i for i in accepted]
            ret = multitle_collect(tgt)
            for i in ret:
                h = HostAsset.objects.get_or_create(ip=i[0], hostname=i[1], fqdn=i[2], domain=i[3], macaddress=i[4], os=i[5],osarch=i[6], osrelease=i[7], manufacturer=i[8], productname=i[9], serialnumber=i[10], cpu_model=i[11], cpu_nums=i[12], cpu_groups=i[13], mem=i[14], virtual=i[15])
                HostList.objects.get_or_create(ip = h[0])
        return HttpResponseRedirect(reverse('host_list'))

def hostasset_list(request, ip):
    """
    list all hostasset
    """
    all_hostasset = HostAsset.objects.get(ip = ip)
    return render(request, 'hostasset_list.html', locals())


import tensorflow as tf
import keras
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
import os
from keras import optimizers
from keras import regularizers

#read in data using pandas
cwd = os.getcwd()
train_df = pd.read_csv(cwd + "/data.csv")
#validation_df = pd.read_csv(cwd + "/dataTest.csv")

#view data structure
train_df.head()
#validation_df.head()

#create a dataframe with all training data except the target column
train_X = train_df.drop(columns=['Class'])
#validation_X = validation_df.drop(columns=['Class'])

#check that the target variable has been removed
train_X.head()
#validation_X.head()

from keras.utils import to_categorical

#one-hot encode target column
train_y = to_categorical(train_df.Class)
#validation_y = to_categorical(validation_df.Class)

#vcheck that target column has been converted
train_y[0:5]
#validation_y[0:5]

#create model
model = Sequential()

#get number of columns in training data
n_cols = train_X.shape[1]

#early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=1, mode='auto')
#model.add(Dense(52, input_dim=52,
#            kernel_regularizer=regularizers.l2(0.01),
#            activity_regularizer=regularizers.l1(0.01)))
#add layers to model
model.add(Dense(52, activation='sigmoid', input_shape=(n_cols,)))
model.add(Dense(20, activation='sigmoid'))
model.add(Dense(20, activation='sigmoid'))
model.add(Dense(4, activation='softmax'))

#opt=optimizers.SGD(lr=0.1, momentum=0.1)
#compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

#train model
model.fit(train_X, train_y, epochs=200, validation_split=1)

print(model.evaluate(train_X, train_y, verbose=0))

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")


import pandas as pd
sal = pd.read_csv('salaries.csv')
sal.head()
sal.info()

# remove nan and string values
# get average of base salary
a = a['BasePay'].apply(pd.to_numeric, errors='coerce').dropna().mean()

# get highest paid guy
sal[sal['TotalPayBenefits'] == sal['TotalPayBenefits'].max()]['EmployeeName']
# or...
sal.loc[sal['TotalPayBenefits'].idxmax()]

# lowest paid
sal.loc[sal['TotalPayBenefits'].argmin()]

# mean pay by year
sal.groupby('Year').mean()

# num. uniqut titles
sal['JobTitle'].nunique()

# top 10 job titles
sal['JobTitle'].value_counts()[0:10]

# job titles with 1 count in 2013
sum(sal[sal.Year == 2013].JobTitle.value_counts() == 1)

# any title with 'chief'
sum(sal['JobTitle'].apply(lambda x: fun_if_includes_chief(x)))

# correlation between len(JobTitle) and salary?
# apply is like JavaScript apply
sal['title_len'] = sal['JobTitle'].apply(len)
# no correlation...obv.
sal[['TotalPayBenefits', 'title_len']].corr()
# below is 1. Of course.

# Also 1.
sal[['OvertimePay', 'Year']].corr()

sal[['JobTitle', 'title_len']].corr()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('pages', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='page',
            name='lock_state',
            field=models.CharField(choices=[('UU', 'Unlocked'), ('LL', 'Locked')], max_length=2, default='UU'),
            preserve_default=True,
        ),
    ]


from keras.layers import Activation, Reshape, Lambda, concatenate, dot, add
from keras.layers import Conv1D, Conv2D, Conv3D
from keras.layers import MaxPool1D
from keras import backend as K

def non_local_block(ip, shield_computation=True, mode='embedded'):
    channel_dim = 1 if K.image_data_format() == 'channels_first' else -1
    ip_shape = K.int_shape(ip)

    if mode not in ['gaussian', 'embedded', 'dot', 'concatenate']:
        raise ValueError('`mode` must be one of `gaussian`, `embedded`, `dot` or `concatenate`')

    dim1, dim2, dim3 = None, None, None

    if len(ip_shape) == 3:  # time series data
        rank = 3
        batchsize, dim1, channels = ip_shape

    elif len(ip_shape) == 4:  # image data
        rank = 4

        if channel_dim == 1:
            batchsize, channels, dim1, dim2 = ip_shape
        else:
            batchsize, dim1, dim2, channels = ip_shape

    elif len(ip_shape) == 5:  # Video / Voxel data
        rank = 5

        if channel_dim == 1:
            batchsize, channels, dim1, dim2, dim3 = ip_shape
        else:
            batchsize, dim1, dim2, dim3, channels = ip_shape

    else:
        raise ValueError('Input dimension has to be either 3 (temporal), 4 (spatial) or 5 (spatio-temporal)')

    if mode == 'gaussian':  # Gaussian instantiation
        x1 = Reshape((-1, channels))(ip)  # xi
        x2 = Reshape((-1, channels))(ip)  # xj
        f = dot([x1, x2], axes=2)
        f = Activation('softmax')(f)

    elif mode == 'dot':  # Dot instantiation
        # theta path
        if rank == 3:
            theta = Conv1D(channels // 2, 1, padding='same', use_bias=False)(ip)
        elif rank == 4:
            theta = Conv2D(channels // 2, (1, 1), padding='same', use_bias=False)(ip)
        else:
            theta = Conv3D(channels // 2, (1, 1, 1), padding='same', use_bias=False)(ip)

        theta = Reshape((-1, channels // 2))(theta)

        # phi path
        if rank == 3:
            phi = Conv1D(channels // 2, 1, padding='same', use_bias=False)(ip)
        elif rank == 4:
            phi = Conv2D(channels // 2, (1, 1), padding='same', use_bias=False)(ip)
        else:
            phi = Conv3D(channels // 2, (1, 1, 1), padding='same', use_bias=False)(ip)

        phi = Reshape((-1, channels // 2))(phi)

        f = dot([theta, phi], axes=2)

        # scale the values to make it size invariant
        f = Lambda(lambda z: 1./batchsize * f)(f)

    elif mode == 'concatenate':  # Concatenation instantiation
        raise NotImplemented('Concatenation mode has not been implemented yet')

    else:  # Embedded Gaussian instantiation
        # theta path
        if rank == 3:
            theta = Conv1D(channels // 2, 1, padding='same', use_bias=False)(ip)
        elif rank == 4:
            theta = Conv2D(channels // 2, (1, 1), padding='same', use_bias=False)(ip)
        else:
            theta = Conv3D(channels // 2, (1, 1, 1), padding='same', use_bias=False)(ip)

        theta = Reshape((-1, channels // 2))(theta)

        # phi path
        if rank == 3:
            phi = Conv1D(channels // 2, 1, padding='same', use_bias=False)(ip)
        elif rank == 4:
            phi = Conv2D(channels // 2, (1, 1), padding='same', use_bias=False)(ip)
        else:
            phi = Conv3D(channels // 2, (1, 1, 1), padding='same', use_bias=False)(ip)

        phi = Reshape((-1, channels // 2))(phi)

        if shield_computation:
            # shielded computation
            phi = MaxPool1D()(phi)

        f = dot([theta, phi], axes=2)
        f = Activation('softmax')(f)

    # g path
    if rank == 3:
        g = Conv1D(channels // 2, 1, padding='same', use_bias=False)(ip)
    elif rank == 4:
        g = Conv2D(channels // 2, (1, 1), padding='same', use_bias=False)(ip)
    else:
        g = Conv3D(channels // 2, (1, 1, 1), padding='same', use_bias=False)(ip)

    g = Reshape((-1, channels // 2))(g)

    if shield_computation and mode == 'embedded':
        # shielded computation
        g = MaxPool1D()(g)

    # compute output path
    y = dot([f, g], axes=[2, 1])

    # reshape to input tensor format
    if rank == 3:
        y = Reshape((dim1, channels // 2))(y)
    elif rank == 4:
        if channel_dim == -1:
            y = Reshape((dim1, dim2, channels // 2))(y)
        else:
            y = Reshape((channels // 2, dim1, dim2))(y)
    else:
        if channel_dim == -1:
            y = Reshape((dim1, dim2, dim3, channels // 2))(y)
        else:
            y = Reshape((channels // 2, dim1, dim2, dim3))(y)

    # project filters
    y = Conv2D(channels, (1, 1), padding='same', use_bias=False)(y)

    # residual connection
    residual = add([ip, y])

    return residual


from loguru import logger
from loguru._logger import Logger
from pathlib import Path
import datetime, sys

# 路径设置
LOG_PATH = "logs"
# INFO显示模板
INFO_FORMAT_TEMPLATE = "<green>{time: YYYY/MM/DD HH:mm:ss}</green> <red>|</red> <green>{level}</green> <red>|</red> {message}"
# SUCCESS显示模板
SUCCESS_FORMAT_TEMPLATE = INFO_FORMAT_TEMPLATE
# Warning显示模板
WARNING_FORMAT_TEMPLATE = "<green>{time: YYYY/MM/DD HH:mm:ss}</green> <red>|</red> <yellow>{level}</yellow> <red>|</red> {message}"
# ERROR显示模板
ERROR_FORMAT_TEMPLATE = "<green>{time: YYYY/MM/DD HH:mm:ss}</green> <red>|</red> <red>{level}</red> <red>|</red> {message}"
# 获取当前事件
DATE = datetime.datetime.now()

def level_filter(level: str):
    def is_level(record: dict):
        return record["level"].name == level
    return is_level

class LocalLogger:
    """ 日志封装 """

    def __init__(self) -> None:
        self.logger: Logger = logger.opt()
        # 调整logger设置
        self.logger.remove(0)
        self.__set_error_logger()
        self.__set_warning_logger()
        self.__set_success_logger()
        self.__set_info_logger()

    def __set_info_logger(self) -> None:
        """ 设置INFO日志配置 """
        # 日志文件名
        all_log_file_name = "all_logs-%s.log" % (DATE.strftime('%Y-%m-%d-%H_%M_%S'))
        all_log_file_path: Path = Path(__file__).parents[0].parents[0].resolve() / LOG_PATH / all_log_file_name
        # 添加日志配置
        self.logger.add(
            sink = all_log_file_path,
            format = INFO_FORMAT_TEMPLATE,
            level = "INFO"
        )

    def __set_success_logger(self) -> None:
        """ 设置SUCCESS日志配置 """
        # 添加日志配置
        self.logger.add(
            sink = sys.stderr,
            format = SUCCESS_FORMAT_TEMPLATE,
            level = "SUCCESS",
            filter = level_filter(level = "SUCCESS")
        )

    def __set_warning_logger(self) -> None:
        """ 设置Warning日志配置 """
        # 添加日志配置
        self.logger.add(
            sink = sys.stderr,
            format = WARNING_FORMAT_TEMPLATE,
            level = "WARNING",
            filter = level_filter(level = "WARNING")
        )

    def __set_error_logger(self) -> None:
        """ 设置ERROR日志配置 """
        # 日志文件名
        err_log_file_name = "err_logs-%s.log" % (DATE.strftime('%Y-%m-%d-%H_%M_%S'))
        err_log_file_path: Path = Path(__file__).parents[0].parents[0].resolve() / LOG_PATH / err_log_file_name
        # 添加日志配置
        self.logger.add(
            sink = err_log_file_path,
            level = "ERROR",
            format = ERROR_FORMAT_TEMPLATE
        )


"""Validate and parse expressions into SQLAlchemy expressions"""

import functools
from dataclasses import dataclass
from datetime import date, datetime

from lark import GrammarError
from sqlalchemy import func
from sqlalchemy.sql.expression import ClauseElement

from sqlalchemy_recipe.dbinfo import DBInfo, ReflectedTable
from sqlalchemy_recipe.expression.datatype import DataType
from sqlalchemy_recipe.expression.grammar import make_columns_for_table
from sqlalchemy_recipe.expression.transformer import TransformToSQLAlchemyExpression
from sqlalchemy_recipe.expression.validator import SQLALchemyValidator

@dataclass
class BuilderResponse:
    datatype: DataType
    expression: ClauseElement
    is_aggr: bool

class SQLAlchemyBuilder:
    def __init__(self, dbinfo: DBInfo, reflected_table: ReflectedTable):
        """Parse an expression by building a custom grammar that
        uses the columns in a table.

        Args:
            dbinfo (DBInfo): _description_
            table (Table): _description_
            grammar (str): _description_
        """
        self.dbinfo = dbinfo
        self.reflected_table = reflected_table
        self.drivername = self.dbinfo.engine.url.drivername
        self.transformer = TransformToSQLAlchemyExpression(
            reflected_table, self.dbinfo.engine.url.drivername
        )

    @functools.lru_cache(maxsize=None)
    def parse(
        self,
        text: str,
        forbid_aggregation: bool = False,
        enforce_aggregation: bool = False,
        convert_dates_with: str = None,
        convert_datetimes_with: str = None,
        default_aggregation=func.sum,
        debug: bool = False,
    ) -> BuilderResponse:
        """Return a parse tree for text

        Args:
            text (str): A field expression
            forbid_aggregation (bool, optional):
              The expression may not contain aggregations. Defaults to False.
            enforce_aggregation (bool, optional):
              Wrap the expression in an aggregation if one is not provided. Defaults to False.
            debug (bool, optional): Show some debug info. Defaults to False.
            convert_dates_with (str, optional): A converter to use for date fields
            convert_datetimes_with (str, optional): A converter to use for datetime fields

        Raises:
            GrammarError: A description of any errors and where they occur

        Returns:
            BuilderResponse
        """
        tree = self.reflected_table.parser.parse(text, start="col")
        validator = SQLALchemyValidator(
            text, forbid_aggregation, self.dbinfo.engine.url.drivername
        )
        # Populate errors and check aggregation
        validator.visit(tree)
        datatype = validator.last_datatype

        if validator.errors:
            if debug:
                print("".join(map(str, validator.errors)))
                print("Tree:\n" + tree.pretty())
            # Validator.errors is a list of VisitError
            ge = GrammarError("".join(map(str, validator.errors)))
            ge.validator_errors = validator.errors
            raise ge

        if debug:
            print("Tree:\n" + tree.pretty())

        self.transformer.text = text
        self.transformer.convert_dates_with = convert_dates_with
        self.transformer.convert_datetimes_with = convert_datetimes_with

        # Build an expression from the bottom of the tree up.
        expr = self.transformer.transform(tree)

        # Expressions that return literal values can't be labeled
        # Possibly we could wrap them in text() but this may be unsafe
        # instead we will disallow them.
        if isinstance(expr, (str, float, int, date, datetime)):
            raise GrammarError("Must return an expression, not a constant value")

        if (
            enforce_aggregation
            and not validator.found_aggregation
            and datatype == "num"
        ):
            return BuilderResponse(
                expression=default_aggregation(expr),
                datatype=datatype,
                is_aggr=validator.found_aggregation,
            )
        else:
            return BuilderResponse(
                expression=expr, datatype=datatype, is_aggr=validator.found_aggregation
            )


#!/usr/bin/env python3
"""
ros2 Image to LaneNet Bridge
"""

import os.path as ops
import sys
import time

from points_vector.msg import PointsVector
from geometry_msgs.msg import Point

import sklearn
import cv2
import numpy as np
import tensorflow as tf

sys.path.append('/home/yvxaiver/lanenet-lane-detection')
from lanenet_model import lanenet
from lanenet_model import lanenet_postprocess
from local_utils.config_utils import parse_config_utils
from local_utils.log_util import init_logger

CFG = parse_config_utils.lanenet_cfg
LOG = init_logger.get_logger(log_file_name_prefix='lanenet_test')

sys.path.append('/home/yvxaiver/LaneNet_to_Trajectory')
from LaneNetToTrajectory import LaneProcessing, DualLanesToTrajectory

class LaneNetImageProcessor():

    def __init__(self, weights_path, image_width, image_height, max_lane_y=420, WARP_RADIUS=20, WP_TO_M_Coeff=[1,1]):
        self.weights_path = weights_path
        self.image_width = image_width
        self.image_height = image_height
        self.lane_processor = LaneProcessing(self.image_width,self.image_height,max_lane_y,WARP_RADIUS,WP_TO_M_Coeff)
        self.calibration = False
        self.full_lane_pts = []
        self.WP_TO_M_Coeff = WP_TO_M_Coeff
        self.following_path = []

    def init_lanenet(self):

        self.input_tensor = tf.placeholder(dtype=tf.float32, shape=[1, 256, 512, 3], name='input_tensor')

        self.net = lanenet.LaneNet(phase='test', cfg=CFG)
        self.binary_seg_ret, self.instance_seg_ret = self.net.inference(input_tensor=self.input_tensor, name='LaneNet')

        self.postprocessor = lanenet_postprocess.LaneNetPostProcessor(cfg=CFG)

        # Set sess configuration
        sess_config = tf.ConfigProto()
        sess_config.gpu_options.per_process_gpu_memory_fraction = CFG.GPU.GPU_MEMORY_FRACTION
        sess_config.gpu_options.allow_growth = CFG.GPU.TF_ALLOW_GROWTH
        sess_config.gpu_options.allocator_type = 'BFC'

        self.sess = tf.Session(config=sess_config)

        # define moving average version of the learned variables for eval
        with tf.variable_scope(name_or_scope='moving_avg'):
            variable_averages = tf.train.ExponentialMovingAverage(
                CFG.SOLVER.MOVING_AVE_DECAY)
            variables_to_restore = variable_averages.variables_to_restore()

        # define saver
        self.saver = tf.train.Saver(variables_to_restore)

        with self.sess.as_default():
            self.saver.restore(sess=self.sess, save_path=self.weights_path)

        return True

    def image_to_trajectory(self, cv_image, lane_fit=True):

        T_start = time.time()

        image_vis = cv_image
        image = cv2.resize(cv_image, (512, 256), interpolation=cv2.INTER_LINEAR)
        image = image / 127.5 - 1.0

        T_resize = time.time()
        LOG.info('Image Resize cost time: {:.5f}s'.format(T_resize-T_start))

        binary_seg_image, instance_seg_image = self.sess.run(
            [self.binary_seg_ret, self.instance_seg_ret],
            feed_dict={self.input_tensor: [image]}
        )

        T_seg_inference = time.time()
        LOG.info('TOTAL Segmentation Inference cost time: {:.5f}s'.format(T_seg_inference-T_resize))

        if not lane_fit:
            out_dict = self.postprocessor.postprocess(
                binary_seg_result=binary_seg_image[0],
                instance_seg_result=instance_seg_image[0],
                source_image=image_vis,
                with_lane_fit=lane_fit,
                data_source='tusimple'
            )
            return out_dict

        full_lane_pts = self.postprocessor.postprocess_lanepts(
            binary_seg_result=binary_seg_image[0],
            instance_seg_result=instance_seg_image[0],
            source_image=image_vis,
            data_source='tusimple'
        )
        self.full_lane_pts = full_lane_pts

        self.lane_processor.process_next_lane(full_lane_pts)
        full_lane_pts = self.lane_processor.get_full_lane_pts()
        physical_fullpts = self.lane_processor.get_physical_fullpts()

        if self.calibration:
            print(self.lane_processor.WARP_RADIUS)
            print(self.lane_processor.get_wp_to_m_coeff())
            self.calibration = False
            raise SystemExit

        phy_centerpts = []
        phy_splines = []
        closest_lane_dist = float('inf')
        closest_lane_idx = 0
        if physical_fullpts:
            #print(physical_fullpts)
            for i in range(len(physical_fullpts)):
                if not i: continue
                traj = DualLanesToTrajectory(physical_fullpts[i-1],physical_fullpts[i],N_centerpts=20)
                phy_centerpts.append(traj.get_centerpoints())
                phy_splines.append(traj.get_spline())
            min_center_y_val = float('inf')
            #for lane in phy_centerpts:
                #if min(lane[1]) < min_center_y_val: min_center_y_val = min(lane[1])
            for i in range(len(phy_splines)):
                new_dist = abs(phy_splines[i](0.2)-(self.image_width*self.WP_TO_M_Coeff[0])/2)
                if new_dist < closest_lane_dist:
                    closest_lane_dist = new_dist
                    closest_lane_idx = i
            if phy_centerpts: self.following_path = phy_centerpts[closest_lane_idx]

        # For display output
        centerpts = []
        if full_lane_pts:
            for i in range(len(full_lane_pts)):
                if not i: continue
                traj = DualLanesToTrajectory(full_lane_pts[i-1],full_lane_pts[i],N_centerpts=20)
                centerpts.append(traj.get_centerpoints())

        T_post_process = time.time()
        LOG.info('Image Post-Process cost time: {:.5f}s'.format(T_post_process-T_seg_inference))

        if centerpts: return full_lane_pts, centerpts, self.following_path
        return None, None, None

    def get_point_vector_path(self):
        #print(self.following_path)
        if self.following_path and self.full_lane_pts:
            vector = []
            for i in range(len(self.following_path[0])):
                pt = Point()
                pt.x = self.following_path[0][i]
                pt.y = self.following_path[1][i]
                pt.z = 0.0
                vector.append(pt)
            ptv = PointsVector()
            ptv.points = vector
            ptv.x_coeff = float(self.lane_processor.get_wp_to_m_coeff()[0])
            return ptv
        return None


""" Unit tests for surface simulations

"""

import logging
import unittest

import astropy.units as u
import numpy
from astropy.coordinates import SkyCoord

from rascil.data_models.memory_data_models import Skycomponent
from rascil.data_models.polarisation import PolarisationFrame
from rascil.processing_components.imaging.primary_beams import (
    create_vp_generic_numeric,
    create_vp,
)
from rascil.processing_components.simulation import create_named_configuration
from rascil.processing_components.simulation.surface import (
    simulate_gaintable_from_zernikes,
)
from rascil.processing_components.visibility.base import create_blockvisibility
from rascil.processing_components import create_image

log = logging.getLogger("rascil-logger")

log.setLevel(logging.WARNING)

class TestSurface(unittest.TestCase):
    def setUp(self):

        from numpy.random import default_rng

        self.rng = default_rng(1805550721)

        from rascil.data_models.parameters import rascil_path, rascil_data_path

        self.doplot = True

        self.midcore = create_named_configuration("MID", rmax=100.0)
        self.nants = len(self.midcore.names)
        self.results_dir = rascil_path("test_results")
        self.ntimes = 100
        interval = 10.0
        self.times = numpy.arange(0.0, float(self.ntimes)) * interval
        self.times *= numpy.pi / 43200.0

        self.frequency = numpy.array([1.4e9])
        self.channel_bandwidth = numpy.array([1e7])
        self.phasecentre = SkyCoord(
            ra=+15.0 * u.deg, dec=-45.0 * u.deg, frame="icrs", equinox="J2000"
        )
        self.vis = create_blockvisibility(
            self.midcore,
            self.times,
            self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre,
            weight=1.0,
            polarisation_frame=PolarisationFrame("stokesI"),
        )
        self.vis["vis"].data *= 0.0

        # Create model
        self.model = create_image(
            npixel=512,
            cellsize=0.001,
            polarisation_frame=PolarisationFrame("stokesI"),
            frequency=self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre,
        )

    def test_simulate_gaintable_from_voltage_patterns(self):
        numpy.random.seed(18051955)
        offset_phasecentre = SkyCoord(
            ra=+15.0 * u.deg, dec=-44.58 * u.deg, frame="icrs", equinox="J2000"
        )
        component = [
            Skycomponent(
                frequency=self.frequency,
                direction=offset_phasecentre,
                polarisation_frame=PolarisationFrame("stokesI"),
                flux=[[1.0]],
            )
        ]

        key_nolls = [3, 5, 6, 7]
        vp_list = list()
        vp_list.append(
            create_vp_generic_numeric(
                self.model,
                pointingcentre=None,
                diameter=15.0,
                blockage=0.0,
                taper="gaussian",
                edge=0.03162278,
                padding=2,
                use_local=True,
            )
        )
        vp_coeffs = numpy.ones([self.nants, len(key_nolls) + 1])
        for inoll, noll in enumerate(key_nolls):
            zernike = {"coeff": 1.0, "noll": noll}
            vp_coeffs[:, inoll + 1] = self.rng.normal(0.0, 0.03, self.nants)
            vp_list.append(
                create_vp_generic_numeric(
                    self.model,
                    pointingcentre=None,
                    diameter=15.0,
                    blockage=0.0,
                    taper="gaussian",
                    edge=0.03162278,
                    zernikes=[zernike],
                    padding=2,
                    use_local=True,
                )
            )

        for vp in vp_list:
            assert (
                vp.image_acc.wcs.wcs.ctype[0] == "AZELGEO long"
            ), vp.image_acc.wcs.wcs.ctype[0]
        gt = simulate_gaintable_from_zernikes(self.vis, component, vp_list, vp_coeffs)

        import matplotlib.pyplot as plt

        assert gt[0].gain.shape == (self.ntimes, self.nants, 1, 1, 1), gt[0].gain.shape

        plt.clf()
        for ant in range(self.nants):
            plt.plot(gt[0].time, 1.0 / numpy.real(gt[0].gain[:, ant, 0, 0, 0]), ".")
        plt.xlabel("Time (s)")
        plt.ylabel("Gain")
        plt.show(block=False)

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


# python3
# rank_from_stream.py - Ranks integers within a stream based on how many integers are less than that specific integer.

import unittest

class IntWithRank:
    """
    Integer with a rank attribute based on how many integers are less than or equal to it.
    """
    def __init__(self, n):
        self.n = n
        self.rank = 0

class RankFromStream:
    """
    Ranks integers within a stream based on how many integers are less than that specific integer.
    """
    def __init__(self):
        self.stream = []

    def _track(self, new, old):
        """
        Tracks the rank of both the new integer coming in
        and the old integers (in the stream) that it is compared to
        """
        if new.n < old.n:
            old.rank += 1
        elif new.n > old.n:
            new.rank += 1
        elif new.n == old.n:
            old.rank += 1
            new.rank += 1

    def is_empty(self):
        return len(self.stream) == 0

    def add_num(self, x):
        """
        Adds new integers to the stream
        """
        new_num = IntWithRank(x)
        if self.is_empty() == True:
            self.stream.append(new_num)
        else:
            for old_num in self.stream:
                self._track(new_num, old_num)
            self.stream.append(new_num)
        return new_num

    def get_rank_of_number(self, x):
        """
        Returns the rank of an inputted integer.
        Returns the first integer that matches the inputted integer
        (The rank will be identical for all integer of the same integer value).
        """
        for num in self.stream:
            if num.n == x:
                return num.rank
        return None

def example():

    stream = [5, 1, 4, 4, 5, 9, 7, 13, 3]

    stream_rank = RankFromStream()
    for num in stream:
        stream_rank.add_num(num)
        print(stream_rank.get_rank_of_number(5))

    print(stream_rank.get_rank_of_number(1))
    print(stream_rank.get_rank_of_number(4))
    print(stream_rank.get_rank_of_number(7))
    print(stream_rank.get_rank_of_number(13))

class Test(unittest.TestCase):

    stream = [5, 1, 4, 4, 5, 9, 7, 13, 3]

    def generate(self):
        stream_rank = RankFromStream()
        for num in self.stream:
            stream_rank.add_num(num)
        return stream_rank

    tests = [
        (5, 5),
        (1, 0),
        (4, 3),
        (9, 7),
        (7, 6),
        (13, 8),
        (3, 1),
        (15, None),
    ]
    def test_rank_from_stream(self):
        stream = self.generate()
        for x, expected in self.tests:
            actual = stream.get_rank_of_number(x)
            assert actual == expected

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


import pandas as pd
from PortCom import *
import numpy as np
import urllib
import pygame,os
import time

def SeuilX(Val, Seuil):
    if Val>=0:
        return max(Val-Seuil,0)
    else:
        return min(Val+Seuil,0)

class monitoring(object):
    def __init__(self, ListVar):
        self.NbVal = len(ListVar)
        self.ListVar=ListVar
        self.iVar=0
        self.indexdataframe=0
        self.datas = pd.DataFrame(index=[0])
        for VarName in ListVar:
            self.datas[VarName+"tps"]=0
            self.datas[VarName+"val"]=0

    def getValues(self, ser,nbTest=5):
        self.datas[self.ListVar[self.iVar]+"tps"][self.indexdataframe]= time.clock()
        self.datas[self.ListVar[self.iVar]+"val"][self.indexdataframe]= readFloat(ser,self.ListVar[self.iVar]+"?",nbTest=nbTest)
        self.iVar += 1
        if self.iVar >= self.NbVal:
            self.iVar = 0
            self.indexdataframe += 1

    def plot(self,x,y,linewidth=2.5, linestyle="-", marker='', RangeX=None, RangeY=None):
        plt.plot(self.datas[x], self.datas[y], linewidth=linewidth, linestyle=linestyle, marker=marker)
        if (RangeX != None):
            Xmax=max(self.datas[x])
            plt.axis([Xmax-RangeX,Xmax, None, None])
        if (RangeY != None):
            Ymax=max(self.datas[y])
            plt.axis([None, None, Ymax-RangeY, Ymax])

    def scatter(self,x,y, s=20, c=u'b', marker=u'o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None,
        linewidths=None, verts=None, hold=None, RangeX=None, RangeY=None):
        plt.scatter(self.datas[x], self.datas[y], s=s, c=c, marker=marker, cmap=cmap, norm=norm,
            vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths, verts=verts, hold=hold)
        if (RangeX != None):
            Xmax=max(self.datas[x])
            plt.axis([Xmax-RangeX,Xmax, None, None])
        if (RangeY != None):
            Ymax=max(self.datas[y])
            plt.axis([None, None, Ymax-RangeY, Ymax])

class joystick_XBox_class(object):
    def __init__(self, VtsMax=500.0, VtsDeadzone=0.07, VtsOffSet=0.05, VtsAcc=30.0, RotMax=500.0, RotDeadzone =0.07, RotOffSet=0.05, RotAcc=30.0):
        pygame.init()
        nb_joysticks = pygame.joystick.get_count()
        disable=0
        #Bouton pour limiter la vitesse du pointeur
        LIMITER=7
        self.mon_joystick = pygame.joystick.Joystick(0)
        self.mon_joystick.init()
        self.jname= self.mon_joystick.get_name()

        #On compte les boutons
        self.nb_boutons = self.mon_joystick.get_numbuttons()

        # Init de la memoire pour les etats des bouton
        self.membstate=map(self.mon_joystick.get_button,range(self.nb_boutons))

        # Init de la memoire pour l' etats de la croix
        self.memhat=[False,False]
        self.hat=[False,False]

        #etat initial des combinaison de touche
        self.bstate_int=0

        self.VtsDeadzone=VtsDeadzone
        self.VtsOffSet=VtsOffSet
        self.VtsMax = VtsMax
        self.RotDeadzone =RotDeadzone
        self.RotOffSet=RotOffSet
        self.RotMax = RotMax
        self.RotAcc = RotAcc
        self.VtsAcc = VtsAcc

        self.VtsD = 0
        self.VtsA = 0

        self.axe = [0.0,0.0]

    def getState(self):
        ev=pygame.event.get()
        #recuperation de l'etat des boutons
        bstate=map(self.mon_joystick.get_button,range(self.nb_boutons))
        # Permet de verifier si un changement d'etat a eu lieu entre 2 boucles
        bstate_ev=map(lambda c,m:c-m,bstate,self.membstate)
        # Conversion de bstate en decimal pour detecter les conbinaion de touches
        self.bstate_int=sum(map(lambda b:int(self.mon_joystick.get_button(b))*(2**b),range(self.nb_boutons)))
        # Gestion du pointeur avec les analogiques
        self.JoyRotRob= - SeuilX(-self.mon_joystick.get_axis(0) + self.RotOffSet, self.RotDeadzone )*self.RotMax*(1-self.RotDeadzone )
        self.JoyVtsRob= - SeuilX(self.mon_joystick.get_axis(2) + self.VtsOffSet, self.VtsDeadzone)*self.VtsMax*(1-self.VtsDeadzone)

        # Gestion des touche HAUT,BAS,GAUCHE,DROITE du clavier avec
        # la croix de la manette
        #nb_hats=self.mon_joystick.get_numhats()
        #if nb_hats>0:
        #    self.hat=self.mon_joystick.get_hat(0)
        #    hat_ev=map(lambda hc,hm:hc!=hm,self.hat,self.memhat)

        self.membstate=bstate
        self.memhat=self.hat

        return [self.JoyVtsRob, self.JoyRotRob]
    def getVtsMString(self):
        self.getState()

        if (self.VtsD + self.VtsAcc) < self.JoyVtsRob:
            self.VtsD += self.VtsAcc
        elif (self.VtsD - self.VtsAcc) > self.JoyVtsRob:
            self.VtsD -= self.VtsAcc
        else :
            self.VtsD = self.JoyVtsRob

        if (self.VtsA + self.RotAcc) < self.JoyRotRob:
            self.VtsA += self.RotAcc
        elif (self.VtsD - self.VtsAcc) > self.JoyVtsRob:
            self.VtsA -= self.RotAcc
        else :
            self.VtsA = self.JoyRotRob

        VtsM1 = int(self.VtsD - self.VtsA/2)
        VtsM2 = int(self.VtsD + self.VtsA/2)
        return "VtsM\t%s\t%s \n" % (VtsM1, VtsM2)


import logging
from urllib.parse import urljoin

from cdpcli.exceptions import WorkloadServiceDiscoveryError

LOG = logging.getLogger('cdpcli.extensions.workload')

def register(operation_callers, operation_model):
    """
    Register an extension to run before or after the CLI command.
    To replace the original CLI caller:
    * operation_callers.insert(0, ReplacementCaller())
    * return False by the ReplacementCaller.invoke(...)
    """
    operation_callers.insert(0, WorkloadServiceDiscovery())

def set_workload_access_token(iam_client,
                              parsed_globals,
                              workload_name,
                              environment_crn):
    """
    Set global Access Token and Endpoint URL for Workload and Environment CRN
    """
    req_params = {
        'workloadName': workload_name,
        'environmentCrn': environment_crn
    }
    response = iam_client.generate_workload_auth_token(**req_params)

    workload_url = response.get('endpointUrl', None)
    workload_access_token = response.get('token', None)
    LOG.debug('Workload service-discovery succeeded. '
              'endpoint_url=%s, access_token=%s...',
              workload_url,
              workload_access_token[0:16] if workload_access_token else None)

    # remove path from the workload URL
    if workload_url is not None:
        workload_url = urljoin(workload_url, '/')
    # add 'Bearer' prefix to access token
    if workload_access_token is not None and \
       not workload_access_token.startswith('Bearer '):
        workload_access_token = 'Bearer ' + workload_access_token

    # Set the workload URL and access-token in parsed_globals, so the
    # next CLI operation caller will use them to make the connection to workload.
    parsed_globals.endpoint_url = workload_url
    parsed_globals.access_token = workload_access_token

class WorkloadServiceDiscovery(object):
    def invoke(self,
               client_creator,
               operation_model,
               parameters,
               parsed_args,
               parsed_globals):
        if parsed_globals.access_token:
            # access-token was explicitly set by user,
            # skip the workload service-discovery
            LOG.debug('Skip workload service-discovery')
            return True

        LOG.debug('Run workload service-discovery')
        service_name = operation_model.service_model.service_name
        if not service_name:
            raise WorkloadServiceDiscoveryError(err_msg='Missing service name')

        if service_name == 'dfworkload':
            workload_name = 'DF'
            environment_crn = parameters.get('environmentCrn', None)
            if not environment_crn:
                raise WorkloadServiceDiscoveryError(err_msg='Missing environment CRN')
        else:
            raise WorkloadServiceDiscoveryError(
                err_msg='Unknown service name \'%s\'' % service_name)

        iam_client = client_creator('iam')
        set_workload_access_token(iam_client, parsed_globals, workload_name,
                                  environment_crn)

        # The command processing is not finished, continue with other operation callers.
        return True


# %%
import os
import torch

import random
import shutil

import numpy as np
import torch
import matplotlib.pyplot as plt
import torchvision.transforms.functional as F
# %%
def del_folder(path, *args):
    '''
    delete the folder which path/version

    Args:
        path (str): path
        version (str): version
    '''
    if os.path.exists(os.path.join(path, *args)):
        shutil.rmtree(os.path.join(path, *args))

def make_folder(path, *args):
    '''
    make folder which path/version

    Args:
        path (str): path
        version (str): version
    '''
    if not os.path.exists(os.path.join(path, *args)):
        os.makedirs(os.path.join(path, *args))
        print("success make dir! where: %s " % os.path.join(path, *args))
    else:
        print("The target path already exists! where: %s " % os.path.join(path, *args))

def tensor2var(x, grad=False):
    '''
    put tensor to gpu, and set grad to false

    Args:
        x (tensor): input tensor
        grad (bool, optional):  Defaults to False.

    Returns:
        tensor: tensor in gpu and set grad to false
    '''
    if torch.cuda.is_available():
        x = x.cuda()
        x.requires_grad_(grad)
    return x

def var2tensor(x):
    '''
    put date to cpu

    Args:
        x (tensor): input tensor

    Returns:
        tensor: put data to cpu
    '''
    return x.data.cpu()

def var2numpy(x):
    return x.data.cpu().numpy()

def get_ckpt_path(config):
    '''
    get the finall train ckpt, for the pretrain model, or some task.

    Args:
        config (parser): parameters of the training

    Returns:
        string: final ckpt file path
    '''

    ckpt_path = os.path.join('/workspace/Walk_Video_PyTorch', 'logs', config.model, config.version, 'checkpoints')
    file_name = os.listdir(ckpt_path)[0]

    ckpt_file_path = os.path.join(ckpt_path, file_name)

    return ckpt_file_path

def plot(imgs, **imshow_kwargs):
    '''
    receive a tensor, and plot the figure

    Args:
        imgs (tensor): a batch of tensor
    '''

    if not isinstance(imgs[0], list):
        # Make a 2d grid even if there's just 1 row
        imgs = [imgs]

    num_rows = len(imgs)
    num_cols = len(imgs[0])

    _, axs = plt.subplots(nrows=num_rows, ncols=num_cols, squeeze=False)
    for row_idx, row in enumerate(imgs):
        for col_idx, img in enumerate(row):
            ax = axs[row_idx, col_idx]
            img = F.to_pil_image(img.to("cpu"))
            ax.imshow(np.asarray(img), **imshow_kwargs)
            ax.set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])

    plt.tight_layout()


import calendar
from functools import reduce

from dateutil.relativedelta import relativedelta

from odoo import _, api, exceptions, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools.float_utils import float_is_zero, float_round

class AccountPaymentTermHoliday(models.Model):
    _name = "account.payment.term.holiday"
    _description = "Payment Term Holidays"

    payment_id = fields.Many2one(comodel_name="account.payment.term")
    holiday = fields.Date(required=True)
    date_postponed = fields.Date(string="Postponed date", required=True)

    @api.constrains("holiday", "date_postponed")
    def check_holiday(self):
        for record in self:
            if fields.Date.from_string(
                record.date_postponed
            ) <= fields.Date.from_string(record.holiday):
                raise ValidationError(
                    _("Holiday %s can only be postponed into the future")
                    % record.holiday
                )
            if (
                record.search_count(
                    [
                        ("payment_id", "=", record.payment_id.id),
                        ("holiday", "=", record.holiday),
                    ]
                )
                > 1
            ):
                raise ValidationError(
                    _("Holiday %s is duplicated in current payment term")
                    % record.holiday
                )
            if (
                record.search_count(
                    [
                        ("payment_id", "=", record.payment_id.id),
                        "|",
                        ("date_postponed", "=", record.holiday),
                        ("holiday", "=", record.date_postponed),
                    ]
                )
                >= 1
            ):
                raise ValidationError(
                    _("Date %s cannot is both a holiday and a Postponed date")
                    % record.holiday
                )

class AccountPaymentTermLine(models.Model):
    _inherit = "account.payment.term.line"

    amount_round = fields.Float(
        string="Amount Rounding",
        digits="Account",
        help="Sets the amount so that it is a multiple of this value.",
    )
    months = fields.Integer(string="Number of Months")
    weeks = fields.Integer(string="Number of Weeks")
    value = fields.Selection(
        selection_add=[
            ("percent_amount_untaxed", "Percent (Untaxed amount)"),
            ("fixed",),
        ],
        ondelete={"percent_amount_untaxed": lambda r: r.write({"value": "percent"})},
    )

    @api.constrains("value", "value_amount")
    def _check_value_amount_untaxed(self):
        for term_line in self:
            if (
                term_line.value == "percent_amount_untaxed"
                and not 0 <= term_line.value_amount <= 100
            ):
                raise ValidationError(
                    _(
                        "Percentages on the Payment Terms lines "
                        "must be between 0 and 100."
                    )
                )

    def compute_line_amount(self, total_amount, remaining_amount, precision_digits):
        """Compute the amount for a payment term line.
        In case of procent computation, use the payment
        term line rounding if defined

            :param total_amount: total balance to pay
            :param remaining_amount: total amount minus sum of previous lines
                computed amount
            :returns: computed amount for this line
        """
        self.ensure_one()
        if self.value == "fixed":
            return float_round(self.value_amount, precision_digits=precision_digits)
        elif self.value in ("percent", "percent_amount_untaxed"):
            amt = total_amount * self.value_amount / 100.0
            if self.amount_round:
                amt = float_round(amt, precision_rounding=self.amount_round)
            return float_round(amt, precision_digits=precision_digits)
        elif self.value == "balance":
            return float_round(remaining_amount, precision_digits=precision_digits)
        return None

    def _decode_payment_days(self, days_char):
        # Admit space, dash and comma as separators
        days_char = days_char.replace(" ", "-").replace(",", "-")
        days_char = [x.strip() for x in days_char.split("-") if x]
        days = [int(x) for x in days_char]
        days.sort()
        return days

    @api.constrains("payment_days")
    def _check_payment_days(self):
        for record in self:
            if not record.payment_days:
                continue
            try:
                payment_days = record._decode_payment_days(record.payment_days)
                error = any(day <= 0 or day > 31 for day in payment_days)
            except Exception:
                error = True
            if error:
                raise exceptions.Warning(_("Payment days field format is not valid."))

    payment_days = fields.Char(
        string="Payment day(s)",
        help="Put here the day or days when the partner makes the payment. "
        "Separate each possible payment day with dashes (-), commas (,) "
        "or spaces ( ).",
    )

class AccountPaymentTerm(models.Model):
    _inherit = "account.payment.term"

    sequential_lines = fields.Boolean(
        string="Sequential lines",
        default=False,
        help="Allows to apply a chronological order on lines.",
    )
    holiday_ids = fields.One2many(
        string="Holidays",
        comodel_name="account.payment.term.holiday",
        inverse_name="payment_id",
    )

    def apply_holidays(self, date):
        holiday = self.holiday_ids.search(
            [("payment_id", "=", self.id), ("holiday", "=", date)]
        )
        if holiday:
            return fields.Date.from_string(holiday.date_postponed)
        return date

    def apply_payment_days(self, line, date):
        """Calculate the new date with days of payments"""
        if line.payment_days:
            payment_days = line._decode_payment_days(line.payment_days)
            if payment_days:
                new_date = None
                payment_days.sort()
                days_in_month = calendar.monthrange(date.year, date.month)[1]
                for day in payment_days:
                    if date.day <= day:
                        if day > days_in_month:
                            day = days_in_month
                        new_date = date + relativedelta(day=day)
                        break
                if not new_date:
                    day = payment_days[0]
                    if day > days_in_month:
                        day = days_in_month
                    new_date = date + relativedelta(day=day, months=1)
                return new_date
        return date

    def compute(self, value, date_ref=False, currency=None):
        """Complete overwrite of compute method for adding extra options."""
        # FIXME: Find an inheritable way of doing this
        self.ensure_one()
        last_account_move = self.env.context.get("last_account_move", False)
        date_ref = date_ref or fields.Date.today()
        amount = value
        result = []
        if not currency:
            if self.env.context.get("currency_id"):
                currency = self.env["res.currency"].browse(
                    self.env.context["currency_id"]
                )
            else:
                currency = self.env.company.currency_id
        precision_digits = currency.decimal_places
        next_date = fields.Date.from_string(date_ref)
        for line in self.line_ids:
            if line.value == "percent_amount_untaxed" and last_account_move:
                if last_account_move.company_id.currency_id == currency:
                    amount_untaxed = -last_account_move.amount_untaxed_signed
                else:
                    raise UserError(
                        _(
                            "Percentage of amount untaxed can't be used with foreign "
                            "currencies"
                        )
                    )
                amt = line.compute_line_amount(amount_untaxed, amount, precision_digits)
            else:
                amt = line.compute_line_amount(value, amount, precision_digits)
            if not self.sequential_lines:
                # For all lines, the beginning date is `date_ref`
                next_date = fields.Date.from_string(date_ref)
                if float_is_zero(amt, precision_digits=precision_digits):
                    continue
            if line.option == "day_after_invoice_date":
                next_date += relativedelta(
                    days=line.days, weeks=line.weeks, months=line.months
                )
            elif line.option == "day_following_month":
                # Getting last day of next month
                next_date += relativedelta(day=line.days, months=1)
            elif line.option == "day_current_month":
                # Getting last day of next month
                next_date += relativedelta(day=line.days, months=0)
            # From Odoo
            elif line.option == "after_invoice_month":
                # Getting 1st of next month
                next_first_date = next_date + relativedelta(day=1, months=1)
                # Then add days
                next_date = next_first_date + relativedelta(
                    days=line.days - 1, weeks=line.weeks, months=line.months
                )
            next_date = self.apply_payment_days(line, next_date)
            next_date = self.apply_holidays(next_date)
            if not float_is_zero(amt, precision_digits=precision_digits):
                result.append((fields.Date.to_string(next_date), amt))
                amount -= amt
        amount = reduce(lambda x, y: x + y[1], result, 0.0)
        dist = round(value - amount, precision_digits)
        if dist:
            last_date = result and result[-1][0] or fields.Date.today()
            result.append((last_date, dist))
        return result


# imports
from os.path import join
from datetime import date, datetime
from argparse import ArgumentParser
import subprocess

# third party imports
import numpy as np
import torch
from PIL import Image

# project imports
from database.Allen import data_loader_subset as DL_Allen_subset
from database import databaseConfig

####################################
######## GLOBAL  PARAMETERS ########
####################################
NIFTY_REG_DIR = '/home/acasamitjana/Software_MI/niftyreg-git/build/'
ALADINcmd = NIFTY_REG_DIR + 'reg-apps' + '/reg_aladin'
REScmd = NIFTY_REG_DIR + 'reg-apps' + '/reg_resample'

MRI_PROCESSING = False
LABELS_PROCESSING = False
###################################
########### DATA LOADER ###########
###################################
if MRI_PROCESSING:
    data_loader = DL_Allen_subset.DataLoader(databaseConfig.ALLEN_subset)
    MASK_MRI = False
    COMPUTE_CROP_COORDINATES = False
    mx = 0
    Mx = 448
    my = 0
    My = 320
    for rid, subject in data_loader.subject_dict.items():
        # 1 Mask MRI
        if MASK_MRI:
            MM = subject.load_mri_mask()
            MI = subject.load_mri()
            MI[MM == 0] = 0
            img = Image.fromarray(MI, mode='L')
            img.save(subject.image_mri)

        # 2 Compute cropping coordinates
        if COMPUTE_CROP_COORDINATES:
            MM = subject.load_mri_mask()
            idx = np.where(MM > 0)
            mx = min(mx, np.min(idx[0]))
            Mx = max(Mx, np.max(idx[0]))
            my = min(my, np.min(idx[1]))
            My = max(My, np.max(idx[1]))

        # 3 Crop
        MI = subject.load_mri()
        MM = Image.open(subject.labels_mri)
        MM = np.array(MM)
        HI = subject.load_histo_affine()
        HM = Image.open(subject.labels_histo_affine)
        HM = np.array(HM)

        MMc = MM[mx:Mx, my:My]
        MIc = MI[mx:Mx, my:My]
        HMc = HM[mx:Mx, my:My]
        HIc = HI[mx:Mx, my:My]
        img = Image.fromarray(MIc, mode='L')
        img.save(subject.image_mri)
        img = Image.fromarray(MMc, mode='RGB')
        img.save(subject.labels_mri)
        img = Image.fromarray(HIc, mode='L')
        img.save(subject.image_histo_affine)
        img = Image.fromarray(HMc, mode='RGB')
        img.save(subject.labels_histo_affine)

    print('Done.')

#################
# WM Cerebellum #
#################
import pdb
if LABELS_PROCESSING:
    data_loader = DL_Allen_subset.DataLoader(databaseConfig.ALLEN_subset_cll)
    data_loader_all = DL_Allen_subset.DataLoader(databaseConfig.ALLEN_subset)

    for rid, subject in data_loader.subject_dict.items():
        subject_full = data_loader_all.subject_dict[rid.split('.')[0]]

        # 1. Histo Affine
        labels_cll = subject.load_histo_labels_affine()
        labels = subject_full.load_histo_labels_affine()

        new_labels = np.zeros(labels_cll.shape + (3,), dtype='uint8')
        new_labels[labels == 1, 0] = 255
        new_labels[labels == 2, 1] = 255
        new_labels[labels == 3, 2] = 255

        new_labels[labels_cll == 1, 0] = 255
        new_labels[labels_cll == 2, :] = 255

        img = Image.fromarray(new_labels, mode='RGB')
        img.save(subject_full.labels_histo_affine[:-4] + '_new.png')

        # 2. MRI
        labels_cll = subject.load_mri_labels()
        labels = subject_full.load_mri_labels()

        new_labels = np.zeros(labels_cll.shape + (3,), dtype='uint8')
        new_labels[labels == 1, 0] = 255
        new_labels[labels == 2, 1] = 255
        new_labels[labels == 3, 2] = 255

        new_labels[labels_cll == 1, 0] = 255
        new_labels[labels_cll == 2, :] = 255

        img = Image.fromarray(new_labels, mode='RGB')
        img.save(subject_full.labels_mri[:-4] + '_new.png')


#  By submitting this assignment, I agree to the following:
#  "Aggies do not lie, cheat, or steal, or tolerate those who do"
#  "I have not given or received any unauthorized aid on this assignment"
#
# Name:        Chase Albright
# Section:     480
# Assignment:  Lab Assignment 10b
# Date:        27 October 2020

import csv

# program to open and read the csv file
file = input('Enter the name of the loan csv file you wish to visualize: ')

monthList = []
balanceList = []

#open the file
with open(file,'r') as csvfile:
    #read each line seperated by a comma
    readCSV = csv.reader(csvfile, delimiter=',')
    #loop to run through each and while loop to limit to 30 values
    next(readCSV) # skip the label field
    for row in readCSV:
        monthList.append(row[0])
        balanceList.append(row[1])
#print(monthList)
#print(balanceList)

# convert list vals into floats
monthNum = []
for numM in monthList:
    x = float(numM)
    monthNum.append(x)

balNum = []
for numB in balanceList:
    y = float(numB)
    balNum.append(y)

#print(monthNum)
#print(balNum)

#limits length to 30 points if longer than 30
if len(monthNum) >= 30:
    del monthNum[29:]
    del balNum[29:]

# here we begin the graphing
import matplotlib.pyplot as plt

plt.scatter(monthNum,balNum,color='k',label='Balance')
plt.plot(monthNum,balNum,'--k')
plt.xlabel('Months')
plt.ylabel('Principal Balance')
plt.title('Balance vs. Time in Months')
plt.legend()
plt.show()


import spgl
import math
import random

# Classes
class MissileCommand(spgl.Game):
    def __init__(self, screen_width, screen_height, background_colour, title, splash_time):
        spgl.Game.__init__(self, screen_width, screen_height, background_colour, title, splash_time)
        self.level = 1
        self.score = 0

    def click(self, x, y):
        closest_missile = None
        closest_missile_distance = 100000
        for player_missile in player_missiles:
            if player_missile.state == 'ready':
                a = player_missile.xcor()-x
                b = player_missile.ycor()-y
                distance = math.sqrt((a**2) + (b**2))
                if distance < closest_missile_distance:
                    closest_missile = player_missile
                    closest_missile_distance = distance
        if closest_missile:
            closest_missile.set_target(x, y)

class City(spgl.Sprite):
    def __init__(self, shape, color, x, y):
        spgl.Sprite.__init__(self, shape, color, x, y)
        self.name = 'City'

    def destroy(self):
        self.clear()
        self.penup()
        self.setposition(2000, 2000)
        self.state = None
        cities.remove(self)

    def tick(self):
        pass

class Silo(spgl.Sprite):
    def __init__(self, shape, color, x, y):
        spgl.Sprite.__init__(self, shape, color, x, y)
        self.name = 'Silo'

    def destroy(self):
        self.clear()
        self.penup()
        self.setposition(2000, 2000)
        self.state = None
        silos.remove(self)

    def tick(self):
        pass

class PlayerMissile(spgl.Sprite):
    def __init__(self, shape, color, x, y):
        spgl.Sprite.__init__(self, shape, color, x, y)
        self.speed = 7
        self.state = 'ready'
        self.target_x = 0
        self.target_y = 0
        self.shapesize(0.2, 0.2, 0)
        self.size = 0.2
        self.frame = 0.0
        self.name = 'Player'

    def set_target(self, target_x, target_y):
        if self.state == 'ready':
            self.target_x = target_x
            self.target_y = target_y

            self.dx = self.xcor() - target_x
            # avoid dive by 0 logic error
            if self.dx == 0:
                self.dx = 0.01
            self.dy = self.ycor() - target_y
            self.m = self.dy / self.dx

            self.state = 'launched'

    def explode(self):
        self.frame += 1.0
        if self.frame < 30.0:
            self.size = self.frame / 10
            self.shapesize(self.size, self.size, 0)
        elif self.frame < 55:
            self.size = (60 - self.frame) / 10
            self.shapesize(self.size, self.size, 0)
        else:
            self.destroy()

    def destroy(self):
        self.clear()
        self.penup()
        self.setposition(2000, 2000)
        self.state = None
        player_missiles.remove(self)

    def tick(self):
        if self.state == 'launched':
            self.pendown()
            self.setx(self.xcor() + (1 / self.m) * self.speed)
            self.sety(self.ycor() + self.speed)

            # check if missile reached target
            a = self.xcor()-self.target_x
            b = self.ycor()-self.target_y
            distance = math.sqrt((a**2) + (b**2))

            if distance < 5:
                self.state = 'explode'

        if self.state == 'explode':
            self.explode()

class EnemyMissile(spgl.Sprite):
    def __init__(self, shape, color, x, y):
        spgl.Sprite.__init__(self, shape, color, x, y)
        self.dx = 0
        self.dy = 0
        self.speed = 4
        self.size = 0.2
        self.shapesize(self.size, self.size, 0)
        self.pendown()
        self.state = 'ready'
        self.target_x = 0
        self.target_y = 0
        self.frame = 2
        self.name = 'Enemy'

    def set_target(self, target):
        self.target_x = target.xcor()
        self.target_y = target.ycor()
        # avoid dive by 0 logic error
        self.dx = self.xcor() - target.xcor()
        if self.dx == 0:
            self.dx = 0.01
        self.dy = self.ycor() - target.ycor()
        self.m = self.dy / self.dx
        self.state = 'launched'

    def explode(self):
        self.frame += 1.0
        if self.frame < 30.0:
            self.size = self.frame / 10
            self.shapesize(self.size, self.size, 0)
        elif self.frame < 55:
            self.size = (60 - self.frame) / 10
            self.shapesize(self.size, self.size, 0)
        else:
            self.destroy()

    def destroy(self):
        self.clear()
        self.penup()
        self.setposition(2000, 2000)
        self.state = None
        enemy_missiles.remove(self)

    def tick(self):
        if self.state == 'launched':
            self.setx(self.xcor() - (1/self.m) * self.speed)
            self.sety(self.ycor() - self.speed)

            # check if missile reached target
            a = self.xcor() - self.target_x
            b = self.ycor() - self.target_y
            distance = math.sqrt((a ** 2) + (b ** 2))

            if distance < 10:
                self.state = 'explode'

        if self.state == 'explode':
            self.explode()

# functions
def check_collision(missile, target):
    # check if missile is exploding
    if missile.state == 'explode':
        radius = (missile.size * 20) / 2
        if missile.distance(target) < radius:
            missile.destroy()
            target.destroy()
            if missile.name == 'Player' and target.name == 'Enemy':
                game.score += 10

# Game set up and scoring
# show hide/splash screen with 0, default is 5
game = MissileCommand(800, 600, "black", "Missile Command", 0)

# Sprites
cities = []
silos = []
# active player missiles used at each level
player_missiles = []
# all player missiles
player_missiles_storage = []
# active enemy missiles used at each level
enemy_missiles = []
# all enemy missiles
enemy_missiles_storage = []

for i in range(6):
    cities.append(City("square", "green", -250 + (i * 100), -250))

for i in range(3):
    silos.append(Silo("square", "blue", -350 + (i * 350), -225))

for i in range(30):
    if i < 10:
        x = -350
    elif i < 20:
        x = 0
    else:
        x = 350

    player_missiles_storage.append(PlayerMissile("circle", "white", x, -225))

for player_missile in player_missiles_storage:
    player_missiles.append(player_missile)

for i in range(30):
    enemy_missiles_storage.append(EnemyMissile("circle", "red", random.randint(-450, 450), random.randint(400, 800)))

for enemy_missile in enemy_missiles_storage:
    if len(enemy_missiles) < game.level:
        enemy_missile.set_target(random.choice(cities))
        enemy_missiles.append(enemy_missile)

# scoring
game_status = spgl.Label("Level: {} \nScore: {} \nCities: {}  \nSilos: {} \nPlayer Missiles: {} \nEnemy Missiles: {}",
                         "white", -390, 210)

while True:
    game.tick()

    # check if player missile collides with enemy missile
    for player_missile in player_missiles:
        for enemy_missile in enemy_missiles:
            check_collision(player_missile, enemy_missile)

    # check if enemy missile collides with city or silos
    for enemy_missile in enemy_missiles:
        for city in cities:
            check_collision(enemy_missile, city)

        for silo in silos:
            check_collision(enemy_missile, silo)

    # no more enemy missiles left
    if len(enemy_missiles) < 1:
        # add up score
        city_bonus = 100 * len(cities)
        silos_bonus = 50 * len(silos)
        missile_bonus = 10 * len(player_missiles)

        game.score += (city_bonus + silos_bonus + missile_bonus)
        print("Level {} Complete".format(game.level))
        print("City bonus: {} Silo bonus: {} Missile bonus: {}".format(city_bonus, silos_bonus, missile_bonus))
        game.level += 1

        # reset enemy missiles
        for enemy_missile in enemy_missiles_storage:
            if len(enemy_missiles) < game.level:
                enemy_missile.set_target(random.choice(cities + silos))
                enemy_missiles.append(enemy_missile)

        # reset player missiles
        for player_missile in player_missiles:
            player_missile.destroy()

        player_missiles = []

        for player_missile in player_missiles_storage:
            player_missiles.append(player_missile)

        for i in range(30):
            if i < 10:
                x = -350
            elif i < 20:
                x = 0
            else:
                x = 350

            player_missiles[i].clear()
            player_missiles[i].state = 'ready'
            player_missiles[i].setposition(x, -225)
            player_missiles[i].shapesize(0.2, 0.2, 0)
            player_missiles[i].clear()

    game_status.update("Level: {} \nScore: {} \nCities: {}  \nSilos: {} \nPlayer Missiles: {} \nEnemy Missiles: {}"
                       .format(game.level, game.score, len(cities),
                               len(silos), len(player_missiles), len(enemy_missiles)))


class Solution:
    def splitArraySameAverage(self, nums: List[int]) -> bool:
        n=len(nums)
        ssum=sum(nums)
        if ssum==0:
            return True if n>1 else False
        @lru_cache(None)
        def subset_sum(idx,val,n1):                # This is O(n^2 * sum) ~ approx
            if idx==n:
                return True if (val==0 and n1==0) else False
            res=False
            #take this element
            if val>=nums[idx]:
                res=res or subset_sum(idx+1,val-nums[idx],n1-1)
            #dont take
            res= res or subset_sum(idx+1,val,n1)
            return res
        for i in range(1,n):     #This is O(n)
            if (ssum*i)%n==0 and subset_sum(0,(ssum*i)//n,i):
                return True
        return False

leetcode/scripts/algorithms/S/Split Array With Same Average/Split Array With Same Average.py


import Model.param as param
import torch.nn as nn

class PositionwiseFeedForward(nn.Module):
    def __init__(self):
        super(PositionwiseFeedForward, self).__init__()
        self.ffn = nn.Sequential(
            nn.Linear(param.embed_dim, param.d_ff),
            nn.ReLU(),
            nn.Dropout(param.dropout),
            nn.Linear(param.d_ff, param.embed_dim)
        )

    def forward(self, inputs):
        """
        :param inputs: (batch_size, q_len, embed_dim)的张量
        :return:       (batch_size, q_len, embed_dim)的张量
        """

        return nn.LayerNorm(param.embed_dim)(self.ffn(inputs) + inputs)
        # return nn.LayerNorm(param.embed_dim).cuda()(self.ffn(inputs) + inputs)


from http import HTTPStatus

test_data_for_create_role_successful = [
    (
        {
            "label": "test",
        },
        {'msg': 'Role created!'},
        {'status': HTTPStatus.OK},
    ),
]

test_data_for_create_role_fail = [
    (
        {
            "label": "test2",
        },
        {
            "validation_error": {
                "body_params": [
                    {
                        "ctx": {
                            "enum_values": [
                                "standard",
                                "privileged",
                                "admin",
                                "test"
                            ]
                        },
                        "loc": [
                            "label"
                        ],
                        "msg": "value is not a valid enumeration member; "
                               "permitted: 'standard', 'privileged', 'admin', 'test'",
                        "type": "type_error.enum"
                    }
                ]
            }
        },
        {'status': HTTPStatus.BAD_REQUEST},
    ),
    (
        {
            "label": "admin",
        },
        {
            "msg": "Role exists!"
        },
        {'status': HTTPStatus.CONFLICT},
    ),
    (
        {
            "lael": "test",
        },
        {
            "validation_error": {
                "body_params": [
                    {
                        "loc": [
                            "label"
                        ],
                        "msg": "field required",
                        "type": "value_error.missing"
                    }
                ]
            }
        },
        {'status': HTTPStatus.BAD_REQUEST},
    ),
]

test_data_for_get_scope_roles = [
    (
        ['standard', 'privileged', 'admin', 'test'],
        {'status': HTTPStatus.OK, 'len': 4},
    ),
]

test_data_for_get_single_role = [
    (
        '815aa676-902d-4505-bbb5-42ca9727617f',
        {
            'id': '815aa676-902d-4505-bbb5-42ca9727617f',
            'label': 'standard'
        },
        {'status': HTTPStatus.OK},
    ),
    (
        'c07d6c8c-1d4f-4f54-b88f-2c2b962ad7b6',
        {
            'id': 'c07d6c8c-1d4f-4f54-b88f-2c2b962ad7b6',
            'label': 'admin'
        },
        {'status': HTTPStatus.OK},
    ),
]

test_data_for_get_single_role_fail = [
    (
        'qwe',
        {
            "msg": "Bad format uuid"
        },
        {'status': HTTPStatus.UNPROCESSABLE_ENTITY},
    ),
    (
        '16bdbb16-d3bf-4184-b08e-e2b2eabeee7f',
        {
            'msg': 'Role not found'
        },
        {'status': HTTPStatus.NOT_FOUND},
    ),
]

test_data_for_delete_role_fail = [
    (
        '16bdbb16-d3bf-4184-b08e-e2b2eabeee7f',
        {
            "msg": "Role not found"
        },
        {'status': HTTPStatus.NOT_FOUND},
    ),
    (
        'qwef',
        {
            "msg": "Bad format uuid"
        },
        {'status': HTTPStatus.UNPROCESSABLE_ENTITY},
    ),
]

test_data_for_delete_role = [
    (
        'c07d6c8c-1d4f-4f54-b88f-2c2b962ad7b6',
        {
            "msg": "Role deleted!"
        },
        {'status': HTTPStatus.OK},
    ),
]

test_data_for_role_appoint_successful = [
    (
        '15e794ed-2286-4690-b092-793f5e51f0ec',
        'privileged',
        {
            "msg": "Role changed!"
        },
        {'status': HTTPStatus.OK},
    ),
]

test_data_for_role_appoint_fail = [
    (
        '13f82a7f-49ca-4e29-8731-8fed5e37695f',
        'privileged',
        {
            "msg": "User not found"
        },
        {'status': HTTPStatus.NOT_FOUND},
    ),
    (
        'qwe',
        'privileged',
        {
            "msg": "Bad format uuid"
        },
        {'status': HTTPStatus.UNPROCESSABLE_ENTITY},
    ),
    (
        '15e794ed-2286-4690-b092-793f5e51f0ec',
        'privileg',
        {
            "validation_error": {
                "query_params": [
                    {
                        "ctx": {
                            "enum_values": [
                                "standard",
                                "privileged",
                                "admin",
                                "test"
                            ]
                        },
                        "loc": [
                            "label"
                        ],
                        "msg": "value is not a valid enumeration member; "
                               "permitted: 'standard', 'privileged', 'admin', 'test'",
                        "type": "type_error.enum"
                    }
                ]
            }
        },
        {'status': HTTPStatus.BAD_REQUEST},
    ),
]

test_data_for_role_take_away_successful = [
    (
        '15e794ed-2286-4690-b092-793f5e51f0ec',
        {
            "msg": "Role changed!"
        },
        {'status': HTTPStatus.OK},
    )
]

test_data_for_role_take_away_fail = [
    (
        'qwe',
        {
            "msg": "Bad format uuid"
        },
        {'status': HTTPStatus.UNPROCESSABLE_ENTITY},
    ),
    (
        '13f82a7f-49ca-4e29-8731-8fed5e37695f',
        {
            "msg": "User not found"
        },
        {'status': HTTPStatus.NOT_FOUND},
    )
]


from lib.args import get_hudi_args
from lib.spark import get_spark
from lib.table import calculate_partitions
from lib.constants import CHANGES_METADATA_FIELD_PREFIX, HUDI_METADATA_FIELD_PREFIX

cmd_args = get_hudi_args()
spark = get_spark()

# Load Hudi table
print(f">>> Load Hudi table from {cmd_args.hudi_path}...")
df = spark \
    .read \
    .format("hudi") \
    .load(f"{cmd_args.hudi_path}/*")

# Filter out Qlik and Hudi metadata fields
drop_columns = []
drop_columns.extend([c for c in df.columns if c.startswith(CHANGES_METADATA_FIELD_PREFIX)])
drop_columns.extend([c for c in df.columns if c.startswith(HUDI_METADATA_FIELD_PREFIX)])
df = df.drop(*drop_columns)

# Export to parquet snapshot
partitions = calculate_partitions(spark=spark, df=df)
print(f">>> Storing snapshot: {cmd_args.snapshot_path}, with {partitions} partition(s)...")

df.repartition(partitions) \
    .write \
    .mode("overwrite") \
    .parquet(cmd_args.snapshot_path)

print(">>> Done!")


""" Module specifically to hold version information.  The reason this
exists is the version information is needed in setup.py for install.
If these values were defined in pw/__init__.py then install
would fail because there are other dependencies imported in
pw/__init__.py that are not present until after
install. Do not import anything into this module."""

""" __version__ MUST align with desired lcmap-pyccd version"""
__version__ = '2017.08.29'
__pyccd_version__ = '2017.08.18'
__name = 'lcmap-pyccd-worker'
__algorithm__ = '-'.join([__name, __pyccd_version__])