text:
import numpy as np
import argparse
from scipy import io, spatial, linalg
from sklearn.metrics import confusion_matrix, log_loss, f1_score

parser = argparse.ArgumentParser(description="SAE")

parser.add_argument('-data', '--dataset', help='choose between APY, AWA2, AWA1, CUB, SUN', default='AWA2', type=str)
parser.add_argument('-mode', '--mode', help='train/test, if test set alpha, gamma to best values below', default='train', type=str)
parser.add_argument('-ld1', '--ld1', default=5, help='best value for F-->S during test, lower bound of variation interval during train', type=float)
parser.add_argument('-ld2', '--ld2', default=5, help='best value for S-->F during test, upper bound of variation interval during train', type=float)
parser.add_argument('-acc', '--accuracy', help='choose between top1, top5, logloss, F1, all', default='all', type=str)

"""

Range of Lambda for Validation:

AWA1 -> 2-8 for [F-->S] and 0.4-1.6 for [S-->F]
AWA2 -> 0.1-1.6
CUB  -> 50-5000 for [F-->S] and 0.05-5 for [S-->F]
SUN  -> 0.005-5
APY  -> 0.5-50

Best Value of Lambda found by validation & corr. test accuracies:

AWA1 -> 0.5134 @ 3.0  [F-->S] 0.5989 @ 0.8  [S-->F]
AWA2 -> 0.5166 @ 0.6  [F-->S] 0.6051 @ 0.2  [S-->F]
CUB  -> 0.3948 @ 100  [F-->S] 0.4670 @ 0.2  [S-->F]
SUN  -> 0.5285 @ 0.32 [F-->S] 0.5986 @ 0.16 [S-->F]
APY  -> 0.1607 @ 2.0  [F-->S] 0.1650 @ 4.0  [S-->F]

"""
class SAE():

	def __init__(self, args):

		self.args = args

		data_folder = '../xlsa17/data/'+args.dataset+'/'
		res101 = io.loadmat(data_folder+'res101.mat')
		att_splits=io.loadmat(data_folder+'att_splits.mat')

		train_loc = 'train_loc'
		val_loc = 'val_loc'
		test_loc = 'test_unseen_loc'

		feat = res101['features']
		self.X_train = feat[:, np.squeeze(att_splits[train_loc]-1)]
		self.X_val = feat[:, np.squeeze(att_splits[val_loc]-1)]
		self.X_test = feat[:, np.squeeze(att_splits[test_loc]-1)]

		print('Tr:{}; Val:{}; Ts:{}\n'.format(self.X_train.shape[1], self.X_val.shape[1], self.X_test.shape[1]))

		labels = res101['labels']
		self.labels_train = labels[np.squeeze(att_splits[train_loc]-1)]
		self.labels_val = labels[np.squeeze(att_splits[val_loc]-1)]
		self.labels_test = labels[np.squeeze(att_splits[test_loc]-1)]

		train_labels_seen = np.unique(self.labels_train)
		val_labels_unseen = np.unique(self.labels_val)
		test_labels_unseen = np.unique(self.labels_test)

		i=0
		for labels in train_labels_seen:
			self.labels_train[self.labels_train == labels] = i
			i+=1

		j=0
		for labels in val_labels_unseen:
			self.labels_val[self.labels_val == labels] = j
			j+=1

		k=0
		for labels in test_labels_unseen:
			self.labels_test[self.labels_test == labels] = k
			k+=1

		sig = att_splits['att']# k x C
		self.train_sig = sig[:, train_labels_seen-1]
		self.val_sig = sig[:, val_labels_unseen-1]
		self.test_sig = sig[:, test_labels_unseen-1]

		self.train_att = np.zeros((self.X_train.shape[1], self.train_sig.shape[0]))
		for i in range(self.train_att.shape[0]):
			self.train_att[i] = self.train_sig.T[self.labels_train[i][0]]

		self.X_train = self.normalizeFeature(self.X_train.T).T

	def normalizeFeature(self, x):
		# x = N x d (d:feature dimension, N:number of instances)
		x = x + 1e-10
		feature_norm = np.sum(x**2, axis=1)**0.5 # l2-norm
		feat = x / feature_norm[:, np.newaxis]

		return feat

	def find_W(self, X, S, ld):

		# INPUTS:
	    # X: d x N - data matrix
	    # S: Number of Attributes (k) x N - semantic matrix
	    # ld: regularization parameter
	    #
	    # Return :
	    # 	W: kxd projection matrix

		A = np.dot(S, S.T)
		B = ld*np.dot(X, X.T)
		C = (1+ld)*np.dot(S, X.T)
		W = linalg.solve_sylvester(A, B, C)

		return W

	def find_lambda(self):

		print('Training...\n')

		if args.accuracy=='logloss':
			best_acc_F2S = 100000
			best_acc_S2F = 100000
		else:
			best_acc_F2S = 0.0
			best_acc_S2F = 0.0

		if args.accuracy=='all':
			if args.dataset=='CUB':
				lambda_F2S = 100
				lambda_S2F = 0.2

				ld = 100
			if args.dataset=='AWA1':
				lambda_F2S = 3.0
				lambda_S2F = 0.8

				ld = 3.0
			if args.dataset=='AWA2':
				lambda_F2S = 0.6
				lambda_S2F = 0.2

				ld = 0.6
			if args.dataset=='APY':
				lambda_F2S = 2.0
				lambda_S2F = 4.0

				ld = 2.0
			if args.dataset=='SUN':
				lambda_F2S = 0.32
				lambda_S2F = 0.16

				ld = 0.32
		else:
			lambda_F2S = self.args.ld1
			lambda_S2F = self.args.ld2

			ld = self.args.ld1

		W = self.find_W(self.X_train, self.train_att.T, ld)

		if args.accuracy=='top1':
			acc_F2S, acc_S2F = self.zsl_acc(self.X_val, W, self.labels_val, self.val_sig, 'val')
		if args.accuracy=='top5':
			acc_F2S, acc_S2F = self.zsl_acc_top5(self.X_val, W, self.labels_val, self.val_sig, 'val')
		if args.accuracy=='logloss':
			acc_F2S, acc_S2F = self.zsl_acc_logloss(self.X_val, W, self.labels_val, self.val_sig, 'val')
		if args.accuracy=='F1':
			acc_F2S, acc_S2F = self.zsl_acc_f1(self.X_val, W, self.labels_val, self.val_sig, 'val')
		if args.accuracy=='all':
			acc_F2S, acc_S2F = self.zsl_acc(self.X_val, W, self.labels_val, self.val_sig, 'val')

		print('Val Acc --> [F-->S]:{} [S-->F]:{} @ lambda = {}\n'.format(acc_F2S, acc_S2F, ld))

		if args.accuracy=='logloss':
			if acc_F2S<best_acc_F2S:
				best_acc_F2S = acc_F2S
				lambda_F2S = ld
				best_W_F2S = np.copy(W)

			if acc_S2F<best_acc_S2F:
				best_acc_S2F = acc_S2F
				lambda_S2F = ld
				best_W_S2F = np.copy(W)
		else:
			if acc_F2S>best_acc_F2S:
				best_acc_F2S = acc_F2S
				lambda_F2S = ld
				best_W_F2S = np.copy(W)

			if acc_S2F>best_acc_S2F:
				best_acc_S2F = acc_S2F
				lambda_S2F = ld
				best_W_S2F = np.copy(W)

		ld*=2

		while (ld<=self.args.ld2):

			W = self.find_W(self.X_train, self.train_att.T, ld)

			if args.accuracy=='top1':
				acc_F2S, acc_S2F = self.zsl_acc(self.X_val, W, self.labels_val, self.val_sig, 'val')
			if args.accuracy=='top5':
				acc_F2S, acc_S2F = self.zsl_acc_top5(self.X_val, W, self.labels_val, self.val_sig, 'val')
			if args.accuracy=='logloss':
				acc_F2S, acc_S2F = self.zsl_acc_logloss(self.X_val, W, self.labels_val, self.val_sig, 'val')
			if args.accuracy=='F1':
				acc_F2S, acc_S2F = self.zsl_acc_f1(self.X_val, W, self.labels_val, self.val_sig, 'val')
			if args.accuracy=='all':
				acc_F2S, acc_S2F = self.zsl_acc(self.X_val, W, self.labels_val, self.val_sig, 'val')

			print('Val Acc --> [F-->S]:{} [S-->F]:{} @ lambda = {}\n'.format(acc_F2S, acc_S2F, ld))

			if args.accuracy=='logloss':
				if acc_F2S<best_acc_F2S:
					best_acc_F2S = acc_F2S
					lambda_F2S = ld
					best_W_F2S = np.copy(W)

				if acc_S2F<best_acc_S2F:
					best_acc_S2F = acc_S2F
					lambda_S2F = ld
					best_W_S2F = np.copy(W)
			else:
				if acc_F2S>best_acc_F2S:
					best_acc_F2S = acc_F2S
					lambda_F2S = ld
					best_W_F2S = np.copy(W)

				if acc_S2F>best_acc_S2F:
					best_acc_S2F = acc_S2F
					lambda_S2F = ld
					best_W_S2F = np.copy(W)

			ld*=2

		print('\nBest Val Acc --> [F-->S]:{} @ lambda = {} [S-->F]:{} @ lambda = {}\n'.format(best_acc_F2S, lambda_F2S, best_acc_S2F, lambda_S2F))

		return best_W_F2S, best_W_S2F

	def zsl_acc(self, X, W, y_true, sig, mode='val'): # Class Averaged Top-1 Accuarcy

		if mode=='F2S':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			pred_F2S = np.array([np.argmax(y) for y in dist_F2S])
			cm_F2S = confusion_matrix(y_true, pred_F2S)
			cm_F2S = cm_F2S.astype('float')/cm_F2S.sum(axis=1)[:, np.newaxis]
			acc_F2S = sum(cm_F2S.diagonal())/sig.shape[1]

			correct_pred = []
			for i in range(len(y_true)):
				correct_pred.append(1) if y_true[i] == pred_F2S[i] else correct_pred.append(0)
			a_file = open("testing/zsl/sae_dist_"+self.args.dataset+".txt", "w")
			b_file = open("testing/zsl/sae_pred_"+self.args.dataset+".txt", "w")
			c_file = open("testing/zsl/sae_"+self.args.dataset+".txt", "w")
			np.savetxt(a_file, dist_F2S)
			np.savetxt(b_file, pred_F2S)
			np.savetxt(c_file, correct_pred)
			a_file.close()
			b_file.close()
			c_file.close()

			return acc_F2S

		if mode=='S2F':
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')
			pred_S2F = np.array([np.argmax(y) for y in dist_S2F])
			cm_S2F = confusion_matrix(y_true, pred_S2F)
			cm_S2F = cm_S2F.astype('float')/cm_S2F.sum(axis=1)[:, np.newaxis]
			acc_S2F = sum(cm_S2F.diagonal())/sig.shape[1]

			return acc_S2F

		if mode=='val':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')

			pred_F2S = np.array([np.argmax(y) for y in dist_F2S])
			pred_S2F = np.array([np.argmax(y) for y in dist_S2F])

			cm_F2S = confusion_matrix(y_true, pred_F2S)
			cm_F2S = cm_F2S.astype('float')/cm_F2S.sum(axis=1)[:, np.newaxis]

			cm_S2F = confusion_matrix(y_true, pred_S2F)
			cm_S2F = cm_S2F.astype('float')/cm_S2F.sum(axis=1)[:, np.newaxis]

			acc_F2S = sum(cm_F2S.diagonal())/sig.shape[1]
			acc_S2F = sum(cm_S2F.diagonal())/sig.shape[1]

			# acc = acc_F2S if acc_F2S>acc_S2F else acc_S2F

			return acc_F2S, acc_S2F

	def zsl_acc_top5(self, X, W, y_true, sig, mode='val'): # Class Averaged Top-5 Accuarcy

		if mode=='F2S':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			pred_F2S = np.argpartition(dist_F2S, kth=-1, axis=-1)[:,-5:]
			classes = np.unique(y_true)
			acc_F2S = 0
			for i in range(len(classes)):
				correct_predictions = 0
				samples = 0
				for j in range(len(y_true)):
					if y_true[j] == classes[i]:
						samples += 1
						if y_true[j] in pred_F2S[j]:
							correct_predictions += 1
				if samples == 0:
					acc_F2S += 1
				else:
					acc_F2S += correct_predictions/samples

			acc_F2S = acc_F2S/len(classes)

			return acc_F2S

		if mode=='S2F':
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')
			pred_S2F = np.argpartition(dist_S2F, kth=-1, axis=-1)[:,-5:]
			classes = np.unique(y_true)
			acc_S2F = 0
			for i in range(len(classes)):
				correct_predictions = 0
				samples = 0
				for j in range(len(y_true)):
					if y_true[j] == classes[i]:
						samples += 1
						if y_true[j] in pred_S2F[j]:
							correct_predictions += 1
				if samples == 0:
					acc_S2F += 1
				else:
					acc_S2F += correct_predictions/samples

			acc_S2F = acc_S2F/len(classes)

			return acc_S2F

		if mode=='val':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')

			pred_F2S = np.argpartition(dist_F2S, kth=-1, axis=-1)[:,-5:]
			pred_S2F = np.argpartition(dist_S2F, kth=-1, axis=-1)[:,-5:]

			classes = np.unique(y_true)

			acc_F2S = 0
			for i in range(len(classes)):
				correct_predictions = 0
				samples = 0
				for j in range(len(y_true)):
					if y_true[j] == classes[i]:
						samples += 1
						if y_true[j] in pred_F2S[j]:
							correct_predictions += 1
				if samples == 0:
					acc_F2S += 1
				else:
					acc_F2S += correct_predictions/samples

			acc_S2F = 0
			for i in range(len(classes)):
				correct_predictions = 0
				samples = 0
				for j in range(len(y_true)):
					if y_true[j] == classes[i]:
						samples += 1
						if y_true[j] in pred_S2F[j]:
							correct_predictions += 1
				if samples == 0:
					acc_S2F += 1
				else:
					acc_S2F += correct_predictions/samples

			acc_F2S = acc_F2S/len(classes)
			acc_S2F = acc_S2F/len(classes)

			# acc = acc_F2S if acc_F2S>acc_S2F else acc_S2F

			return acc_F2S, acc_S2F

	def zsl_acc_logloss(self, X, W, y_true, sig, mode='val'): # Class Averaged LogLoss Accuarcy

		if mode=='F2S':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			acc_F2S = log_loss(y_true, dist_F2S)

			return acc_F2S

		if mode=='S2F':
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')
			acc_S2F = log_loss(y_true, dist_S2F)

			return acc_S2F

		if mode=='val':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')

			acc_F2S = log_loss(y_true, dist_F2S)
			acc_S2F = log_loss(y_true, dist_S2F)

			# acc = acc_F2S if acc_F2S>acc_S2F else acc_S2F

			return acc_F2S, acc_S2F

	def zsl_acc_f1(self, X, W, y_true, sig, mode='val'): # Class Averaged F1 Score Accuarcy

		if mode=='F2S':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			pred_F2S = np.array([np.argmax(y) for y in dist_F2S])
			acc_F2S = f1_score( y_true, pred_F2S, average='micro')

			return acc_F2S

		if mode=='S2F':
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')
			pred_S2F = np.array([np.argmax(y) for y in dist_S2F])
			acc_S2F = f1_score( y_true, pred_S2F, average='micro')

			return acc_S2F

		if mode=='val':
			# [F --> S], projecting data from feature space to semantic space
			F2S = np.dot(X.T, self.normalizeFeature(W).T)# N x k
			dist_F2S = 1-spatial.distance.cdist(F2S, sig.T, 'cosine')# N x C(no. of classes)
			# [S --> F], projecting from semantic to visual space
			S2F = np.dot(sig.T, self.normalizeFeature(W))
			dist_S2F = 1-spatial.distance.cdist(X.T, self.normalizeFeature(S2F), 'cosine')

			pred_F2S = np.array([np.argmax(y) for y in dist_F2S])
			pred_S2F = np.array([np.argmax(y) for y in dist_S2F])

			acc_F2S = f1_score( y_true, pred_F2S, average='micro')
			acc_S2F = f1_score( y_true, pred_S2F, average='micro')

			# acc = acc_F2S if acc_F2S>acc_S2F else acc_S2F

			return acc_F2S, acc_S2F

	def evaluate(self):

		if self.args.mode=='train': best_W_F2S, best_W_S2F = self.find_lambda()
		else:
			if args.accuracy=='all':
				if args.dataset=='CUB':
					self.args.ld1 = 100
					self.args.ld2 = 0.2
				if args.dataset=='AWA1':
					self.args.ld1 = 3.0
					self.args.ld2 = 0.8
				if args.dataset=='AWA2':
					self.args.ld1 = 0.6
					self.args.ld2 = 0.2
				if args.dataset=='APY':
					self.args.ld1 = 2.0
					self.args.ld2 = 4.0
				if args.dataset=='SUN':
					self.args.ld1 = 0.32
					self.args.ld2 = 0.16
			best_W_F2S = self.find_W(self.X_train, self.train_att.T, self.args.ld1)
			best_W_S2F = self.find_W(self.X_train, self.train_att.T, self.args.ld2)

		if args.accuracy=='top1':
			test_acc_F2S = self.zsl_acc(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_S2F = self.zsl_acc(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')
		if args.accuracy=='top5':
			test_acc_F2S = self.zsl_acc_top5(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_S2F = self.zsl_acc_top5(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')
		if args.accuracy=='logloss':
			test_acc_F2S = self.zsl_acc_logloss(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_S2F = self.zsl_acc_logloss(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')
		if args.accuracy=='F1':
			test_acc_F2S = self.zsl_acc_f1(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_S2F = self.zsl_acc_f1(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')
		if args.accuracy=='all':
			test_acc_top1_F2S = self.zsl_acc(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_top1_S2F = self.zsl_acc(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')
			test_acc_top5_F2S = self.zsl_acc_top5(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_top5_S2F = self.zsl_acc_top5(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')
			test_acc_logloss_F2S = self.zsl_acc_logloss(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_logloss_S2F = self.zsl_acc_logloss(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')
			test_acc_f1_F2S = self.zsl_acc_f1(self.X_test, best_W_F2S, self.labels_test, self.test_sig, 'F2S')
			test_acc_f1_S2F = self.zsl_acc_f1(self.X_test, best_W_S2F, self.labels_test, self.test_sig, 'S2F')

		if args.accuracy=='all':
			print('Test Acc Top 1 --> [F-->S]:{} [S-->F]:{}'.format(test_acc_top1_F2S, test_acc_top1_S2F))
			print('Test Acc Top 5 --> [F-->S]:{} [S-->F]:{}'.format(test_acc_top5_F2S, test_acc_top5_S2F))
			print('Test Acc LogLoss --> [F-->S]:{} [S-->F]:{}'.format(test_acc_logloss_F2S, test_acc_logloss_S2F))
			print('Test Acc F1 --> [F-->S]:{} [S-->F]:{}'.format(test_acc_f1_F2S, test_acc_f1_S2F))
		else:
			print('Test Acc --> [F-->S]:{} [S-->F]:{}'.format(test_acc_F2S, test_acc_S2F))

if __name__ == '__main__':

	args = parser.parse_args()
	print('Dataset : {}\n'.format(args.dataset))
	clf = SAE(args)
	clf.evaluate()

reason: import numpy,from scipy


text:
import cv2
import time
import requests
import numpy as np

server_IP = '127.0.0.1'
port = 5000
video_length = 4 # seconds
video_num = 3
ctr = 0

fps = 25
video_size = (1280, 720)

r = requests.get('http://'+server_IP+':'+str(port)+'/video_feed', stream=True)
if(r.status_code == 200):
    # Initialize videowriter
    # codec: *'XVID' saves more space
    video_name = ''.join(['data/', str(ctr), '.avi'])
    out = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc('M','J','P','G'), fps, video_size)

    # Receive streaming
    bytes = bytes()
    start_time = time.time()
    for chunk in r.iter_content(chunk_size=1024*10):
        bytes += chunk
        a = bytes.find(b'\xff\xd8')
        b = bytes.find(b'\xff\xd9')
        if a != -1 and b != -1:
            jpg = bytes[a:b+2]
            bytes = bytes[b+2:]
            frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)

            # show streaming
            # cv2.imshow('frame', frame)

            # save video to file
            out.write(frame)

            if time.time() - start_time > video_length:
                out.release()
                print ('[LOG] {} successfully saved'.format(video_name))
                ctr += 1
                if ctr == video_num:    # stop
                    cv2.destroyAllWindows()
                    exit(0)
                else:   # next video clip
                    video_name = ''.join(['data/', str(ctr), '.avi'])
                    out = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc('M','J','P','G'), fps, video_size)
                    start_time = time.time()

            if cv2.waitKey(1) & 0xFF == ord('q'):
                out.release()
                cv2.destroyAllWindows()
                exit(0)

else:
    print("Received unexpected status code {}".format(r.status_code))

ext: py

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
This module contains only two public functions:

    * `read`
    * `write`

Function `read` reads a MF8/MT section from a string and produces a content object with a dictionary-like
structure.
The content object can be accessed using most of the keywords specified in the ENDF6 manual for this specific
MF section.

Function `write` writes a content object for a MF8/MT section into a string.
MAT, MF, MT and line numbers are also added (each line ends with a `\n`).
"""

import numpy as np
import pdb

from sandy.formats.records import *
from sandy.formats.utils import Section

__author__ = "Luca Fiorito"
__all__ = ["read", "write"]

def read(text):
    """Read MT section for MF8

    Parameters
    ----------
    text : `str`
        one string containing the whole section

    Returns
    -------
    `sandy.formats.utils.Section`
        MF8 content sructured as a collection of dictionaries
    """
    str_list = text.splitlines()
    MAT, MF, MT = read_control(str_list[0])[:3]
    if MT in (454, 459):
        return _read_fy(text)
    elif MT == 457:
        return _read_rdd(text)

def write(sec):
    """Write MT section for MF8

    Parameters
    ----------
    sec : `sandy.utils.Section`
        dictionary with MT section for MF8

    Returns
    -------
    `str`
    """
    if sec["MT"] in (454, 459):
        return _write_fy(sec)
    elif sec["MT"] == 457:
        return _write_rdd(sec)

def _read_fy(text):
    str_list = text.splitlines()
    MAT, MF, MT = read_control(str_list[0])[:3]
    out = {"MAT" : MAT, "MF" : MF, "MT" : MT}
    i = 0
    C, i = read_cont(str_list, i)
    out.update({"ZA" : C.C1, "AWR" : C.C2, "E" : {}})
    for j in range(C.L1):
        L, i = read_list(str_list, i)
        FY = { int(ZAFP*10+FPS) : {"ZAFP" : int(ZAFP), "FPS" : int(FPS), "YI" : YI, "DYI" : DYI} for ZAFP,FPS,YI,DYI in  zip(*[iter(L.B)]*4)}
        out["E"].update({ L.C1 : { "FY" : FY } })
        if j > 0:
            out["E"][L.C1].update({ "I" : L.L1 })
    return Section(out)

def _write_fy(sec):
    LE = len(sec["E"])
    text = write_cont(sec["ZA"], sec["AWR"], LE, 0, 0, 0)
    for i,(e,esec) in enumerate(sorted(sec["E"].items())):
        tab = [ksec[j] for k,ksec in sorted(esec["FY"].items()) for j in ("ZAFP","FPS","YI","DYI")]
        NFP = len(esec["FY"])
        I = LE-1 if i == 0 else esec["I"]
        text += write_list(e, 0, I, 0, NFP, tab)
    TextOut = []; iline = 1
    for line in text:
        if iline > 99999:
            iline = 1
        TextOut.append("{:<66}{:4}{:2}{:3}{:5}\n".format(line, sec["MAT"], sec["MF"], sec["MT"], iline))
        iline += 1
    return "".join(TextOut)

def _read_rdd(text):
    str_list = text.splitlines()
    MAT, MF, MT = read_control(str_list[0])[:3]
    out = {"MAT" : MAT, "MF" : MF, "MT" : MT}
    i = 0
    C, i = read_cont(str_list, i)
    out["ZA"] = ZA = C.C1       # Designation of the original (radioactive) nuclide (ZA = Z*1000 + A)
    out["AWR"] = AWR = C.C2     # Ratio of the LIS state nuclide mass to that of neutron
    out["LIS"] = LIS = C.L1     # State of the original nuclide (LIS=0, ground state, LIS=1, first excited state, etc.)
    out["LISO"] = LISO = C.L2   # Isomeric state number for the original nuclide (LISO=0, ground state; LISO=1, first isomeric state; etc.)
    out["NST"] = NST = C.N1     # Nucleus stability flag (NST=0, radioactive; NST=1, stable)
    NSP = C.N2     # Total number of radiation types (STYP) for which spectral information is given (NSP may be zero)
    L, i = read_list(str_list, i)
    out["HL"] = HL = L.C1       # half-life of the original nuclide (seconds)
    out["DHL"] = DHL = L.C2     # uncertainty on half-life
    out["E"] = E = L.B[::2]     # list of average decay energies (eV) for different radiation types.
    out["DE"] = DE = L.B[1::2]  # list of uncertainties on average decay energy (eV) for different radiation types.
    out["LAMBDA"] = np.log(2)/HL if HL else 0
    L, i = read_list(str_list, i)
    out["SPI"] = SPI = L.C1 # Spin of the nuclide in its LIS state
    out["PAR"] = PAR = L.C2 # Parity of the nuclide in its LIS state
    NDK = L.N2              # Total number of decay modes given
    dk = {}
    # Update list of decay modes when nuclide is radioactive
    for idk,(RTYP,RFS,Q,DQ,BR,DBR) in enumerate(zip(*[iter(L.B)]*6)):
        RTYP = str(RTYP).replace(".", "") # Decay Mode (Multiple particle decay is also allowed using combination of RTYP variables)
        decay = {
                 "RFS" : RFS,   # Isomeric state flag for daughter nuclide
                 "Q" : Q,       # Total decay energy (eV) available in the corresponding decay process
                 "DQ" : DQ,     # Uncertainty on the total decay heat available
                 "BR" : BR,     # Branching ratio
                 "DBR" : DBR    # Uncertainty on branching ratio
                }
        dk[RTYP] = decay
    if NDK > 0:
        out["DK"] = dk
    spectra = {}
    for ist in range(NSP):
        L, i = read_list(str_list, i)
        STYP = int(L.C2)  # Decay spectrum type
        spectra[STYP] = {}
        spectra[STYP]["LCON"] = LCON = L.L1 # Continuum spectrum flag (0=no continuous spectrum, 1=only continuous spectrum, 2=both discrete and continuum spectra)
        NER = L.N2   # Total number of tabulated discrete energies for a given spectral type (STYP)
        spectra[STYP]["FD"] = FD = L.B[0]   # Discrete spectrum normalization factor
        spectra[STYP]["DFD"] = L.B[1]
        spectra[STYP]["ERAV"] = L.B[2]      # Average decay energy of radiation produced
        spectra[STYP]["DERAV"] = L.B[3]
        spectra[STYP]["FC"] = L.B[4]        # Continuum spectrum normalization factor
        spectra[STYP]["DFC"] = L.B[5]
        if LCON != 1:
            discrete_spectrum = {}
            for ier in range(NER):
                discr = {}
                L, i = read_list(str_list, i)
                ER = L.C1                 # Discrete energy (eV) of radiation produced
                discr["DER"] = DER = L.C2 # Uncertainty on discrete energy
                NT = len(L.B)             # Number of entries given for each discrete energy (ER)
                discr["RTYP"] = L.B[0]    # Decay mode
                discr["TYPE"] = L.B[1]    # Type of transition for beta and electron capture
                discr["RI"] = L.B[2]      # intensity of discrete radiation produced (relative units).
                discr["DRI"] = L.B[3]     # uncertainty on the intensity of discrete radiation produced
                discr["RIS"] = L.B[4]     # Internal pair formation coefficient (STYP=2.0 positron intensity, STYP=0.0 otherwise)
                discr["DRIS"] = L.B[5]    # Uncertainty on internal pair formation coefficient
                if NT == 12:
                    discr["RICC"] = L.B[6]   # Total internal conversion coefficient (STYP=0.0 only)
                    discr["DRICC"] = L.B[7]  # Uncertainty on RICC1
                    discr["RICK"] = L.B[8]   # K-shell internal conversion coefficient (STYP=0.0 only)
                    discr["DRICK"] = L.B[9]  # Uncertainty on RICC1
                    discr["RICL"] = L.B[10]  # L-shell internal conversion coefficient (STYP=0.0 only)
                    discr["DRICL"] = L.B[11] # Uncertainty on RICL1
                discrete_spectrum[ER] = discr
            if discrete_spectrum:
                spectra[STYP]["ER"] = discrete_spectrum
        if LCON != 0:
            spectra[STYP]["CONT"] = {}
            cont = {}
            T, i = read_tab1(str_list, i)
            cont["RTYP"] = T.C1        # Decay mode
            cont["LCOV"] = LCOV = T.L2 # Flag indicating whether covariance data are given (0=no, 1=yes).
            cont["NBT"] = T.NBT
            cont["INT"] = T.INT
            cont["E"] = T.x,
            cont["RP"] = T.y           # Normalized spectrum of the continuum component of the radiation (1/eV)
            spectra[STYP]["CONT"][RTYP] = cont
    if spectra:
        out["SPECTRA"] = spectra
    return Section(out)

max_stars_repo_name: khurrumsaleem/sandy

max_issues_repo_name: khurrumsaleem/sandy

max_forks_repo_name: khurrumsaleem/sandy


text:
# -*- coding: utf-8 -*-
##########################################################################
# NSAp - Copyright (C) CEA, 2019 - 2020
# Distributed under the terms of the CeCILL-B license, as published by
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
# for details.
##########################################################################

import logging
import torch
import torch.nn as nn
import torch.nn.functional as func
from sklearn.metrics.pairwise import rbf_kernel, cosine_similarity, polynomial_kernel, linear_kernel, \
    manhattan_distances, laplacian_kernel
from sklearn.metrics import pairwise_distances
import numpy as np

# Global parameters
logger = logging.getLogger("SMLvsDL")

class WeaklySupervisedNTXenLoss(nn.Module):
    """
    This loss is proposed in B. Dufumier, Contrastive Learning with Continuous Proxy Meta-Data for 3D MRI Classification, MICCAI 2021
    """
    def __init__(self, kernel='rbf', temperature=0.1, return_logits=False, sigma=1.0):
        """
        :param kernel: a callable function f: [K, *] x [K, *] -> [K, K]
                                              y1, y2          -> f(y1, y2)
                        where (*) is the dimension of the labels (yi)
        default: an rbf kernel parametrized by 'sigma' which corresponds to gamma=1/(2*sigma**2)

        :param temperature:
        :param return_logits:
        """

        # sigma = prior over the label's range
        super().__init__()
        self.kernel = kernel
        self.sigma = sigma
        if self.kernel == 'rbf':
            self.kernel = lambda y1, y2: rbf_kernel(y1, y2, gamma=1./(2*self.sigma**2))
        elif self.kernel == "discrete":
            self.kernel = WeaklySupervisedNTXenLoss.discrete_kernel
        if self.kernel == 'cosine':
            self.kernel = lambda y1, y2: cosine_similarity(y1, y2)
        elif self.kernel == 'linear':
            self.kernel = lambda y1, y2: linear_kernel(y1, y2)
        elif self.kernel == 'polynomial':
            self.kernel = lambda y1, y2: polynomial_kernel(y1, y2, gamma=1/self.sigma)
        elif self.kernel == "manhattan":
            self.kernel = lambda y1, y2: manhattan_distances(y1, y2)
        elif self.kernel == 'laplacian':
            self.kernel = lambda y1, y2: laplacian_kernel(y1, y2, gamma=1/self.sigma)
        else:
            assert hasattr(self.kernel, '__call__'), 'kernel must be a callable'
        self.temperature = temperature
        self.return_logits = return_logits
        self.INF = 1e8

    @staticmethod
    def discrete_kernel(y1, y2):
        """
        :param y1: matrix shape [N, *]
        :param y2: matrix shape [N, *]
        :return: matrix M shape [N, N] where M[i][j] = 1({y1[i] == y2[j]})
        """
        M = (pairwise_distances(y1, y2, metric="hamming") == 0) # dist = proportion of components disageeing
        return M.astype(np.float)

    def forward(self, z_i, z_j, labels):
        N = len(z_i)
        assert N == len(labels), "Unexpected labels length: %i"%len(labels)
        z_i = func.normalize(z_i, p=2, dim=-1) # dim [N, D]
        z_j = func.normalize(z_j, p=2, dim=-1) # dim [N, D]
        sim_zii= (z_i @ z_i.T) / self.temperature # dim [N, N] => Upper triangle contains incorrect pairs
        sim_zjj = (z_j @ z_j.T) / self.temperature # dim [N, N] => Upper triangle contains incorrect pairs
        sim_zij = (z_i @ z_j.T) / self.temperature # dim [N, N] => the diag contains the correct pairs (i,j) (x transforms via T_i and T_j)
        # 'Remove' the diag terms by penalizing it (exp(-inf) = 0)
        sim_zii = sim_zii - self.INF * torch.eye(N, device=z_i.device)
        sim_zjj = sim_zjj - self.INF * torch.eye(N, device=z_i.device)

        all_labels = labels.view(N, -1).repeat(2, 1).detach().cpu().numpy() # [2N, *]
        weights = self.kernel(all_labels, all_labels) # [2N, 2N]
        weights = weights * (1 - np.eye(2*N)) # puts 0 on the diagonal
        weights /= weights.sum(axis=1)
        # if 'rbf' kernel and sigma->0, we retrieve the classical NTXenLoss (without labels)
        sim_Z = torch.cat([torch.cat([sim_zii, sim_zij], dim=1),
                           torch.cat([sim_zij.T, sim_zjj], dim=1)], dim=0) # [2N, 2N]
        log_sim_Z = func.log_softmax(sim_Z, dim=1)

        loss = -1./N * (torch.from_numpy(weights).to(z_i.device) * log_sim_Z).sum()

        correct_pairs = torch.arange(N, device=z_i.device).long()

        if self.return_logits:
            return loss, sim_zij, correct_pairs

        return loss

    def __str__(self):
        return "{}(temp={}, kernel={}, sigma={})".format(type(self).__name__, self.temperature,
                                                         self.kernel.__name__, self.sigma)

max_stars_repo_name: Duplums/SMLvsDL

max_issues_repo_name: Duplums/SMLvsDL

max_forks_repo_name: Duplums/SMLvsDL


text:
import sys, os
import numpy
from PyQt5.QtWidgets import QApplication

import platform

from orangewidget import gui
from orangewidget.settings import Setting
from oasys.widgets import gui as oasysgui, congruence

from orangecontrib.xoppy.util.xoppy_util import locations
from oasys.widgets.exchange import DataExchangeObject

from orangecontrib.xoppy.widgets.gui.ow_xoppy_widget import XoppyWidget

from syned.widget.widget_decorator import WidgetDecorator
import syned.beamline.beamline as synedb
from syned.storage_ring.magnetic_structures.insertion_device import InsertionDevice as synedid

class OWtcap(XoppyWidget):
    name = "Aperture Flux TC"
    id = "orange.widgets.datatcap"
    description = "On-axis Aperture Flux Undulator Tuning Curves"
    icon = "icons/xoppy_xtc.png"
    priority = 8
    category = ""
    keywords = ["xoppy", "tcap"]

    #25 variables (minus one: TITLE removed)
    ENERGY = Setting(6.0)
    CURRENT = Setting(200.0)
    ENERGY_SPREAD = Setting(0.00138)
    SIGX = Setting(0.0148)
    SIGY = Setting(0.0037)
    SIGX1 = Setting(0.0029)
    SIGY1 = Setting(0.0015)
    PERIOD = Setting(2.8)
    NP = Setting(84)
    EMIN = Setting(3217.0)
    EMAX = Setting(11975.0)
    N = Setting(50)
    DISTANCE = Setting(30.0)
    XPS = Setting(1.0)
    YPS = Setting(1.0)
    XPC = Setting(0.0)
    YPC = Setting(0.0)
    HARMONIC_FROM = Setting(1)
    HARMONIC_TO = Setting(7)
    HARMONIC_STEP = Setting(2)
    HRED = Setting(0)
    HELICAL = Setting(0)
    NEKS = Setting(100)
    METHOD = Setting(0)
    BSL = Setting(0)

    inputs = WidgetDecorator.syned_input_data()

    def build_gui(self):

        self.IMAGE_WIDTH = 850

        tabs_setting = oasysgui.tabWidget(self.controlArea)
        tabs_setting.setFixedWidth(self.CONTROL_AREA_WIDTH-5)

        tab_1 = oasysgui.createTabPage(tabs_setting, self.name + " Input Parameters")
        tab_2 = oasysgui.createTabPage(tabs_setting, "Undulator Setting")

        box = oasysgui.widgetBox(tab_1, "", orientation="vertical", width=self.CONTROL_AREA_WIDTH-15)

        idx = -1

        #widget index 1
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_ENERGY = oasysgui.lineEdit(box1, self, "ENERGY",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 2
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_CURRENT = oasysgui.lineEdit(box1, self, "CURRENT",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 3
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_ENERGY_SPREAD = oasysgui.lineEdit(box1, self, "ENERGY_SPREAD",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 4
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_SIGX = oasysgui.lineEdit(box1, self, "SIGX",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 5
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_SIGY = oasysgui.lineEdit(box1, self, "SIGY",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 6
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_SIGX1 = oasysgui.lineEdit(box1, self, "SIGX1",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 7
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_SIGY1 = oasysgui.lineEdit(box1, self, "SIGY1",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 8
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_PERIOD = oasysgui.lineEdit(box1, self, "PERIOD",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 9
        idx += 1
        box1 = gui.widgetBox(box)
        self.id_NP = oasysgui.lineEdit(box1, self, "NP",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 10
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "EMIN",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 11
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "EMAX",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float,  orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 12
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "N",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 13
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "DISTANCE",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 14
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "XPS",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 15
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "YPS",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 16
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "XPC",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 17
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "YPC",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=float, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        box = oasysgui.widgetBox(tab_2, "", orientation="vertical", width=self.CONTROL_AREA_WIDTH-15)

        #widget index 18
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "HARMONIC_FROM",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 19
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "HARMONIC_TO",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 20
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "HARMONIC_STEP",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 21
        idx += 1
        box1 = gui.widgetBox(box)
        gui.comboBox(box1, self, "HRED",
                     label=self.unitLabels()[idx], addSpace=False,
                     items=['No', 'Yes'],
                     valueType=int, orientation="horizontal", labelWidth=350)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 22
        idx += 1
        box1 = gui.widgetBox(box)
        gui.comboBox(box1, self, "HELICAL",
                     label=self.unitLabels()[idx], addSpace=False,
                     items=['Planar undulator', 'Helical undulator'],
                     valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 23
        idx += 1
        box1 = gui.widgetBox(box)
        oasysgui.lineEdit(box1, self, "NEKS",
                     label=self.unitLabels()[idx], addSpace=False,
                    valueType=int, orientation="horizontal", labelWidth=250)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 24
        idx += 1
        box1 = gui.widgetBox(box)
        gui.comboBox(box1, self, "METHOD",
                     label=self.unitLabels()[idx], addSpace=False,
                     items=['Infinite N +convolution (Dejus)','Infinite N +convolution (Dejus)','Infinite N +convolution(Walker)','Finite-N'],
                     valueType=int, orientation="horizontal", labelWidth=150)
        self.show_at(self.unitFlags()[idx], box1)

        #widget index 25
        idx += 1
        box1 = gui.widgetBox(box)
        gui.comboBox(box1, self, "BSL",
                     label=self.unitLabels()[idx], addSpace=False,
                     items=['No', 'Yes'],
                     valueType=int, orientation="horizontal", labelWidth=350)
        self.show_at(self.unitFlags()[idx], box1)

    def unitLabels(self):
         return ['Electron energy (GeV)','Current (mA)','Energy Spread (dE/E)',
                 'Sigma X (mm)','Sigma Y (mm)',"Sigma X' (mrad)","Sigma Y' (mrad)",
                 'Period length (cm)','Number of periods',
                 'E1 minimum energy (eV)','E1 maximum energy (eV)','Number of energy-points',
                 'Distance to Aperture [m]', 'Aperture Width H [mm]', 'Aperture Height V [mm]',
                 'Aperture Center H [mm]', 'Aperture Center V [mm]',
                 'Minimum harmonic number','Maximum harmonic number','Harmonic step size','Apply Harmonic Reduction',
                 'Mode','Neks OR % Helicity','Method','Baseline Subraction']

    def unitFlags(self):
         return ['True' for i in range(26)]

    def get_help_name(self):
        return 'tcap'

    def check_fields(self):
        self.ENERGY = congruence.checkStrictlyPositiveNumber(self.ENERGY, "Electron Energy")
        self.CURRENT = congruence.checkStrictlyPositiveNumber(self.CURRENT, "Current")
        self.ENERGY_SPREAD = congruence.checkStrictlyPositiveNumber(self.ENERGY_SPREAD, "Energy Spread")
        self.SIGX  = congruence.checkPositiveNumber(self.SIGX , "Sigma X")
        self.SIGY  = congruence.checkPositiveNumber(self.SIGY , "Sigma Y")
        self.SIGX1 = congruence.checkPositiveNumber(self.SIGX1, "Sigma X'")
        self.SIGY1 = congruence.checkPositiveNumber(self.SIGY1, "Sigma Y'")
        self.PERIOD = congruence.checkStrictlyPositiveNumber(self.PERIOD, "Period length")
        self.NP = congruence.checkStrictlyPositiveNumber(self.NP, "Number of periods")
        self.EMIN = congruence.checkPositiveNumber(self.EMIN, "E1 minimum energy")
        self.EMAX = congruence.checkStrictlyPositiveNumber(self.EMAX, "E1 maximum energy")
        congruence.checkLessThan(self.EMIN, self.EMAX, "E1 minimum energy", "E1 maximum energy")
        self.N = congruence.checkStrictlyPositiveNumber(self.N, "Number of Energy Points")
        self.DISTANCE = congruence.checkPositiveNumber(self.DISTANCE, "Distance to slit")
        self.XPS = congruence.checkPositiveNumber(self.XPS, "Aperture Width H")
        self.YPS = congruence.checkPositiveNumber(self.YPS, "Aperture Height V")
        self.XPC = congruence.checkPositiveNumber(self.XPC, "Aperture Center H")
        self.YPC = congruence.checkPositiveNumber(self.YPC, "Aperture Center V")
        self.HARMONIC_FROM = congruence.checkStrictlyPositiveNumber(self.HARMONIC_FROM, "Minimum harmonic number")
        self.HARMONIC_TO = congruence.checkStrictlyPositiveNumber(self.HARMONIC_TO, "Maximum harmonic number")
        congruence.checkLessThan(self.HARMONIC_FROM, self.HARMONIC_TO, "Minimum harmonic number", "Maximum harmonic number")
        self.HARMONIC_STEP = congruence.checkStrictlyPositiveNumber(self.HARMONIC_STEP, "Harmonic step size")
        self.NEKS = congruence.checkStrictlyPositiveNumber(self.NEKS, "Neks OR % Helicity")

    def do_xoppy_calculation(self):
        return self.xoppy_calc_xtcap()

    def extract_data_from_xoppy_output(self, calculation_output):
        return calculation_output

    def plot_results(self, calculated_data, progressBarValue=80):
        if not self.view_type == 0:
            if not calculated_data is None:
                self.view_type_combo.setEnabled(False)

                xoppy_data_harmonics = calculated_data.get_content("xoppy_data_harmonics")

                titles = self.getTitles()
                xtitles = self.getXTitles()
                ytitles = self.getYTitles()

                progress_bar_step = (100-progressBarValue)/len(titles)

                for index in range(0, len(titles)):
                    x_index, y_index = self.getVariablesToPlot()[index]
                    log_x, log_y = self.getLogPlot()[index]

                    if not self.plot_canvas[index] is None:
                        self.plot_canvas[index].clear()

                    try:
                        for h_index in range(0, len(xoppy_data_harmonics)):

                            self.plot_histo(xoppy_data_harmonics[h_index][1][:, x_index],
                                            xoppy_data_harmonics[h_index][1][:, y_index],
                                            progressBarValue + ((index+1)*progress_bar_step),
                                            tabs_canvas_index=index,
                                            plot_canvas_index=index,
                                            title=titles[index],
                                            xtitle=xtitles[index],
                                            ytitle=ytitles[index],
                                            log_x=log_x,
                                            log_y=log_y,
                                            harmonic=xoppy_data_harmonics[h_index][0],
                                            control=True)

                        self.plot_canvas[index].addCurve(numpy.zeros(1),
                                                         numpy.array([max(xoppy_data_harmonics[h_index][1][:, y_index])]),
                                                         "Click on curve to highlight it",
                                                         xlabel=xtitles[index], ylabel=ytitles[index],
                                                         symbol='', color='white')

                        self.plot_canvas[index].setActiveCurve("Click on curve to highlight it")
                        self.plot_canvas[index].getLegendsDockWidget().setFixedHeight(150)
                        self.plot_canvas[index].getLegendsDockWidget().setVisible(True)

                        self.tabs.setCurrentIndex(index)
                    except Exception as e:
                        self.view_type_combo.setEnabled(True)

                        raise Exception("Data not plottable: bad content\n" + str(e))

                self.view_type_combo.setEnabled(True)
            else:
                raise Exception("Empty Data")

    def plot_histo(self, x, y, progressBarValue, tabs_canvas_index, plot_canvas_index, title="", xtitle="", ytitle="",
                   log_x=False, log_y=False, harmonic=1, color='blue', control=True):
        h_title = "Harmonic " + str(harmonic)

        hex_r = hex(min(255, 128 + harmonic*10))[2:].upper()
        hex_g = hex(min(255, 20 + harmonic*15))[2:].upper()
        hex_b = hex(min(255, harmonic*10))[2:].upper()
        if len(hex_r) == 1: hex_r = "0" + hex_r
        if len(hex_g) == 1: hex_g = "0" + hex_g
        if len(hex_b) == 1: hex_b = "0" + hex_b

        #super().plot_histo(x, y, progressBarValue, tabs_canvas_index, plot_canvas_index, h_title, xtitle, ytitle,
        #                   log_x, log_y, color="green", replace=False, control=control)

        super().plot_histo(x, y, progressBarValue, tabs_canvas_index, plot_canvas_index, h_title, xtitle, ytitle,
                           log_x, log_y, color="#" + hex_r + hex_g + hex_b, replace=False, control=control)

        self.plot_canvas[plot_canvas_index].setGraphTitle(title)
        self.plot_canvas[plot_canvas_index].setDefaultPlotLines(True)

    def get_data_exchange_widget_name(self):
        return "XTCAP"

    def getTitles(self):
        return ["Flux peak","Ky","Total Power","Power density peak","Integrated Power on aperture"]

    def getXTitles(self):
        return ["Energy (eV)","Energy (eV)","Energy (eV)","Energy (eV)","Energy (eV)"]

    def getYTitles(self):
        return ["Flux (ph/s/mm^2/0.1%bw)","Ky","Total Power (W)","Power density (W/mm^2)","Integrated Power on Aperture (W)"]

    def getVariablesToPlot(self):
        return [(0, 1), (0, 3), (0, 4), (0, 5), (0, 6)]

    def getLogPlot(self):
        return[(False, True), (False, False), (False, False), (False, False),(False, False)]

    def xoppy_calc_xtcap(self):

        for file in ["tcap.inp","tcap.out","tcap.log"]:
            try:
                os.remove(os.path.join(locations.home_bin_run(),file))
            except:
                pass

        with open("tcap.inp", "wt") as f:
            f.write("TCAP called from xoppy\n")
            f.write("%10.3f %10.2f %10.6f %s\n"%(self.ENERGY,self.CURRENT,self.ENERGY_SPREAD,"Ring-Energy(GeV) Current(mA) Beam-Energy-Spread"))
            f.write("%10.4f %10.4f %10.4f %10.4f %s\n"%(self.SIGX,self.SIGY,self.SIGX1,self.SIGY1,"Sx(mm) Sy(mm) Sx1(mrad) Sy1(mrad)"))
            f.write("%10.3f %d %s\n"%(self.PERIOD,self.NP,"Period(cm) N"))
            f.write("%10.1f %10.1f %d %s\n"%(self.EMIN,self.EMAX,self.N,"Emin Emax Ne"))
            f.write("%10.3f %10.3f %10.3f %10.3f %10.3f %d %d %s\n"%(self.DISTANCE,self.XPC,self.YPC,self.XPS,self.YPS,10,10,"d xpc ypc xps yps nxp nyp"))
            f.write("%d %d %d %d %s\n"%(self.HARMONIC_FROM,self.HARMONIC_TO,self.HARMONIC_STEP,self.HRED,"Hmin Hmax Hstep Hreduction"))
            f.write("%d %d %d %d %d %s\n"%(self.HELICAL,self.METHOD,1,self.NEKS,self.BSL,"Helical Method Print_K Neks Bsl-Subtr "))
            f.write("foreground\n")

        if platform.system() == "Windows":
            command = "\"" + os.path.join(locations.home_bin(),'tcap.exe') + "\""
        else:
            command = "'" + os.path.join(locations.home_bin(), 'tcap') + "'"

        print("Running command '%s' in directory: %s "%(command, locations.home_bin_run()))
        print("\n--------------------------------------------------------\n")
        # os.system(command)

        #
        #   catch the optut and write the output to a log file as well as print it.
        #
        retvalue = os.popen(command).read()
        print(retvalue)

        with open("tcap.log", "wt") as f:
            f.write(retvalue)

        print("Output file: '%s/tcap.out'"%(os.getcwd()) )
        print("\n--------------------------------------------------------\n")

        #
        # parse result files to exchange object
        #

        with open("tcap.out","r") as f:
            lines = f.readlines()

        # print output file
        # for line in lines:
        #     print(line, end="")

        # remove returns
        lines = [line[:-1] for line in lines]
        harmonics_data = []

        # separate numerical data from text
        floatlist = []
        harmoniclist = []
        txtlist = []
        for line in lines:
            try:
                tmp = line.strip()

                if tmp.startswith("Harmonic"):
 #                   harmonic_number = int(tmp.split("Harmonic")[1].strip())
                    harmonic_number = int(tmp.split()[1])

                    if harmonic_number != self.HARMONIC_FROM:
                        harmonics_data[-1][1] = harmoniclist
                        harmoniclist = []

                    harmonics_data.append([harmonic_number, None])

                tmp = float(line.strip()[0])

                floatlist.append(line)
                harmoniclist.append(line)
            except:
                txtlist.append(line)

        harmonics_data[-1][1] = harmoniclist

        data = numpy.loadtxt(floatlist)

        for index in range(0, len(harmonics_data)):
            # print (harmonics_data[index][0], harmonics_data[index][1])
            harmonics_data[index][1] = numpy.loadtxt(harmonics_data[index][1])

        #send exchange
        calculated_data = DataExchangeObject("XOPPY", self.get_data_exchange_widget_name())

        try:
            calculated_data.add_content("xoppy_data", data)
            calculated_data.add_content("xoppy_data_harmonics", harmonics_data)
            calculated_data.add_content("plot_x_col", 0)
            calculated_data.add_content("plot_y_col", 1)
        except:
            pass

        try:
            calculated_data.add_content("labels",["Energy(eV)","Flux(ph/s/0.1%bw)","FWHM(eV)","Ky",
                                            "Ptot(W)","Pd(W/mm^2)","IntP(W)"])

        except:
            pass
        try:
            calculated_data.add_content("info",txtlist)
        except:
            pass

        return calculated_data

    def receive_syned_data(self, data):

        if isinstance(data, synedb.Beamline):
            if not data.get_light_source() is None and isinstance(data.get_light_source().get_magnetic_structure(), synedid):
                light_source = data.get_light_source()

                self.ENERGY = light_source.get_electron_beam().energy()
                self.ENERGY_SPREAD = light_source.get_electron_beam()._energy_spread
                self.CURRENT = 1000.0 * light_source.get_electron_beam().current()

                x, xp, y, yp = light_source.get_electron_beam().get_sigmas_all()

                self.SIGX = 1e3 * x
                self.SIGY = 1e3 * y
                self.SIGX1 = 1e3 * xp
                self.SIGY1 = 1e3 * yp
                self.PERIOD = 100.0 * light_source._magnetic_structure._period_length
                self.NP = light_source._magnetic_structure._number_of_periods

                self.EMIN = light_source.get_magnetic_structure().resonance_energy(gamma=light_source.get_electron_beam().gamma())
                self.EMAX = 5 * self.EMIN

                self.set_enabled(False)

            else:
                self.set_enabled(True)
                # raise ValueError("Syned data not correct")
        else:
            self.set_enabled(True)
            # raise ValueError("Syned data not correct")

    def set_enabled(self,value):
        if value == True:
                self.id_ENERGY.setEnabled(True)
                self.id_ENERGY_SPREAD.setEnabled(True)
                self.id_SIGX.setEnabled(True)
                self.id_SIGX1.setEnabled(True)
                self.id_SIGY.setEnabled(True)
                self.id_SIGY1.setEnabled(True)
                self.id_CURRENT.setEnabled(True)
                self.id_PERIOD.setEnabled(True)
                self.id_NP.setEnabled(True)
        else:
                self.id_ENERGY.setEnabled(False)
                self.id_ENERGY_SPREAD.setEnabled(False)
                self.id_SIGX.setEnabled(False)
                self.id_SIGX1.setEnabled(False)
                self.id_SIGY.setEnabled(False)
                self.id_SIGY1.setEnabled(False)
                self.id_CURRENT.setEnabled(False)
                self.id_PERIOD.setEnabled(False)
                self.id_NP.setEnabled(False)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = OWtcap()
    w.show()
    app.exec()
    w.saveSettings()


text:
#%% [markdown]
# # Chapter 6 ゲート付きRNN

#%% [markdown]
# 1. RNNの問題点
# 1. 勾配消失とLSTM
# 1. LSTMの実装
# 1. LSTMを使った言語モデル
# 1. LSTMのさらなる改善
# 1. まとめ

#%% [markdown]
### 勾配クリッピングの実装

#%%
import numpy as np

dW1 = np.random.rand(3, 3) * 10
dW2 = np.random.rand(3, 3) * 10
grads = [dW1, dW2]
max_norm = 5.0

def clip_grads(grads, max_norm):
    total_norm = 0
    for grad in grads:
        total_norm += np.sum(grad ** 2)
    total_norm = np.sqrt(total_norm)

    rate = max_norm / (total_norm + 1e-6)
    if rate < 1:
        for grad in grads:
            grad *= rate

print('before:', dW1.flatten())
clip_grads(grads, max_norm)
print('after:', dW1.flatten())

#%% [markdown]
### LSTMクラスの実装

#%%
class LSTM:
    def __init__(self, Wx, Wh, b):
        '''

        Parameters
        ----------
        Wx: 入力`x`用の重みパラーメタ（4つ分の重みをまとめる）
        Wh: 隠れ状態`h`用の重みパラメータ（4つ分の重みをまとめる）
        b: バイアス（4つ分のバイアスをまとめる）
        '''
        self.params = [Wx, Wh, b]
        self.grads = [np.zeros_like(Wx), np.zeros_like(Wh), np.zeros_like(b)]
        self.cache = None

    def forward(self, x, h_prev, c_prev):
        Wx, Wh, b = self.params
        N, H = h_prev.shape

        A = np.dot(x, Wx) + np.dot(h_prev, Wh) + b

        f = A[:, :H]
        g = A[:, H:2*H]
        i = A[:, 2*H:3*H]
        o = A[:, 3*H:]

        f = sigmoid(f)
        g = np.tanh(g)
        i = sigmoid(i)
        o = sigmoid(o)

        c_next = f * c_prev + g * i
        h_next = o * np.tanh(c_next)

        self.cache = (x, h_prev, c_prev, i, f, g, o, c_next)
        return h_next, c_next

    def backward(self, dh_next, dc_next):
        Wx, Wh, b = self.params
        x, h_prev, c_prev, i, f, g, o, c_next = self.cache

        tanh_c_next = np.tanh(c_next)

        ds = dc_next + (dh_next * o) * (1 - tanh_c_next ** 2)

        dc_prev = ds * f

        di = ds * g
        df = ds * c_prev
        do = dh_next * tanh_c_next
        dg = ds * i

        di *= i * (1 - i)
        df *= f * (1 - f)
        do *= o * (1 - o)
        dg *= (1 - g ** 2)

        dA = np.hstack((df, dg, di, do))

        dWh = np.dot(h_prev.T, dA)
        dWx = np.dot(x.T, dA)
        db = dA.sum(axis=0)

        self.grads[0][...] = dWx
        self.grads[1][...] = dWh
        self.grads[2][...] = db

        dx = np.dot(dA, Wx.T)
        dh_prev = np.dot(dA, Wh.T)

        return dx, dh_prev, dc_prev

#%% [markdown]
### TimeLSTMレイヤの実装

#%%
class TimeLSTM:
    def __init__(self, Wx, Wh, b, stateful=False):
        self.params = [Wx, Wh, b]
        self.grads = [np.zeros_like(Wx), np.zeros_like(Wh), np.zeros_like(b)]
        self.layers = None

        self.h, self.c = None, None
        self.dh = None
        self.stateful = stateful

    def forward(self, xs):
        Wx, Wh, b = self.params
        N, T, D = xs.shape
        H = Wh.shape[0]

        self.layers = []
        hs = np.empty((N, T, H), dtype='f')

        if not self.stateful or self.h is None:
            self.h = np.zeros((N, H), dtype='f')
        if not self.stateful or self.c is None:
            self.c = np.zeros((N, H), dtype='f')

        for t in range(T):
            layer = LSTM(*self.params)
            self.h, self.c = layer.forward(xs[:, t, :], self.h, self.c)
            hs[:, t, :] = self.h

            self.layers.append(layer)

        return hs

    def backward(self, dhs):
        Wx, Wh, b = self.params
        N, T, H = dhs.shape
        D = Wx.shape[0]

        dxs = np.empty((N, T, D), dtype='f')
        dh, dc = 0, 0

        grads = [0, 0, 0]
        for t in reversed(range(T)):
            layer = self.layers[t]
            dx, dh, dc = layer.backward(dhs[:, t, :] + dh, dc)
            dxs[:, t, :] = dx
            for i, grad in enumerate(layer.grads):
                grads[i] += grad

        for i, grad in enumerate(grads):
            self.grads[i][...] = grad
        self.dh = dh
        return dxs

    def set_state(self, h, c=None):
        self.h, self.c = h, c

    def reset_state(self):
        self.h, self.c = None, None

#%% [markdown]
### LSTMレイヤを使ったRNNLMクラス
import sys
sys.path.append('..')
from common.time_layers import *
from common.base_model import BaseModel

class Rnnlm(BaseModel):
    def __init__(self, vocab_size=10000, wordvec_size=100, hidden_size=100):
        V, D, H = vocab_size, wordvec_size, hidden_size
        rn = np.random.randn

        # 重みの初期化
        embed_W = (rn(V, D) / 100).astype('f')
        lstm_Wx = (rn(D, 4 * H) / np.sqrt(D)).astype('f')
        lstm_Wh = (rn(H, 4 * H) / np.sqrt(H)).astype('f')
        lstm_b = np.zeros(4 * H).astype('f')
        affine_W = (rn(H, V) / np.sqrt(H)).astype('f')
        affine_b = np.zeros(V).astype('f')

        # レイヤの生成
        self.layers = [
            TimeEmbedding(embed_W),
            TimeLSTM(lstm_Wx, lstm_Wh, lstm_b, stateful=True),
            TimeAffine(affine_W, affine_b)
        ]
        self.loss_layer = TimeSoftmaxWithLoss()
        self.lstm_layer = self.layers[1]

        # すべての重みと勾配をリストにまとめる
        self.params, self.grads = [], []
        for layer in self.layers:
            self.params += layer.params
            self.grads += layer.grads

    def predict(self, xs):
        for layer in self.layers:
            xs = layer.forward(xs)
        return xs

    def forward(self, xs, ts):
        score = self.predict(xs)
        loss = self.loss_layer.forward(score, ts)
        return loss

    def backward(self, dout=1):
        dout = self.loss_layer.backward(dout)
        for layer in reversed(self.layers):
            dout = layer.backward(dout)
        return dout

    def reset_state(self):
        self.lstm_layer.reset_state()

#%% [markdown]
### PTBデータセットの学習（時間がかかりそうなので実行しない）

#%%
'''
from common.optimizer import SGD
from common.trainer import RnnlmTrainer
from common.util import eval_perplexity
from dataset import ptb
from rnnlm import Rnnlm

# ハイパーパラメータの設定
batch_size = 20
wordvec_size = 100
hidden_size = 100  # RNNの隠れ状態ベクトルの要素数
time_size = 35  # RNNを展開するサイズ
lr = 20.0
max_epoch = 4
max_grad = 0.25

# 学習データの読み込み
corpus, word_to_id, id_to_word = ptb.load_data('train')
corpus_test, _, _ = ptb.load_data('test')
vocab_size = len(word_to_id)
xs = corpus[:-1]
ts = corpus[1:]

# モデルの生成
model = Rnnlm(vocab_size, wordvec_size, hidden_size)
optimizer = SGD(lr)
trainer = RnnlmTrainer(model, optimizer)

# 勾配クリッピングを適用して学習
trainer.fit(xs, ts, max_epoch, batch_size, time_size, max_grad,
            eval_interval=20)
trainer.plot(ylim=(0, 500))

# テストデータで評価
model.reset_state()
ppl_test = eval_perplexity(model, corpus_test)
print('test perplexity: ', ppl_test)

# パラメータの保存
model.save_params()
'''

#%% [markdown]
# ## RNNのさらなる改善
# - LSTMレイヤの多層化
# - Dropoutを仕様（深さ方向のみ）
# - 重み共有（EmbeddingレイヤとAffineレイヤ）

#%%
class BetterRnnlm(BaseModel):
    '''
     LSTMレイヤを2層利用し、各層にDropoutを使うモデル
     [1]で提案されたモデルをベースとし、weight tying[2][3]を利用

     [1] Recurrent Neural Network Regularization (https://arxiv.org/abs/1409.2329)
     [2] Using the Output Embedding to Improve Language Models (https://arxiv.org/abs/1608.05859)
     [3] Tying Word Vectors and Word Classifiers (https://arxiv.org/pdf/1611.01462.pdf)
    '''
    def __init__(self, vocab_size=10000, wordvec_size=650,
                 hidden_size=650, dropout_ratio=0.5):
        V, D, H = vocab_size, wordvec_size, hidden_size
        rn = np.random.randn

        embed_W = (rn(V, D) / 100).astype('f')
        lstm_Wx1 = (rn(D, 4 * H) / np.sqrt(D)).astype('f')
        lstm_Wh1 = (rn(H, 4 * H) / np.sqrt(H)).astype('f')
        lstm_b1 = np.zeros(4 * H).astype('f')
        lstm_Wx2 = (rn(H, 4 * H) / np.sqrt(H)).astype('f')
        lstm_Wh2 = (rn(H, 4 * H) / np.sqrt(H)).astype('f')
        lstm_b2 = np.zeros(4 * H).astype('f')
        affine_b = np.zeros(V).astype('f')

        self.layers = [
            TimeEmbedding(embed_W),
            TimeDropout(dropout_ratio),
            TimeLSTM(lstm_Wx1, lstm_Wh1, lstm_b1, stateful=True),
            TimeDropout(dropout_ratio),
            TimeLSTM(lstm_Wx2, lstm_Wh2, lstm_b2, stateful=True),
            TimeDropout(dropout_ratio),
            TimeAffine(embed_W.T, affine_b)  # weight tying!!
        ]
        self.loss_layer = TimeSoftmaxWithLoss()
        self.lstm_layers = [self.layers[2], self.layers[4]]
        self.drop_layers = [self.layers[1], self.layers[3], self.layers[5]]

        self.params, self.grads = [], []
        for layer in self.layers:
            self.params += layer.params
            self.grads += layer.grads

    def predict(self, xs, train_flg=False):
        for layer in self.drop_layers:
            layer.train_flg = train_flg

        for layer in self.layers:
            xs = layer.forward(xs)
        return xs

    def forward(self, xs, ts, train_flg=True):
        score = self.predict(xs, train_flg)
        loss = self.loss_layer.forward(score, ts)
        return loss

    def backward(self, dout=1):
        dout = self.loss_layer.backward(dout)
        for layer in reversed(self.layers):
            dout = layer.backward(dout)
        return dout

    def reset_state(self):
        for layer in self.lstm_layers:
            layer.reset_state()

#%% [markdown]
# ## BetterRNNの学習（時間がかかりそうなので実行しない）

#%%
'''
from common import config
# GPUで実行する場合は下記のコメントアウトを消去（要cupy）
# ==============================================
# config.GPU = True
# ==============================================
from common.optimizer import SGD
from common.trainer import RnnlmTrainer
from common.util import eval_perplexity, to_gpu
from dataset import ptb
from better_rnnlm import BetterRnnlm

# ハイパーパラメータの設定
batch_size = 20
wordvec_size = 650
hidden_size = 650
time_size = 35
lr = 20.0
max_epoch = 40
max_grad = 0.25
dropout = 0.5

# 学習データの読み込み
corpus, word_to_id, id_to_word = ptb.load_data('train')
corpus_val, _, _ = ptb.load_data('val')
corpus_test, _, _ = ptb.load_data('test')

if config.GPU:
    corpus = to_gpu(corpus)
    corpus_val = to_gpu(corpus_val)
    corpus_test = to_gpu(corpus_test)

vocab_size = len(word_to_id)
xs = corpus[:-1]
ts = corpus[1:]

model = BetterRnnlm(vocab_size, wordvec_size, hidden_size, dropout)
optimizer = SGD(lr)
trainer = RnnlmTrainer(model, optimizer)

best_ppl = float('inf')
for epoch in range(max_epoch):
    trainer.fit(xs, ts, max_epoch=1, batch_size=batch_size,
                time_size=time_size, max_grad=max_grad)

    model.reset_state()
    ppl = eval_perplexity(model, corpus_val)
    print('valid perplexity: ', ppl)

    if best_ppl > ppl:
        best_ppl = ppl
        model.save_params()
    else:
        lr /= 4.0
        optimizer.lr = lr

    model.reset_state()
    print('-' * 50)

# テストデータでの評価
model.reset_state()
ppl_test = eval_perplexity(model, corpus_test)
print('test perplexity: ', ppl_test)
'''


text:
"""DataLoader using Multi Processes and Multi Data Queue"""
from __future__ import absolute_import
import six

import copy
import random
import time
from random import randint

import multiprocessing as mp
import multiprocessing.queues

import numpy as np
import numpy.random as npr

from . import sampler as _sampler

# NOTE: if use this dataloader, you should do multiprcessing reduction firstly,
# such as case of pytorch, you should do following before creating dataloader:
#from torch import multiprocessing as torch_mp
# in which the function of init_reductions() is called.

class DataLoaderSharedMemory(object):

    # for distinguishing from other dataloader
    _attr_ngn_dataloader = True

    """Dataloader Control Entry"""
    def __init__(self, dataset, batch_size=None,
                 data_shape=None,
                 shuffle=False, sampler=None,
                 last_batch=None, batch_sampler=None,
                 batchify_fn=None,  # for self batch
                 inter_batchify_fn=None,  # for internal dataloader
                 part_num=20,  # for part loader
                 num_workers=0):
        self.dataset = dataset

        self.batch_size = batch_size
        self.shuffle = shuffle
        self.sampler = sampler
        self.last_batch = last_batch
        self.batch_sampler = batch_sampler

        self.num_workers = num_workers
        if self.num_workers < 1:
            raise Exception('num_workers should > 0')

        if batchify_fn is None:
            raise Exception('no batchify_fn is specified')
        else:
            self.batchify_fn = batchify_fn

        self.inter_batchify_fn = inter_batchify_fn

        self.num = None  # num
        if self.num is None:
            self.num = len(self.dataset) // self.batch_size

        self.cache_n = self.num_workers  # cache_n
        self.cache_i = 0
        self.cache_num = None  # cache_num
        if self.cache_num is None:
            self.init_cache_num()

        self.part_num = part_num  # num of batches in shared memory once time

        self.init_qs()

        self.init_sampler()
        self.init_loaders()

    def init_cache_num(self):
        # NOTE: self.cache_n > 0
        n = self.num // self.cache_n
        m = self.num % self.cache_n
        if m == 0:
            self.cache_num = n
        else:
            self.cache_num = n + 1

    def init_qs(self):
        # TODO: set queue size
        self.qs_msg = [mp.Queue() for i in range(self.cache_n)]
        self.qs_data = [mp.Queue() for i in range(self.cache_n)]
        self.qs_ctl = [mp.Queue() for i in range(self.cache_n)]

    def init_sampler(self):
        if self.batch_sampler is None:
            if self.batch_size is None:
                raise ValueError("batch_size must be specified unless " \
                                 "batch_sampler is specified")
            if self.sampler is None:
                self.init_slice_sampler()
            elif self.shuffle:
                raise ValueError("shuffle must not be specified if sampler is specified")

            self.batch_sampler = _sampler.BatchSampler(
                self.sampler, self.batch_size,
                self.last_batch if self.last_batch else 'keep')

        elif self.batch_size is not None or self.shuffle or self.sampler is not None or \
                self.last_batch is not None:
            raise ValueError("batch_size, shuffle, sampler and last_batch must " \
                             "not be specified if batch_sampler is specified.")

    def init_slice_sampler(self):
        ds_len = self.num * self.batch_size  #len(self.dataset)
        self.sampler = _sampler.SliceSampler(int(ds_len), shuffle=self.shuffle)

    def get_one_batch_sampler(self, i):
        a = self.cache_num * self.batch_size * i
        b = self.cache_num * self.batch_size * (i + 1)
        s = self.sampler[int(a):int(b)]
        bs = _sampler.SliceBatchSampler(s, self.batch_size)
        return bs

    def reset_batch_sampler(self, new_sampler):
        self.sampler = new_sampler
        self.batch_sampler = _sampler.BatchSampler(
            self.sampler, self.batch_size,
            self.last_batch if self.last_batch else 'keep')

        self.num = len(new_sampler) // self.batch_size
        self.init_cache_num()

        for i in range(self.cache_n):
            bs = self.get_one_batch_sampler(i)
            self.qs_ctl[i].put(('reset_batch_sampler', bs))

    def get_one_loader(self, i):
        bs = self.get_one_batch_sampler(i)
        ldr = DataloaderShmem(self.dataset, bs, self.batch_size,
                              batchify_fn=self.inter_batchify_fn)
        return ldr

    def start_one_loader_cache(self, i, q_msg, q_data, q_ctl,
                               ldr, num, part_num, shuffle):
        cchp = DataloaderShmemPart(
            i, q_msg, q_data, q_ctl, ldr,
            num=num, part_num=part_num, shuffle=shuffle)
        return cchp

    def init_loaders(self):
        self.op_loaders = []
        self.workers = []

        for i in range(self.cache_n):
            ldr = self.get_one_loader(i)
            self.op_loaders.append(ldr)

            worker = mp.Process(
                target=self.start_one_loader_cache,
                args=(i, self.qs_msg[i], self.qs_data[i], self.qs_ctl[i],
                      ldr, len(ldr), self.part_num, self.shuffle))
            worker.daemon = True
            worker.start()
            self.workers.append(worker)

    def next_cache(self):
        self.cache_i += 1
        self.cache_i %= self.cache_n

    def __len__(self):
        return self.num

    def _get_a_batch(self, dt):
        d = self.batchify_fn(dt)
        return d

    def __iter__(self):
        # clear qs
        self.clear_qs()

        # NOTE: ask loader to start load data
        self.qs_ctl_put(('start_epoch', 1))

        # begin from the first cache
        self.cache_i = 0

        q_get_timeout_n = 0

        num_batches = self.num

        curr_idx = 0
        while curr_idx < num_batches:
            #print('>>>> ', self.cache_i, curr_idx, num_batches)

            #n = self.qs_msg[self.cache_i].get()

            # NOTE: use timeout to avoid blocking forever
            '''
            try:
                if curr_idx == 0:  # wait with no timeout at the first time
                    tmout = None
                else:
                    # NOTE: dynamically set the time
                    tmout = self.part_num * (3 + q_get_timeout_n)
                n = self.qs_msg[self.cache_i].get(timeout=tmout)
            except Exception as e:
                if q_get_timeout_n <= self.cache_n:
                    n = 0
                    q_get_timeout_n += 1
                else:
                    break  # no data
            '''

            try:
                if curr_idx == 0:  # wait with no timeout at the first time
                    tmout = None
                else:
                    # NOTE: dynamically set the time
                    tmout = 0.01
                n = self.qs_msg[self.cache_i].get(timeout=tmout)
            except Exception as e:
                n = 0  # goto next cache

            for j in range(n):
                dt = self.qs_data[self.cache_i].get()  # (data, label)
                data_batch = self._get_a_batch(dt)
                curr_idx += 1
                yield data_batch  # >>> iter entry

            if n > 0:  # continue load data
                self.qs_ctl[self.cache_i].put(('get_items', self.part_num))  # continue load data

            self.next_cache()

        for k in range(self.cache_n):
            self.qs_ctl[k].put(('stop_epoch', 0))

    def clear(self):  # NOTE: should be called outside when main process ends

        self.qs_ctl_put(('clear', 0))

        for worker in self.workers:
            worker.join()

    def q_clear(self, q):
        while not q.empty():
            try:
                a = q.get(timeout=1)
            except Exception as e:
                pass

    def qs_clear(self, qs):
        for q in qs:
            self.q_clear(q)

    def clear_qs(self):
        self.qs_clear(self.qs_msg)
        self.qs_clear(self.qs_data)
        self.qs_clear(self.qs_ctl)

    def qs_ctl_put(self, msg):
        for q in self.qs_ctl:
            q.put(msg)

class DataloaderShmem(object):
    """Simple dataloader for one process"""
    def __init__(self, dataset, batch_sampler, batch_size, batchify_fn=None,
                 last_batch=None, *args, **kwargs):
        self._dataset = dataset
        self.batch_sampler = batch_sampler
        self.batch_size = batch_size
        self._batchify_fn = batchify_fn
        if self._batchify_fn is None:
            self._batchify_fn = self._default_batchify_fn

    def _default_batchify_fn(self, dt):
        return dt

    def __len__(self):
        return len(self.batch_sampler)

    def __iter__(self):
        for batch in self.batch_sampler:
            yield self._batchify_fn([self._dataset[idx] for idx in batch])
        return

    def reset(self):
        pass

    def shuffle(self):
        self.batch_sampler.shuffle()

    def reset_batch_sampler(self, new_batch_sampler):
        self.batch_sampler = new_batch_sampler

class DataloaderShmemPart(object):
    """Dataloader process for part"""
    def __init__(self, cache_i, q_msg, q_data, q_ctl,
                 dataloader, num=None, part_num=20, shuffle=True,
                 *args, **kwargs):
        self.cache_i = cache_i
        self.q_msg = q_msg
        self.q_data = q_data
        self.q_ctl = q_ctl

        self.dataloader = dataloader

        self.batch_size = self.dataloader.batch_size

        self.num = num
        if self.num is None:
            self.num = len(self.dataloader)

        self.part_num = part_num
        self.init_part_n()

        self.shuffle = shuffle

        self.cache = {}

        self.loop()

    def init_part_n(self):
        n = self.num / self.part_num
        m = self.num % self.part_num
        if m == 0:
            self.part_n = n
        else:
            self.part_n = n + 1

    def loop(self):
        while 1:
            msg = self.q_ctl.get()
            cmd = msg[0]
            if cmd == 'start_epoch':
                ret = self.load()
                cmd = ret[0]
                if cmd == 'clear':
                    break
            elif cmd == 'get_items':
                pass
            elif cmd == 'stop_epoch':
                pass
            elif cmd == 'clear':
                break
            elif cmd == 'reset_batch_sampler':
                bs = msg[1]
                self.reset_batch_sampler(bs)
            else:
                raise Exception('unknown cmd %s' % str(msg))

    def load(self):
        if self.shuffle:
            self.dataloader.shuffle()

        nbatch = 0

        part_num = self.part_num

        data_iter = iter(self.dataloader)
        end_of_batch = False

        try:
            # pre fetch next batch
            next_data_batch = next(data_iter)
        except StopIteration:
            end_of_batch = True
        except Exception as e:
            print('nx'*100, e)
            end_of_batch = True

        i = 0
        while not end_of_batch:
            #print('>', self.cache_i, nbatch)

            #data_batch = next_data_batch
            #data, label = data_batch

            try:
                #self.q_data.put((data, label))
                self.q_data.put(next_data_batch)

                i += 1
            except Exception as e:
                print('x' * 40, e)
                pass  # next data

            if i % part_num == 0:
                self.q_msg.put(i)
                i = 0

                msg = self.q_ctl.get()
                cmd = msg[0]
                if cmd == 'stop_epoch':
                    end_of_batch = True
                elif cmd == 'get_items':
                    part_num = msg[1]
                else:  # clear
                    return msg

            try:
                # pre fetch next batch
                next_data_batch = next(data_iter)
            except StopIteration:
                end_of_batch = True

            nbatch += 1

            if not end_of_batch:
                if nbatch >= self.num:
                    end_of_batch = True

        # the last data
        if i > 0:
            self.q_msg.put(i)
            i = 0

        # wait control msg from q
        while 1:
            try:
                msg = self.q_ctl.get(timeout=100)
            except Exception as e:
                msg = ('stop_epoch', 0)

            return msg

    def __len__(self):
        return self.num

    def reset(self):
        self.dataloader.reset()

    def reset_batch_sampler(self, new_batch_sampler):
        self.dataloader.reset_batch_sampler(new_batch_sampler)


text:
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""MindSpore Serving Client"""

import grpc
import numpy as np
import mindspore_serving.proto.ms_service_pb2 as ms_service_pb2
import mindspore_serving.proto.ms_service_pb2_grpc as ms_service_pb2_grpc

def _create_tensor(data, tensor=None):
    """Create tensor from numpy data"""
    if tensor is None:
        tensor = ms_service_pb2.Tensor()

    tensor.shape.dims.extend(data.shape)
    dtype_map = {
        np.bool: ms_service_pb2.MS_BOOL,
        np.int8: ms_service_pb2.MS_INT8,
        np.uint8: ms_service_pb2.MS_UINT8,
        np.int16: ms_service_pb2.MS_INT16,
        np.uint16: ms_service_pb2.MS_UINT16,
        np.int32: ms_service_pb2.MS_INT32,
        np.uint32: ms_service_pb2.MS_UINT32,

        np.int64: ms_service_pb2.MS_INT64,
        np.uint64: ms_service_pb2.MS_UINT64,
        np.float16: ms_service_pb2.MS_FLOAT16,
        np.float32: ms_service_pb2.MS_FLOAT32,
        np.float64: ms_service_pb2.MS_FLOAT64,
    }
    for k, v in dtype_map.items():
        if k == data.dtype:
            tensor.dtype = v
            break
    if tensor.dtype == ms_service_pb2.MS_UNKNOWN:
        raise RuntimeError("Unknown data type " + str(data.dtype))
    tensor.data = data.tobytes()
    return tensor

def _create_scalar_tensor(vals, tensor=None):
    """Create tensor from scalar data"""
    if not isinstance(vals, (tuple, list)):
        vals = (vals,)
    return _create_tensor(np.array(vals), tensor)

def _create_bytes_tensor(bytes_vals, tensor=None):
    """Create tensor from bytes data"""
    if tensor is None:
        tensor = ms_service_pb2.Tensor()

    if not isinstance(bytes_vals, (tuple, list)):
        bytes_vals = (bytes_vals,)
    tensor.shape.dims.extend([len(bytes_vals)])
    tensor.dtype = ms_service_pb2.MS_BYTES
    for item in bytes_vals:
        tensor.bytes_val.append(item)
    return tensor

def _create_str_tensor(str_vals, tensor=None):
    """Create tensor from str data"""
    if tensor is None:
        tensor = ms_service_pb2.Tensor()

    if not isinstance(str_vals, (tuple, list)):
        str_vals = (str_vals,)
    tensor.shape.dims.extend([len(str_vals)])
    tensor.dtype = ms_service_pb2.MS_STRING
    for item in str_vals:
        tensor.bytes_val.append(bytes(item, encoding="utf8"))
    return tensor

def _create_numpy_from_tensor(tensor):
    """Create numpy from protobuf tensor"""
    dtype_map = {
        ms_service_pb2.MS_BOOL: np.bool,
        ms_service_pb2.MS_INT8: np.int8,
        ms_service_pb2.MS_UINT8: np.uint8,
        ms_service_pb2.MS_INT16: np.int16,
        ms_service_pb2.MS_UINT16: np.uint16,
        ms_service_pb2.MS_INT32: np.int32,
        ms_service_pb2.MS_UINT32: np.uint32,

        ms_service_pb2.MS_INT64: np.int64,
        ms_service_pb2.MS_UINT64: np.uint64,
        ms_service_pb2.MS_FLOAT16: np.float16,
        ms_service_pb2.MS_FLOAT32: np.float32,
        ms_service_pb2.MS_FLOAT64: np.float64,
    }
    if tensor.dtype == ms_service_pb2.MS_STRING or tensor.dtype == ms_service_pb2.MS_BYTES:
        result = []
        for item in tensor.bytes_val:
            if tensor.dtype == ms_service_pb2.MS_STRING:
                result.append(bytes.decode(item))
            else:
                result.append(item)
        if len(result) == 1:
            return result[0]
        return result

    result = np.frombuffer(tensor.data, dtype_map[tensor.dtype]).reshape(tensor.shape.dims)
    return result

def _check_str(arg_name, str_val):
    """Check whether the input parameters are reasonable str input"""
    if not isinstance(str_val, str):
        raise RuntimeError(f"Parameter '{arg_name}' should be str, but actually {type(str_val)}")
    if not str_val:
        raise RuntimeError(f"Parameter '{arg_name}' should not be empty str")

def _check_int(arg_name, int_val, minimum=None, maximum=None):
    """Check whether the input parameters are reasonable int input"""
    if not isinstance(int_val, int):
        raise RuntimeError(f"Parameter '{arg_name}' should be int, but actually {type(int_val)}")
    if minimum is not None and int_val < minimum:
        if maximum is not None:
            raise RuntimeError(f"Parameter '{arg_name}' should be in range [{minimum},{maximum}]")
        raise RuntimeError(f"Parameter '{arg_name}' should be >= {minimum}")
    if maximum is not None and int_val > maximum:
        if minimum is not None:
            raise RuntimeError(f"Parameter '{arg_name}' should be in range [{minimum},{maximum}]")
        raise RuntimeError(f"Parameter '{arg_name}' should be <= {maximum}")

class SSLConfig:
    """
    The client's ssl_config encapsulates grpc's ssl channel credentials for SSL-enabled connections.

    Args:
        certificate (str, optional): File holding the PEM-encoded certificate chain as a byte string to use or None if
            no certificate chain should be used. Default: None.
        private_key (str, optional): File holding the PEM-encoded private key as a byte string, or None if no private
            key should be used. Default: None.
        custom_ca (str, optional): File holding the PEM-encoded root certificates as a byte string, or None to retrieve
            them from a default location chosen by gRPC runtime. Default: None.

    Raises:
        RuntimeError: The type or value of the parameters is invalid.

    """

    def __init__(self, certificate=None, private_key=None, custom_ca=None):
        if certificate is not None:
            _check_str("certificate", certificate)
        if private_key is not None:
            _check_str("private_key", private_key)
        if custom_ca is not None:
            _check_str("custom_ca", custom_ca)

        self.certificate = certificate
        self.private_key = private_key
        self.custom_ca = custom_ca

class Client:
    """
    The Client encapsulates the serving gRPC API, which can be used to create requests,
    access serving, and parse results.

    Note:
        The maximum amount of data that the client can send in one request is 512MB, and the maximum amount of data that
        the server can accept can be configured as 1~512MB, 100MB by default.

    Args:
        address (str): Serving address.
        servable_name (str): The name of servable supplied by Serving.
        method_name (str): The name of method supplied by servable.
        version_number (int, optional): The version number of servable, 0 means the maximum version number in all
            running -versions. Default: 0.
        ssl_config (mindspore_serving.client.SSLConfig, optional): The server's ssl_config, if None, disabled ssl.
            Default: None.

    Raises:
        RuntimeError: The type or value of the parameters are invalid, or other errors happened.

    Examples:
        >>> from mindspore_serving.client import Client
        >>> import numpy as np
        >>> client = Client("localhost:5500", "add", "add_cast")
        >>> instances = []
        >>> x1 = np.ones((2, 2), np.int32)
        >>> x2 = np.ones((2, 2), np.int32)
        >>> instances.append({"x1": x1, "x2": x2})
        >>> result = client.infer(instances)
        >>> print(result)
    """

    def __init__(self, address, servable_name, method_name, version_number=0, ssl_config=None):
        _check_str("address", address)
        _check_str("servable_name", servable_name)
        _check_str("method_name", method_name)
        _check_int("version_number", version_number, 0)

        self.address = address
        self.servable_name = servable_name
        self.method_name = method_name
        self.version_number = version_number

        msg_bytes_size = 512 * 1024 * 1024  # 512MB
        options = [
            ('grpc.max_send_message_length', msg_bytes_size),
            ('grpc.max_receive_message_length', msg_bytes_size),
        ]
        if ssl_config is not None:
            if not isinstance(ssl_config, SSLConfig):
                raise RuntimeError("The type of ssl_config should be type of SSLConfig")
            rc_bytes = pk_bytes = c_bytes = None
            if ssl_config.certificate is not None:
                with open(ssl_config.certificate, 'rb') as c_fs:
                    c_bytes = c_fs.read()
            if ssl_config.private_key is not None:
                with open(ssl_config.private_key, 'rb') as pk_fs:
                    pk_bytes = pk_fs.read()
            if ssl_config.custom_ca is not None:
                with open(ssl_config.custom_ca, 'rb') as rc_fs:
                    rc_bytes = rc_fs.read()
            if (c_bytes is None and pk_bytes is not None) or (c_bytes is not None and pk_bytes is None):
                raise RuntimeError("The certificate and private_key should be passed at the same time")
            creds = grpc.ssl_channel_credentials(root_certificates=rc_bytes,
                                                 private_key=pk_bytes,
                                                 certificate_chain=c_bytes)
            self.channel = grpc.secure_channel(address, creds, options=options)
        else:
            self.channel = grpc.insecure_channel(address, options=options)

        self.stub = ms_service_pb2_grpc.MSServiceStub(self.channel)

    def infer(self, instances):
        """
        Used to create requests, access serving service, and parse and return results.

        Args:
            instances (map, tuple of map): Instance or tuple of instances, every instance item is the inputs map.
                The map key is the input name, and the value is the input value, the type of value can be python int,
                float, bool, str, bytes, numpy number, or numpy array object.

        Raises:
            RuntimeError: The type or value of the parameters is invalid, or other errors happened.

        Examples:
            >>> from mindspore_serving.client import Client
            >>> import numpy as np
            >>> client = Client("localhost:5500", "add", "add_cast")
            >>> instances = []
            >>> x1 = np.ones((2, 2), np.int32)
            >>> x2 = np.ones((2, 2), np.int32)
            >>> instances.append({"x1": x1, "x2": x2})
            >>> result = client.infer(instances)
            >>> print(result)
        """
        request = self._create_request(instances)
        try:
            result = self.stub.Predict(request)
            return self._paser_result(result)

        except grpc.RpcError as e:
            print(e.details())
            status_code = e.code()
            print(status_code.name)
            print(status_code.value)
            return {"error": f"Grpc Error, {status_code.value}, {e.details()}"}

    def infer_async(self, instances):
        """
        Used to create requests, async access serving.

        Args:
            instances (map, tuple of map): Instance or tuple of instances, every instance item is the inputs map.
                The map key is the input name, and the value is the input value.

        Raises:
            RuntimeError: The type or value of the parameters is invalid, or other errors happened.

        Examples:
            >>> from mindspore_serving.client import Client
            >>> import numpy as np
            >>> client = Client("localhost:5500", "add", "add_cast")
            >>> instances = []
            >>> x1 = np.ones((2, 2), np.int32)
            >>> x2 = np.ones((2, 2), np.int32)
            >>> instances.append({"x1": x1, "x2": x2})
            >>> result_future = client.infer_async(instances)
            >>> result = result_future.result()
            >>> print(result)
        """
        request = self._create_request(instances)
        try:
            result_future = self.stub.Predict.future(request)
            return ClientGrpcAsyncResult(result_future)

        except grpc.RpcError as e:
            print(e.details())
            status_code = e.code()
            print(status_code.name)
            print(status_code.value)
            return ClientGrpcAsyncError({"error": f"Grpc Error, {status_code.value}, {e.details()}"})

    def _create_request(self, instances):
        """Used to create request spec."""
        if not isinstance(instances, (tuple, list)):
            instances = (instances,)

        request = ms_service_pb2.PredictRequest()
        request.servable_spec.name = self.servable_name
        request.servable_spec.method_name = self.method_name
        request.servable_spec.version_number = self.version_number

        for item in instances:
            if isinstance(item, dict):
                request.instances.append(self._create_instance(**item))
            else:
                raise RuntimeError("instance should be a map")
        return request

    @staticmethod
    def _create_instance(**kwargs):
        """Used to create gRPC instance."""
        instance = ms_service_pb2.Instance()
        for k, w in kwargs.items():
            tensor = instance.items[k]
            if isinstance(w, (np.ndarray, np.number)):
                _create_tensor(w, tensor)
            elif isinstance(w, str):
                _create_str_tensor(w, tensor)
            elif isinstance(w, (bool, int, float)):
                _create_scalar_tensor(w, tensor)
            elif isinstance(w, bytes):
                _create_bytes_tensor(w, tensor)
            else:
                raise RuntimeError("Not support value type " + str(type(w)))
        return instance

    @staticmethod
    def _paser_result(result):
        """Used to parse result."""
        error_msg_len = len(result.error_msg)
        if error_msg_len == 1 and result.error_msg[0].error_code != 0:
            return {"error": bytes.decode(result.error_msg[0].error_msg)}
        ret_val = []
        instance_len = len(result.instances)
        if error_msg_len not in (0, instance_len):
            raise RuntimeError(f"error msg result size {error_msg_len} not be 0, 1 or "
                               f"length of instances {instance_len}")
        for i in range(instance_len):
            instance = result.instances[i]
            if error_msg_len == 0 or result.error_msg[i].error_code == 0:
                instance_map = {}
                for k, w in instance.items.items():
                    instance_map[k] = _create_numpy_from_tensor(w)
                ret_val.append(instance_map)
            else:
                ret_val.append({"error": bytes.decode(result.error_msg[i].error_msg)})
        return ret_val

class ClientGrpcAsyncResult:
    """
    When Client.infer_async invoke successfully, a ClientGrpcAsyncResult object is returned.

    Examples:
        >>> from mindspore_serving.client import Client
        >>> import numpy as np
        >>> client = Client("localhost:5500", "add", "add_cast")
        >>> instances = []
        >>> x1 = np.ones((2, 2), np.int32)
        >>> x2 = np.ones((2, 2), np.int32)
        >>> instances.append({"x1": x1, "x2": x2})
        >>> result_future = client.infer_async(instances)
        >>> result = result_future.result()
        >>> print(result)
    """

    def __init__(self, result_future):
        self.result_future = result_future

    def result(self):
        """Wait and get result of inference result, the gRPC message will be parse to tuple of instances result.
        Every instance result is dict, and value could be numpy array/number, str or bytes according gRPC Tensor
        data type.
        """
        result = self.result_future.result()
        # pylint: disable=protected-access
        result = Client._paser_result(result)
        return result

class ClientGrpcAsyncError:
    """When gRPC failed happened when calling Client.infer_async, a ClientGrpcAsyncError object is returned.
    """

    def __init__(self, result_error):
        self.result_error = result_error

    def result(self):
        """Get gRPC error message.
        """
        return self.result_error

max_stars_repo_name: mindspore-ai/serving

max_issues_repo_name: mindspore-ai/serving

max_forks_repo_name: mindspore-ai/serving

reason: import numpy,from numpy