text:
### Authors: Nicolas Y. Masse, Gregory D. Grant

import numpy as np
from parameters_v6 import par
from math import gamma
import scipy.signal
import matplotlib.pyplot as plt

class Stimulus:

	def __init__(self, analysis=False):

		# Stimulus shapes
		self.input_shape    = [par['num_time_steps']*par['trials_per_seq'], par['batch_size'], par['n_input'] ]
		self.filtered_input_shape    = [par['num_time_steps']*par['trials_per_seq'], par['batch_size'], par['n_filters']*par['n_input'] ]
		self.output_shape   = [par['num_time_steps']*par['trials_per_seq'], par['batch_size'], par['n_pol'] ]
		self.stimulus_shape = [par['num_time_steps']*par['trials_per_seq'], par['batch_size'], par['num_motion_tuned'] ]
		self.response_shape = [par['num_time_steps']*par['trials_per_seq'], par['batch_size'], par['num_motion_dirs'] ]
		self.fixation_shape = [par['num_time_steps']*par['trials_per_seq'], par['batch_size'], par['num_fix_tuned'] ]
		self.rule_shape		= [par['num_time_steps']*par['trials_per_seq'], par['batch_size'], par['num_rule_tuned'] ]
		self.mask_shape     = [par['num_time_steps']*par['trials_per_seq'], par['batch_size']]

		# Gamma filters
		self.gamma_filters = self.make_gamma_filters()

		# Motion information
		self.modality_size    	= par['num_motion_tuned']//2
		self.motion_dirs	  	= np.arange(0,2*np.pi,2*np.pi/par['num_motion_dirs'])
		self.stimulus_dirs	  	= np.arange(0,2*np.pi,2*np.pi/self.modality_size)
		self.pref_motion_dirs 	= self.stimulus_dirs[:,np.newaxis]

		# Configurations
		self.dm_c_set 	  = np.array([-0.4, -0.2, -0.1, 0.1, 0.2, 0.4])
		self.dm_dly_c_set = np.array([-0.4, -0.2, -0.1, 0.1, 0.2, 0.4])

		# Timings (ms)
		self.fix_time = 400

		if not analysis:
			self.go_delay 		 	= np.array([200, 400, 800])//par['dt']
			self.dm_stim_lengths	= np.array([200, 400, 800])//par['dt']
			self.dm_dly_delay 	 	= np.array([200, 400, 800])//par['dt']
			self.match_delay 	 	= np.array([200, 400, 800])//par['dt']
		else:
			self.go_delay 		 	= np.array([400])//par['dt']
			self.dm_stim_lengths 	= np.array([400])//par['dt']
			self.dm_dly_delay 	 	= np.array([400])//par['dt']
			self.match_delay 	 	= np.array([400])//par['dt']

		# Initialize task interface
		self.get_tasks()

	def make_gamma_filters(self):

		c = 0.08
		t = np.arange(1000//par['dt'])/(1000/par['dt'])
		f = np.zeros((1, par['n_filters'], len(t)), dtype = np.float32)
		for i in range(par['n_filters']):
			k = 2**i
			f[0, i, :] = t**(k-1)*np.exp(-t/c)/(gamma(k)*c**k)
			f[0, i, :] /= np.sum(f[0, i, :])

		return np.real(f)

	def circ_tuning(self, theta):
		ang_dist = np.angle(np.exp(1j*theta - 1j*self.pref_motion_dirs))
		return par['tuning_height']*np.exp(-0.5*(8*ang_dist/np.pi)**2)

	def rule_tuning(self, task):
		if par['num_rule_tuned'] >= len(self.task_types):
			tuning = np.zeros(self.input_shape, dtype=np.float32)
			tuning[:,:,-par['num_rule_tuned']+task] = par['tuning_height']
			return tuning
		else:
			raise Exception('Use more rule neurons than task types.')

	def get_tasks(self):

		if par['task'] == 'go':
			self.task_types = [
				[self.task_go, 'go', 0],
				[self.task_go, 'go', np.pi]
			]

		elif par['task'] == 'dms':
			self.task_types = [
				[self.task_matching, 'dms'],
			]

		elif par['task'] == 'multistim':

			self.task_types = []
			for offset in [0, np.pi, np.pi/2, -np.pi/2, np.pi/4, -np.pi/4, 3*np.pi/4, -3*np.pi/4]:

				self.task_types.append([self.task_go, 'go', offset])
				self.task_types.append([self.task_go, 'rt_go', offset])

				self.task_types.append([self.task_go, 'go_OIC', offset, 0])
				self.task_types.append([self.task_go, 'go_OIC', offset, 2])
				self.task_types.append([self.task_go, 'rt_OIC', offset, 0])
				self.task_types.append([self.task_go, 'rt_OIC', offset, 2])

				#self.task_types.append([self.task_go, 'go_OIC', offset, 5])
				#self.task_types.append([self.task_go, 'dly_go', offset])

				self.task_types.append([self.task_dm, 'dm1',offset])
				self.task_types.append([self.task_dm, 'dm2',offset])

				self.task_types.append([self.task_dm, 'ctx_dm1',offset])
				self.task_types.append([self.task_dm, 'ctx_dm2',offset])
				self.task_types.append([self.task_dm, 'multsen_dm',offset])
				"""

				self.task_types.append([self.task_dm_dly, 'dm1_dly',offset])
				self.task_types.append([self.task_dm_dly, 'dm2_dly',offset])
				self.task_types.append([self.task_dm_dly, 'ctx_dm1_dly',offset])
				self.task_types.append([self.task_dm_dly, 'ctx_dm2_dly',offset])
				self.task_types.append([self.task_dm_dly, 'multsen_dm_dly',offset])
				# the above trials are used for the 100_tasks_XXX

				self.task_types.append([self.task_matching, 'dms',offset])
				self.task_types.append([self.task_matching, 'dmc',offset])
				"""

		elif par['task'] == 'twelvestim':
			self.task_types = [
				[self.task_go, 'go', 0],
				[self.task_go, 'dly_go', 0],

				[self.task_dm, 'dm1'],
				[self.task_dm, 'dm2'],
				[self.task_dm, 'ctx_dm1'],
				[self.task_dm, 'ctx_dm2'],
				[self.task_dm, 'multsen_dm'],

				[self.task_dm_dly, 'dm1_dly'],
				[self.task_dm_dly, 'dm2_dly'],
				[self.task_dm_dly, 'ctx_dm1_dly'],
				[self.task_dm_dly, 'ctx_dm2_dly'],
				[self.task_dm_dly, 'multsen_dm_dly']
			]

	def generate_trial(self, fixed_task_num = None):

		# Create blank trial info
		self.trial_info = {
			'neural_input'   : np.random.normal(par['input_mean'], par['noise_in'], size=self.input_shape),
			'neural_input_filtered'   : np.random.normal(par['input_mean'], par['noise_in'], size=self.filtered_input_shape),
			'desired_output' : np.zeros(self.output_shape, dtype=np.float32),
			'reward_data'    : np.zeros(self.output_shape, dtype=np.float32),
			'reward_matrix'  : np.zeros([*self.output_shape, par['num_reward_types']], dtype=np.float32),
			'train_mask'     : np.ones(self.mask_shape, dtype=np.float32)
		}

		# Populate trial info with basic information
		self.trial_info['train_mask'][:par['dead_time']//par['dt'],:] = 0.
		if par['num_rule_tuned'] > 0:
			self.trial_info['neural_input'] += self.rule_tuning(task_num)

		# Apply reinforcement learning task specifications
		for self.trial_num in range(par['batch_size']):

			if fixed_task_num is None:
				task_num = np.random.choice(par['task_list'])
			else:
				task_num = fixed_task_num
			#print('task num ', task_num, self.task_types[task_num][1])
			current_task = self.task_types[task_num]
			current_task[0](*current_task[1:])

			resp_vect = np.sum(self.trial_info['desired_output'][:,self.trial_num,:-1], axis=1)

			for b in range(par['trials_per_seq']):

				# Designate timings
				t0 = par['num_time_steps']*b
				t1 = par['num_time_steps']*(b+1)
				respond_time    = np.where(resp_vect[t0:t1] > 0)[0]
				fix_time        = list(range(t0,t0+respond_time[0])) if len(respond_time) > 0 else [t1-1]
				respond_time    = t0+respond_time if len(respond_time) > 0 else [t1-1]

				# Designate responses
				correct_response    = np.where(self.trial_info['desired_output'][respond_time[0],self.trial_num,:]==1)[0]
				incorrect_response  = np.where(self.trial_info['desired_output'][respond_time[0],self.trial_num,:-1]==0)[0]

				# Build reward data
				self.trial_info['reward_data'][fix_time,self.trial_num,:-1] = par['fix_break_penalty']
				self.trial_info['reward_matrix'][fix_time,self.trial_num,:-1, 0] = 1.
				self.trial_info['reward_data'][respond_time,self.trial_num,correct_response] = par['correct_choice_reward']
				self.trial_info['reward_matrix'][respond_time,self.trial_num,correct_response, 3] = 1.
				self.trial_info['reward_matrix'][fix_time,self.trial_num,-1, 2] = 1.

				for i in incorrect_response:
					self.trial_info['reward_data'][respond_time,self.trial_num,i] = par['wrong_choice_penalty']
					self.trial_info['reward_matrix'][respond_time,self.trial_num,i,1] = 1.

					# Penalize fixating throughout entire trial if response was required
					if not self.trial_info['desired_output'][t1-1,self.trial_num,-1] == 1:
						pass
						#self.trial_info['reward_data'][t1-1,self.trial_num,-1] = par['fix_break_penalty']
						#self.trial_info['reward_matrix'][t1-1,self.trial_num,-1, 0] = 1.
					else:
						self.trial_info['reward_data'][t1-1,self.trial_num,-1] = par['correct_choice_reward']
						self.trial_info['reward_matrix'][t1-1,self.trial_num,-1, 3] = 1.

		# Make required corrections
		self.trial_info['neural_input'] = np.maximum(0., self.trial_info['neural_input'])

		# SIMPLIFYING STIM; TESTING ONLY
		#self.trial_info['neural_input'] = np.float32(self.trial_info['neural_input'] > par['tuning_height']-0.001)

		s = [scipy.signal.convolve(self.trial_info['neural_input'], np.transpose(self.gamma_filters[0:1,i:i+1,:],[2,1,0]), 'full') \
			for i in range(par['n_filters'])]
		"""
		for i in range(5):
			plt.imshow(s[i][:, 0, :], aspect = 'auto')
			plt.show()
		"""
		s_concat = np.concatenate([*s], axis = -1)
		s_concat = s_concat[:par['num_time_steps']*par['trials_per_seq'], :,:]
		self.trial_info['neural_input_filtered'] = s_concat

		# Returns the task name and trial info
		return current_task[1], self.trial_info

	def task_go(self, variant='go', offset=0, cat_boundary = 0):

		# Task parameters
		if variant == 'go' or variant == 'go_OIC':
			stim_onset = np.random.randint(self.fix_time, self.fix_time+800, par['trials_per_seq'])//par['dt']
			stim_off = par['num_time_steps']
			fixation_end = np.ones(par['trials_per_seq'], dtype=np.int16)*(self.fix_time+800)//par['dt']
			resp_onset = fixation_end
		elif variant == 'rt_go' or variant == 'rt_OIC':
			stim_onset = np.random.randint(self.fix_time, self.fix_time+800, par['trials_per_seq'])//par['dt']
			stim_off = par['num_time_steps']
			fixation_end = np.ones(par['trials_per_seq'],dtype=np.int16)*par['num_time_steps']
			resp_onset = stim_onset
		elif variant == 'dly_go':
			stim_onset = self.fix_time//par['dt']*np.ones((par['trials_per_seq']),dtype=np.int16)
			stim_off = (self.fix_time+300)//par['dt']
			fixation_end = stim_off + np.random.choice(self.go_delay, size=par['trials_per_seq'])
			resp_onset = fixation_end
		else:
			raise Exception('Bad task variant.')

		# Need dead time
		self.trial_info['train_mask'][:par['dead_time']//par['dt'], :] = 0

		for b in range(par['trials_per_seq']):

			t0 = par['num_time_steps']*b
			t1 = par['num_time_steps']*(b+1)

			# Input neurons index above par['num_motion_tuned'] encode fixation
			self.trial_info['neural_input'][t0:t0+fixation_end[b], self.trial_num, par['num_motion_tuned']:par['num_motion_tuned']+par['num_fix_tuned']] \
				+= par['tuning_height']
			"""
			self.trial_info['neural_input'][:fixation_end[b], self.trial_num, par['num_motion_tuned']:par['num_motion_tuned']+par['num_fix_tuned']] \
				+= par['tuning_height']
			"""
			modality   = np.random.randint(2)
			neuron_ind = range(self.modality_size*modality, self.modality_size*(1+modality))
			stim_dir   = np.random.choice(self.motion_dirs)
			if 'OIC' in variant:
				stim_index = int(np.round(par['num_motion_dirs']*(stim_dir+offset)/(2*np.pi)))
				stimulus_category_one = (stim_index in \
					list(np.arange(cat_boundary,cat_boundary+par['num_motion_dirs']//2)%par['num_motion_dirs']))
				if stimulus_category_one:
					target_ind = int(np.round(par['num_motion_dirs']*(offset)/(2*np.pi))%par['num_motion_dirs'])
				else:
					target_ind = int(np.round(par['num_motion_dirs']*(offset+np.pi)/(2*np.pi))%par['num_motion_dirs'])
			else:
				target_ind = int(np.round(par['num_motion_dirs']*(stim_dir+offset)/(2*np.pi))%par['num_motion_dirs'])

			#print(b, stim_dir, stim_index, stimulus_category_one, target_ind)
			#print('stim_dir', stim_dir)

			self.trial_info['neural_input'][t0+stim_onset[b]:t0+stim_off, self.trial_num, neuron_ind] += np.reshape(self.circ_tuning(stim_dir),(1,-1))
			self.trial_info['desired_output'][t0+resp_onset[b]:t1, self.trial_num, target_ind] = 1
			self.trial_info['desired_output'][t0:t0+resp_onset[b], self.trial_num, -1] = 1

			self.trial_info['train_mask'][t0+resp_onset[b]:t0+resp_onset[b]+par['mask_duration']//par['dt'], self.trial_num] = 0

		return self.trial_info

	def task_dm(self, variant='dm1', offset = 0):

		# Create trial stimuli
		stim_dir1 = np.random.choice(self.motion_dirs, [1, par['trials_per_seq']])
		stim_dir2 = (stim_dir1 + np.pi/2 + np.random.choice(self.motion_dirs[::2], [1, par['trials_per_seq']])/2)%(2*np.pi)

		stim1 = self.circ_tuning(stim_dir1)
		stim2 = self.circ_tuning(stim_dir2)

		# Determine the strengths of the stimuli in each modality
		c_mod1 = np.random.choice(self.dm_c_set, [1, par['trials_per_seq']])
		c_mod2 = np.random.choice(self.dm_c_set, [1, par['trials_per_seq']])
		mean_gamma = 0.8 + 0.4*np.random.rand(1, par['trials_per_seq'])
		gamma_s1_m1 = mean_gamma + c_mod1
		gamma_s2_m1 = mean_gamma - c_mod1
		gamma_s1_m2 = mean_gamma + c_mod2
		gamma_s2_m2 = mean_gamma - c_mod2

		# Determine response directions and convert to output indices
		resp_dir_mod1 = np.where(gamma_s1_m1 > gamma_s2_m1, stim_dir1, stim_dir2)
		resp_dir_mod2 = np.where(gamma_s1_m2 > gamma_s2_m2, stim_dir1, stim_dir2)
		resp_dir_sum  = np.where(gamma_s1_m1 + gamma_s1_m2 > gamma_s2_m1 + gamma_s2_m2, stim_dir1, stim_dir2)

		resp_dir_mod1 = np.round(par['num_motion_dirs']*resp_dir_mod1/(2*np.pi))
		resp_dir_mod2 = np.round(par['num_motion_dirs']*resp_dir_mod2/(2*np.pi))
		resp_dir_sum  = np.round(par['num_motion_dirs']*resp_dir_sum/(2*np.pi))

		# Apply stimuli to modalities and build appropriate response
		if variant == 'dm1':
			modality1 = gamma_s1_m1*stim1 + gamma_s2_m1*stim2
			modality2 = np.zeros_like(stim1)
			resp_dirs = resp_dir_mod1
		elif variant == 'dm2':
			modality1 = np.zeros_like(stim1)
			modality2 = gamma_s1_m2*stim1 + gamma_s2_m2*stim2
			resp_dirs = resp_dir_mod2
		elif variant == 'ctx_dm1':
			modality1 = gamma_s1_m1*stim1 + gamma_s2_m1*stim2
			modality2 = gamma_s1_m2*stim1 + gamma_s2_m2*stim2
			resp_dirs = resp_dir_mod1
		elif variant == 'ctx_dm2':
			modality1 = gamma_s1_m1*stim1 + gamma_s2_m1*stim2
			modality2 = gamma_s1_m2*stim1 + gamma_s2_m2*stim2
			resp_dirs = resp_dir_mod2
		elif variant == 'multsen_dm':
			modality1 = gamma_s1_m1*stim1 + gamma_s2_m1*stim2
			modality2 = gamma_s1_m2*stim1 + gamma_s2_m2*stim2
			resp_dirs = resp_dir_sum
		else:
			raise Exception('Bad task variant.')

		resp_dirs = np.int8(np.round(par['num_motion_dirs']*(resp_dirs+offset)/(2*np.pi))%par['num_motion_dirs'])

		resp = np.zeros([par['num_motion_dirs'], par['trials_per_seq']])
		for b in range(par['trials_per_seq']):
			resp[np.int16(resp_dirs[0,b]%par['num_motion_dirs']),b] = 1

		# Setting up arrays
		fixation = np.zeros(self.fixation_shape)
		response = np.zeros(self.response_shape)
		stimulus = np.zeros(self.stimulus_shape)
		mask     = np.ones(self.mask_shape)
		mask[:par['dead_time']//par['dt'],:] = 0
		resp_fix  = np.copy(fixation[:,:,0:1])

		# Identify stimulus onset for each trial and build each trial from there
		stim_onset = self.fix_time//par['dt']
		stim_off   = stim_onset + np.random.choice(self.dm_stim_lengths, par['trials_per_seq'])

		for b in range(par['trials_per_seq']):
			t0 = par['num_time_steps']*b
			t1 = par['num_time_steps']*(b+1)

			fixation[t0:t0+stim_off[b],self.trial_num,:] = par['tuning_height']
			resp_fix[t0:t0+stim_off[b],self.trial_num] = 1
			#stimulus[t0+stim_onset:t0+stim_off[b],self.trial_num,:] = np.transpose(np.concatenate([modality1[:,b], modality2[:,b]], axis=0)[:,np.newaxis])

			# TEMP CHANGE SO NO WORKING MEMORY REQUIRED
			stimulus[t0+stim_onset:t1,self.trial_num,:] = np.transpose(np.concatenate([modality1[:,b], modality2[:,b]], axis=0)[:,np.newaxis])

			response[t0+stim_off[b]:t1,self.trial_num,:] = np.transpose(resp[:,b,np.newaxis])
			mask[t0+stim_off[b]:t0+stim_off[b]+par['mask_duration']//par['dt'],self.trial_num] = 0

		# Merge activies and fixations into single vector
		stimulus = np.concatenate([stimulus, fixation], axis=2)
		response = np.concatenate([response, resp_fix], axis=2)

		self.trial_info['neural_input'][:,:,:par['num_motion_tuned']+par['num_fix_tuned']] += stimulus
		self.trial_info['desired_output'] = response
		self.trial_info['train_mask'] = mask

		return self.trial_info

	def task_dm_dly(self, variant='dm1', offset = 0):

		# Create trial stimuli
		stim_dir1 = 2*np.pi*np.random.rand(1, par['trials_per_seq'])
		stim_dir2 = (stim_dir1 + np.pi/2 + np.pi*np.random.rand(1, par['trials_per_seq']))%(2*np.pi)
		stim1 = self.circ_tuning(stim_dir1)
		stim2 = self.circ_tuning(stim_dir2)

		# Determine the strengths of the stimuli in each modality
		c_mod1 = np.random.choice(self.dm_dly_c_set, [1, par['trials_per_seq']])
		c_mod2 = np.random.choice(self.dm_dly_c_set, [1, par['trials_per_seq']])
		mean_gamma = 0.8 + 0.4*np.random.rand(1, par['trials_per_seq'])
		gamma_s1_m1 = mean_gamma + c_mod1
		gamma_s2_m1 = mean_gamma - c_mod1
		gamma_s1_m2 = mean_gamma + c_mod2
		gamma_s2_m2 = mean_gamma - c_mod2

		# Determine the delay for each trial
		delay = np.random.choice(self.dm_dly_delay, [1, par['trials_per_seq']])

		# Determine response directions and convert to output indices
		resp_dir_mod1 = np.where(gamma_s1_m1 > gamma_s2_m1, stim_dir1, stim_dir2)
		resp_dir_mod2 = np.where(gamma_s1_m2 > gamma_s2_m2, stim_dir1, stim_dir2)
		resp_dir_sum  = np.where(gamma_s1_m1 + gamma_s1_m2 > gamma_s2_m1 + gamma_s2_m2, stim_dir1, stim_dir2)

		resp_dir_mod1 = np.round(par['num_motion_dirs']*resp_dir_mod1/(2*np.pi))
		resp_dir_mod2 = np.round(par['num_motion_dirs']*resp_dir_mod2/(2*np.pi))
		resp_dir_sum  = np.round(par['num_motion_dirs']*resp_dir_sum/(2*np.pi))

		# Apply stimuli to modalities and build appropriate response
		if variant == 'dm1_dly':
			modality1_t1 = gamma_s1_m1*stim1
			modality2_t1 = np.zeros_like(stim1)
			modality1_t2 = gamma_s2_m1*stim2
			modality2_t2 = np.zeros_like(stim2)
			resp_dirs = resp_dir_mod1
		elif variant == 'dm2_dly':
			modality1_t1 = np.zeros_like(stim1)
			modality2_t1 = gamma_s1_m2*stim1
			modality1_t2 = np.zeros_like(stim2)
			modality2_t2 = gamma_s2_m2*stim2
			resp_dirs = resp_dir_mod2
		elif variant == 'ctx_dm1_dly':
			modality1_t1 = gamma_s1_m1*stim1
			modality2_t1 = gamma_s1_m2*stim1
			modality1_t2 = gamma_s2_m1*stim2
			modality2_t2 = gamma_s2_m2*stim2
			resp_dirs = resp_dir_mod1
		elif variant == 'ctx_dm2_dly':
			modality1_t1 = gamma_s1_m1*stim1
			modality2_t1 = gamma_s1_m2*stim1
			modality1_t2 = gamma_s2_m1*stim2
			modality2_t2 = gamma_s2_m2*stim2
			resp_dirs = resp_dir_mod2
		elif variant == 'multsen_dm_dly':
			modality1_t1 = gamma_s1_m1*stim1
			modality2_t1 = gamma_s1_m2*stim1
			modality1_t2 = gamma_s2_m1*stim2
			modality2_t2 = gamma_s2_m2*stim2
			resp_dirs = resp_dir_sum
		else:
			raise Exception('Bad task variant.')

		resp_dirs = np.int8(np.round(par['num_motion_dirs']*(resp_dirs+offset)/(2*np.pi))%par['num_motion_dirs'])

		resp = np.zeros([par['num_motion_dirs'], par['trials_per_seq']])
		for b in range(par['trials_per_seq']):
			resp[np.int16(resp_dirs[0,b]%par['num_motion_dirs']),b] = 1

		# Setting up arrays
		fixation = np.zeros(self.fixation_shape)
		response = np.zeros(self.response_shape)
		stimulus = np.zeros(self.stimulus_shape)
		mask     = np.ones(self.mask_shape)
		mask[:par['dead_time']//par['dt'],:] = 0
		resp_fix  = np.copy(fixation[:,:,0:1])

		# Identify stimulus onset for each trial and build each trial from there
		stim_on1   = self.fix_time//par['dt']
		stim_off1  = (self.fix_time+300)//par['dt']
		stim_on2   = delay + stim_off1
		stim_off2  = par['num_time_steps']
		resp_time  = stim_on2
		for b in range(par['trials_per_seq']):
			t0 = par['num_time_steps']*b
			t1 = par['num_time_steps']*(b+1)

			fixation[t0:t0+resp_time[0,b],self.trial_num,:] = par['tuning_height']
			resp_fix[t0:t0+resp_time[0,b],self.trial_num] = 1
			stimulus[t0+stim_on1:t0+stim_off1,self.trial_num,:] = np.concatenate([modality1_t1[:,b], modality2_t1[:,b]], axis=0)[np.newaxis,:]
			stimulus[t0+stim_on2[0,b]:t1,self.trial_num,:] = np.concatenate([modality1_t2[:,b], modality2_t2[:,b]], axis=0)[np.newaxis,:]
			response[t0+resp_time[0,b]:t1,self.trial_num,:] = resp[np.newaxis,:,b]
			mask[t0+resp_time[0,b]:t0+resp_time[0,b]+par['mask_duration'],self.trial_num] = 0

		# Merge activies and fixations into single vectors
		stimulus = np.concatenate([stimulus, fixation], axis=2)
		response = np.concatenate([response, resp_fix], axis=2)    # Duplicates starting fixation on output

		self.trial_info['neural_input'][:,:,:par['num_motion_tuned']+par['num_fix_tuned']] += stimulus
		self.trial_info['desired_output'] = response
		self.trial_info['train_mask'] = mask

		return self.trial_info

	def task_matching(self, variant='dms', offset = 0):

		# Determine matches, and get stimuli
		if variant in ['dms', 'dnms']:
			stim1 = np.random.choice(self.motion_dirs, par['trials_per_seq'])
			nonmatch = (stim1 + np.random.choice(self.motion_dirs[1:], par['trials_per_seq']))%(2*np.pi)

			match = np.random.choice(np.array([True, False]), par['trials_per_seq'])
			stim2 = np.where(match, stim1, nonmatch)

		elif variant in ['dmc', 'dnmc']:
			stim1 = np.random.choice(self.motion_dirs, par['trials_per_seq'])
			stim2 = np.random.choice(self.motion_dirs, par['trials_per_seq'])

			stim1_cat = np.logical_and(np.less(-1e-3, stim1), np.less(stim1, np.pi))
			stim2_cat = np.logical_and(np.less(-1e-3, stim2), np.less(stim2, np.pi))
			match = np.logical_not(np.logical_xor(stim1_cat, stim2_cat))
		else:
			raise Exception('Bad variant.')

		# Establishing stimuli
		stimulus1 = self.circ_tuning(stim1)
		stimulus2 = self.circ_tuning(stim2)

		# Convert to response
		stim1_int = np.round(par['num_motion_dirs']*stim1/(2*np.pi))
		stim2_int = np.round(par['num_motion_dirs']*stim2/(2*np.pi))

		# Saccade targets for match and non-match
		r0 = np.int8(np.round(par['num_motion_dirs']*offset/(2*np.pi))%par['num_motion_dirs'])
		r1 = (r0+par['num_motion_dirs']//2)%par['num_motion_dirs']

		if variant in ['dms', 'dmc']:
			resp = np.where(match, r0, r1)#stim1_int, -1)
		else:
			raise Exception('Bad variant.')

		# Setting up arrays
		modality_choice = np.random.choice(np.array([0,1], dtype=np.int16), [2, par['trials_per_seq']])
		modalities = np.zeros([2, par['trials_per_seq']*par['num_time_steps'], par['batch_size'], par['num_motion_tuned']//2])
		fixation = np.zeros(self.fixation_shape)
		response = np.zeros(self.response_shape)
		stimulus = np.zeros(self.stimulus_shape)
		mask     = np.ones(self.mask_shape)
		mask[:par['dead_time']//par['dt'],:] = 0

		# Decide timings and build each trial
		stim1_on  = self.fix_time//par['dt']
		stim1_off = (self.fix_time+300)//par['dt']
		stim2_on  = stim1_off + np.random.choice(self.match_delay, par['trials_per_seq'])
		stim2_off = par['num_time_steps']
		resp_time = stim2_on
		resp_fix  = np.copy(fixation[:,:,0:1])

		for b in range(par['trials_per_seq']):
			t0 = par['num_time_steps']*b
			t1 = par['num_time_steps']*(b+1)

			fixation[t0:t0+resp_time[b],self.trial_num,:] = par['tuning_height']

			# Ensuring that sample and test stimuli are in same modality (RF)
			modalities[modality_choice[0,b],t0+stim1_on:t0+stim1_off,self.trial_num,:] = stimulus1[np.newaxis,:,b]
			modalities[modality_choice[0,b],t0+stim2_on[b]:t1,self.trial_num,:] = stimulus2[np.newaxis,:,b]

			mask[t0+resp_time[b]:t0+resp_time[b]+par['mask_duration']//par['dt'],self.trial_num] = 0
			if not resp[b] == -1:
				response[t0+resp_time[b]:t1,self.trial_num,int(resp[b])] = 1
				resp_fix[t0:t0+resp_time[b],self.trial_num] = 1
			else:
				resp_fix[t0:t1,b,:] = 1

		# Merge activies and fixations into single vectors)
		#print(modalities.shape, fixation.shape)
		stimulus = np.concatenate([modalities[0], modalities[1], fixation], axis=2)
		response = np.concatenate([response, resp_fix], axis=2)

		self.trial_info['neural_input'][:,:,:par['num_motion_tuned']+par['num_fix_tuned']] += stimulus
		self.trial_info['desired_output'] = response
		self.trial_info['train_mask'] = mask

		return self.trial_info

if __name__ == '__main__':
	print('Testing stimulus generation.')
	s = Stimulus()
	s.generate_trial()

ext: py

max_stars_repo_name: gdgrant/Associative-Transfer

max_issues_repo_name: gdgrant/Associative-Transfer

max_forks_repo_name: gdgrant/Associative-Transfer

reason: import numpy,import scipy


text:
#setup: import numpy as np;lx,ly=(2**7,2**7);u=np.zeros([lx,ly],dtype=np.double);u[lx//2,ly//2]=1000.0;tempU=np.zeros([lx,ly],dtype=np.double)
#run: diffusion(u,tempU,100)

#pythran export diffusion(float [][], float [][], int)
import numpy as np

def diffusion(u, tempU, iterNum):
    """
    Apply Numpy matrix for the Forward-Euler Approximation
    """
    mu = .1

    for n in range(iterNum):
        tempU[1:-1, 1:-1] = u[1:-1, 1:-1] + mu * (
            u[2:, 1:-1] - 2 * u[1:-1, 1:-1] + u[0:-2, 1:-1] +
            u[1:-1, 2:] - 2 * u[1:-1, 1:-1] + u[1:-1, 0:-2])
        u[:, :] = tempU[:, :]
        tempU[:, :] = 0.0

max_stars_repo_name: adriendelsalle/numpy-benchmarks

max_issues_repo_name: adriendelsalle/numpy-benchmarks

max_forks_repo_name: adriendelsalle/numpy-benchmarks

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Dec  3 12:03:49 2020

@author: Kyungmin Lee
"""

# R / Week3 Assignment

import warnings
warnings.filterwarnings("ignore")

import cartopy
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

help(ccrs.PlateCarree())

# 1st figure
plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
# Above we declare our axes to be of the ccrs.PlateCarree()
#(which is just latitude/longitude) projection.
# On those axes defined in that projection, we can add coastlines(),
#another cartopy function with built in coastline data.

# 2nd figure
plt.figure()
ax = plt.axes(projection=ccrs.Mollweide())
ax.stock_img()
# Above we declare our axes to be of ccrs.Mollweide() projection.
# We use cartopy's stock_img() function to fill the axes
# with a quasi-true color stock image from cartopy.

# 3rd figure
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61

plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='blue', linewidth=2, marker='o',
         transform=ccrs.Geodetic(),
         )
plt.text(ny_lon - 3, ny_lat - 12, 'New York',
         horizontalalignment='right',
         transform=ccrs.Geodetic())

plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi',
         horizontalalignment='left',
         transform=ccrs.Geodetic())

# 4th figure
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61

plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='gray', linestyle='--',
         transform=ccrs.PlateCarree(),
         )

plt.text(ny_lon - 3, ny_lat - 12, 'New York',
         horizontalalignment='right',
         transform=ccrs.Geodetic())

plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi',
         horizontalalignment='left',
         transform=ccrs.Geodetic())
# Notice the difference in how the dots are connected.
# It all comes down the the `transform` argument within the `plt.plot` .
# We are declaring different data projections even though they're both just points.

#By default, the coordinate system of any data added to a GeoAxes is the same
# as the coordinate system of the GeoAxes itself, to control which coordinate system
# that the given data is in, you can add the transform keyword with an appropriate cartopy.crs.CRS instance

# 3rd figure ver2
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61

plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='blue', linewidth=2, marker='o',
         transform=ccrs.Geodetic(),
         )
plt.text(ny_lon - 3, ny_lat - 12, 'Delaware',
         horizontalalignment='right',
         transform=ccrs.Geodetic())

plt.text(delhi_lon + 3, delhi_lat - 12, 'Seoul',
         horizontalalignment='left',
         transform=ccrs.Geodetic())

# 4th figure ver2
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61

plt.figure()
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='gray', linestyle='--',
         transform=ccrs.PlateCarree(),
         )

plt.text(ny_lon - 3, ny_lat - 12, 'Delaware',
         horizontalalignment='right',
         transform=ccrs.Geodetic())

plt.text(delhi_lon + 3, delhi_lat - 12, 'Seoul',
         horizontalalignment='left',
         transform=ccrs.Geodetic())

# Cartopy Fundamentals - Features (cfeatures)

# 5th figure
import cartopy.feature as cf

plt.figure()
ax = plt.axes(projection = ccrs.LambertConformal())
ax.add_feature(cf.COASTLINE)
ax.set_title("Title")

# 6th figure
import numpy as np
import cartopy.feature as cf

central_lat = 37.5
central_lon = -96
extent = [-120, -70, 24, 50.5]
central_lon = np.mean(extent[:2])
central_lat = np.mean(extent[2:])

plt.figure(figsize=(12, 6))
ax = plt.axes(projection=ccrs.AlbersEqualArea(central_lon, central_lat))
ax.set_extent(extent)

ax.add_feature(cf.OCEAN)
ax.add_feature(cf.LAND, edgecolor='black')
ax.add_feature(cf.LAKES, edgecolor='black')
ax.add_feature(cf.RIVERS)
ax.gridlines()

# Hurricane Katrina Path

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import shapely.geometry as sgeom

import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader

#HURDAT2 dataset from AOML/NOAA:
#http://www.aoml.noaa.gov/hrd/hurdat/newhurdat-all.html on 14th Dec 2012.

lons = [-75.1, -75.7, -76.2, -76.5, -76.9, -77.7, -78.4, -79.0,
        -79.6, -80.1, -80.3, -81.3, -82.0, -82.6, -83.3, -84.0,
        -84.7, -85.3, -85.9, -86.7, -87.7, -88.6, -89.2, -89.6,
        -89.6, -89.6, -89.6, -89.6, -89.1, -88.6, -88.0, -87.0,
        -85.3, -82.9]

lats = [23.1, 23.4, 23.8, 24.5, 25.4, 26.0, 26.1, 26.2, 26.2, 26.0,
        25.9, 25.4, 25.1, 24.9, 24.6, 24.4, 24.4, 24.5, 24.8, 25.2,
        25.7, 26.3, 27.2, 28.2, 29.3, 29.5, 30.2, 31.1, 32.6, 34.1,
        35.6, 37.0, 38.6, 40.1]

# Define a plotting function
#that will vary the color of particular states
#given the following conditions
def colorize_state(geometry):
    facecolor = (0.9375, 0.9375, 0.859375)
    if geometry.intersects(track):
        facecolor = 'red'
    elif geometry.intersects(track_buffer):
        facecolor = '#FF7E00'
    return {'facecolor': facecolor, 'edgecolor': 'black'}

#Note that this function will be called below to help color states

# 7th figure

# Chunk 1
fig = plt.figure(figsize=(12,8))
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())

shapename = 'admin_1_states_provinces_lakes_shp'
states_shp = shpreader.natural_earth(resolution='110m',
                                     category='cultural', name=shapename)

# Chunk 2
ax.background_patch.set_visible(False)
ax.outline_patch.set_visible(False)
ax.set_title('US States which intersect the track of '
             'Hurricane Katrina (2005)')

# Chunk 3
track = sgeom.LineString(zip(lons, lats))
track_buffer = track.buffer(2)

# Chunk 4
ax.add_geometries(
    shpreader.Reader(states_shp).geometries(),
    ccrs.PlateCarree(),
    styler=colorize_state)

ax.add_geometries([track_buffer], ccrs.PlateCarree(),
                  facecolor='#C8A2C8', alpha=0.5)
ax.add_geometries([track], ccrs.PlateCarree(),
                  facecolor='none', edgecolor='k')

# Chunk 5
direct_hit = mpatches.Rectangle((0, 0), 1, 1, facecolor="red")
within_2_deg = mpatches.Rectangle((0, 0), 1, 1, facecolor="#FF7E00")
labels = ['State directly intersects\nwith track',
          'State is within \n2 degrees of track']
ax.legend([direct_hit, within_2_deg], labels,
          loc='lower left', bbox_to_anchor=(0.025, -0.1), fancybox=True)

#Adding data to a cartopy plot
import numpy as np

#Let's create some sample data and plot a contour plot
# 8th figure
lon = np.linspace(-80, 80, 25)
lat = np.linspace(30, 70, 25)
lon2d, lat2d = np.meshgrid(lon, lat)
data = np.cos(np.deg2rad(lat2d) * 4) + np.sin(np.deg2rad(lon2d) * 4)

plt.figure(figsize=(10,6))
plt.contourf(lon2d, lat2d, data)

#Now let's overlay this onto a cartopy plot with geospatial axes
# 9th figure
plt.figure(figsize=(12,8))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_global()
ax.coastlines()
ax.contourf(lon, lat, data)

#We created a PlateCarree projection
#and plot the data on it without any transform keyword.
#This happens to work because PlateCarree is the simplest projection of lat / lon data.

#Try this with a more complex projection
#10th figure
projection = ccrs.RotatedPole(pole_longitude=-177.5, pole_latitude=37.5)

plt.figure(figsize=(12,8))
ax = plt.axes(projection=projection)
ax.set_global()
ax.coastlines()
ax.contourf(lon, lat, data)

#This is wrong! why?
#Because we need to add in the transform argument because our data comes in a different projection.
#You can plot in whatever projection you want so long as the data is transformed to the projection of the plot!

#11th figure
projection = ccrs.RotatedPole(pole_longitude=-177.5, pole_latitude=37.5)

plt.figure(figsize=(12,8))
ax = plt.axes(projection=projection)
ax.set_global()
ax.coastlines()
ax.contourf(lon, lat, data, transform=ccrs.PlateCarree())

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

# load in a built-in tutorial dataset from xarray
ds = xr.tutorial.load_dataset('air_temperature')

ds

# convert this to fahrenheit
ds['air'].values = (ds['air'].values - 273.15)*(9/5) + 32

# select one time slice - create separate object and call it airtemp
# 12th figure
airtemp = ds.air.sel(time='2013-01-01 12:00', method='nearest')

fig = plt.figure(figsize=(16,10))
ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines()
ax.gridlines()
airtemp.plot(ax=ax, transform=ccrs.PlateCarree(),
         vmin=20, vmax=50, cbar_kwargs={'shrink': 0.4}) # cbar_kwargs={'shrink': 0.4} just shrinks the colorbar

#Note that I know the initial dataset comes in ccrs.PlateCarree(),
#hence the transform=ccrs.PlateCarree() argument .
#We'll learn how to get python to tell us what the projection is in the future

#Let's choose a better colormap instead of blue green yellow

#13th figure
from matplotlib import cm #import colormap module from the matplotlib library

new_cmap = cm.get_cmap('YlOrRd') # Yellow orange red colormap

fig = plt.figure(figsize=(16,10))
ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines()
ax.gridlines()
airtemp.plot(ax=ax, transform=ccrs.PlateCarree(),
         vmin=20, vmax=50, cbar_kwargs={'shrink': 0.4}, cmap=new_cmap) # notice the cmap argument

# Cartopy assignment
#1) Download treecov.nc from the datasets folder (https://github.com/jsimkins2/geog473-673/blob/master/datasets/treecov.nc)

#2) Use xarray to open dataset (xr.open_dataset).

import xarray as xr

treeData = xr.open_dataset("/Users/x-note/Downloads/treecov.nc")
treeData

#3) Remove bad values (values below 0%). (hint: xarray.where() function)

#4) Plot TreeCover percentage of North America with a green colormap¶

#5) Plot as ccrs.LambertConformal() (treecov.nc comes as ccrs.PlateCarree() )

#6) Submit resulting plot to Canvas - Cartopy Assignment

max_stars_repo_name: KLeeDE/Python

max_issues_repo_name: KLeeDE/Python

max_forks_repo_name: KLeeDE/Python


text:
# Copyright 2019-2021 Cambridge Quantum Computing
#
# 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.

from pathlib import Path

import pytest  # type: ignore
from pytket import Circuit
from pytket.quipper import circuit_from_quipper
from pytket.transform import Transform  # type: ignore
from pytket.utils.results import compare_unitaries
import numpy as np
import os
from typing import Any

curr_file_path = Path(__file__).resolve().parent

def approx_equal_up_to_phase(s0: np.ndarray, s1: np.ndarray, eps: float) -> bool:
    d = np.vdot(s0, s1)  # type: ignore
    return bool(abs(abs(d) - 1) < eps)

def unitary_from_simulate_output(simout: Any, n: int) -> np.ndarray:
    """
    Convert text output by Quipper's `Simulate' program to a unitary matrix.
    `n' is the number of qubits. `simout' is the output of `Simulate'.
    The encoding is little-endian.
    """
    N = pow(2, n)
    fmt = "{0:0%db}" % n
    reps = ["|" + fmt.format(i) + ">" for i in range(N)]
    lines = simout.split("\n")
    n_lines = len(lines)
    pos = 0
    a = np.zeros((N, N), dtype=complex)
    for i in range(N):
        assert lines[pos].startswith(reps[i] + " ->")
        pos += 1
        for j in range(N):
            line = lines[pos]
            if line.startswith(" "):
                line0 = lines[pos].strip()
                if line0.startswith(reps[j] + " "):
                    valstr = (
                        line0[n + 3 :]
                        .replace(" ", "")
                        .replace("*", "")
                        .replace("i", "j")
                    )
                    a[j][i] = complex(valstr)
                    pos += 1
    return np.array(a)

def test_quipper_1() -> None:
    circ = circuit_from_quipper(
        str(curr_file_path / "quipper_test_files" / "test1.quip")
    )
    assert circ.n_qubits == 16

def test_quipper_2() -> None:
    circ = circuit_from_quipper(
        str(curr_file_path / "quipper_test_files" / "test2.quip")
    )
    assert circ.n_qubits == 4

def test_quipper_3() -> None:
    # This circuit should be the identity (up to phase).
    circ = circuit_from_quipper(
        str(curr_file_path / "quipper_test_files" / "test3.quip")
    )
    assert circ.n_qubits == 4
    Transform.OptimisePostRouting().apply(circ)
    circ0 = Circuit(4, 0)
    s = circ.get_statevector()
    s0 = circ0.get_statevector()
    assert approx_equal_up_to_phase(s, s0, 1e-10)

def test_quipper_4() -> None:
    # Check some test circuits against simulator output.
    for i in range(11):
        fname = "test4-%d.quip" % i
        fpath = str(curr_file_path / "quipper_test_files" / fname)
        circ = circuit_from_quipper(fpath)
        n_qubits = circ.n_qubits
        Transform.DecomposeBoxes().apply(circ)
        Transform.RebaseToTket().apply(circ)
        a = circ.get_unitary()
        with open(fpath + ".simout") as f:
            simout = f.read()
        b = unitary_from_simulate_output(simout, n_qubits)
        # The unitary matrices a and b should be equal up to a phase.
        assert compare_unitaries(a, b)

def test_quipper_5() -> None:
    # Invalid operation (CCH gate).
    with pytest.raises(NotImplementedError):
        circ = circuit_from_quipper(
            str(curr_file_path / "quipper_test_files" / "test5.quip")
        )


text:
from __future__ import print_function
import numpy as np

from pyNastran.bdf.cards.coordinate_systems import CORD2R
from pyNastran.bdf.mesh_utils.cut_model_by_plane import _p1_p2_zaxis_to_cord2r
from pyNastran.bdf.mesh_utils.cutting_plane_plotter import cut_and_plot_model

from pyNastran.gui.menus.cutting_plane.cutting_plane import CuttingPlaneWindow
from pyNastran.gui.qt_files.colors import PURPLE_FLOAT

class CuttingPlaneObject(object):
    def __init__(self, gui):
        self.gui = gui
        self._cutting_plane_window_shown = False
        self._cutting_plane_window = None

    def set_font_size(self, font_size):
        """sets the font size for the preferences window"""
        if self._cutting_plane_window_shown:
            self._cutting_plane_window.set_font_size(font_size)

    def set_cutting_plane_menu(self):
        """
        Opens a dialog box to set:

        +--------+----------+
        |  Max   |  Float   |
        +--------+----------+
        """
        #if not hasattr(self, 'case_keys'):  # TODO: maybe include...
            #self.log_error('No model has been loaded.')
            #return

        camera = self.gui.GetCamera()
        #min_clip, max_clip = camera.GetClippingRange()
        settings = self.gui.settings
        model_name = self.gui.name
        model = self.gui.models[model_name]
        if hasattr(model, 'coords'):
            cids = list(model.coords.keys())
            cids.sort()
        else:
            cids = [0]

        data = {
            'model_name' : model_name,
            'font_size' : settings.font_size,
            'cids' : cids,
            'plane_color' : (1., 0., 1.), # purple
            'name' : model_name,

            'clicked_ok' : False,
            'close' : False,
        }
        if not self._cutting_plane_window_shown:
            self._cutting_plane_window = CuttingPlaneWindow(data, win_parent=self.gui)
            self._cutting_plane_window.show()
            self._cutting_plane_window = True
            self._cutting_plane_window.exec_()
        else:
            self._cutting_plane_window.activateWindow()

        if data['close']:
            #if not self._cutting_plane_window._updated_preference:
            #    settings.on_set_font_size(data['font_size'])
            del self._cutting_plane_window
            if hasattr(self.gui, 'plane_actor'):
                del self.gui.plane_actor
            self.gui.clear_actor('smt_plane')
            self._cutting_plane_window_shown = False
        else:
            self._cutting_plane_window.activateWindow()

    def make_cutting_plane_from_data(self, data):
        """Creates a cutting plane of the aero_model for the active plot result"""
        model_name = data['model_name']
        #model = self.models[model_name]

        cid_p1, p1 = data['p1']
        cid_p2, p2 = data['p2']
        cid_zaxis, zaxis = data['zaxis']
        ytol = data['ytol']
        zero_tol = data['zero_tol']
        plane_color = data['plane_color']
        method = data['method']
        plane_opacity = data['plane_opacity']
        csv_filename = None
        if 'csv_filename' in data:
            csv_filename = data['csv_filename']
        self.gui.make_cutting_plane(
            model_name,
            p1, p2, zaxis,
            method=method,
            cid_p1=cid_p1, cid_p2=cid_p2, cid_zaxis=cid_zaxis,
            ytol=ytol, plane_atol=zero_tol,
            plane_color=plane_color,
            plane_opacity=plane_opacity, csv_filename=csv_filename)

    def make_cutting_plane(self, model_name,
                           p1, p2, zaxis,
                           method='Z-Axis Projection',
                           cid_p1=0, cid_p2=0, cid_zaxis=0,
                           ytol=1., plane_atol=1e-5,
                           plane_color=None, plane_opacity=0.5,
                           csv_filename=None, show=True):
        """Creates a cutting plane of the aero_model for the active plot result"""
        if plane_color is None:
            plane_color = PURPLE_FLOAT
        assert len(plane_color) == 3, plane_color
        log = self.gui.log

        model = self.gui.models[model_name]
        class_name = model.__class__.__name__
        if class_name in ['BDF', 'OP2Geom']:
            out = model.get_displacement_index_xyz_cp_cd()
            unused_icd_transform, icp_transform, xyz_cp, nid_cp_cd = out
            nids = nid_cp_cd[:, 0]
            nid_cd = nid_cp_cd[:, [0, 2]]
            xyz_cid0 = model.transform_xyzcp_to_xyz_cid(
                xyz_cp, nids, icp_transform,
                cid=0)
        else:
            log.error('%r is not supported' % class_name)
            return

        #xyz_min, xyz_max = model.xyz_limits
        xyz_min = xyz_cid0.min(axis=0)
        xyz_max = xyz_cid0.max(axis=0)
        assert len(xyz_min) == 3, xyz_min

        dxyz = np.abs(xyz_max - xyz_min)
        dim_max = dxyz.max()
        izero = np.where(dxyz == 0)
        dxyz[izero] = dim_max

        xyz1, xyz2, unused_z_global, i, k, origin, zaxis, xzplane = _p1_p2_zaxis_to_cord2r(
            model, p1, p2, zaxis,
            cid_p1=cid_p1, cid_p2=cid_p2, cid_zaxis=cid_zaxis,
            method=method)

        plane_actor = self.gui._create_plane_actor_from_points(
            xyz1, xyz2, i, k, dim_max,
            actor_name='plane')
        prop = plane_actor.GetProperty()
        prop.SetColor(*plane_color)
        prop.SetOpacity(plane_opacity) # 0=transparent, 1=solid

        self.gui.rend.Render()

        try:
            # i/j/k vector is nan
            coord = CORD2R(1, rid=0, origin=origin, zaxis=zaxis, xzplane=xzplane,
                           comment='')
        except:
            log.error('The coordinate system is invalid; check your cutting plane.')
            return
        #print(coord)
        origin = coord.origin
        beta = coord.beta().T

        cid = ''
        self.gui.create_coordinate_system(
            cid, dim_max, label='%s' % cid, origin=origin,
            matrix_3x3=beta, coord_type='xyz')

        nnodes = xyz_cid0.shape[0]
        #nodal_result = np.linspace(0., 1., num=nnodes)

        #case = self.result_cases[self.icase_aero]
        #res_scalars, location = model.aero_raw_plot_result()
        (obj, (i, name)) = self.gui.result_cases[self.gui.icase_fringe]
        res_scalars = obj.get_scalar(i, name)
        location = obj.get_location(i, name)

        if res_scalars is None:
            if plane_actor is not None:
                plane_actor.VisibilityOn()
                #print(self.model_frame.plane_actor)
                #print(dir(self.model_frame.plane_actor))
                #plane_actor.VisibilityOff()
            log.error('No result is selected.')
            return

        if hasattr(obj, 'titles'):
            title = obj.titles[0]
        elif hasattr(obj, 'title'):
            title = obj.title
        else:
            raise NotImplementedError(obj)

        plane_actor.VisibilityOn()
        if location == 'centroid':
            if hasattr(model, 'map_centroidal_result'):
                nodal_result = model.map_centroidal_result(res_scalars)
            else:
                log.error('Centroidal results are not supported.')
                return
            #np.savetxt('Cp_centroid.csv', res_scalars, header='# Cp')
            #np.savetxt('Cp_nodal.csv', nodal_result, header='# Cp')
        else:
            nodal_result = res_scalars
        assert len(nodal_result) == nnodes

        invert_yaxis = False
        if title in ['Cp']:
            invert_yaxis = True

        cut_and_plot_model(title, p1, p2, zaxis,
                           model, coord, nodal_result,
                           log,
                           ytol,
                           plane_atol=plane_atol,
                           csv_filename=csv_filename,
                           invert_yaxis=invert_yaxis,
                           cut_type='edge', show=show)


text:
import os
from collections import defaultdict
import numpy as np
import timeit

class KnowledgeGraph:

    @property
    def num_triple_train(self):
        return self.__num_triple_train

    @property
    def num_triple_test(self):
        return self.__num_triple_test

    @property
    def num_entity(self):
        return self.__num_entity

    @property
    def num_relation(self):
        return self.__num_relation

    @property
    def hr_t(self):
        return self.__hr_t

    @property
    def tr_h(self):
        return self.__tr_h

    def __init__(self, data_dir, negative_sampling):
        self.__data_dir = data_dir
        self.__negative_sampling = negative_sampling

        self.__entity2id = {}
        self.__id2entity = {}
        self.__relation2id = {}
        self.__id2relation = {}
        self.__hr_t = defaultdict(set)
        self.__tr_h = defaultdict(set)

        self.__triple_train = []
        self.__triple_test = []
        self.__triple_valid = []

        self.__num_entity = 0
        self.__num_relation = 0
        self.__num_triple_train = 0
        self.__num_triple_test = 0
        self.__num_triple_valid = 0

        self.__load_data()
        print('finish preparing data. ')

    def get_training_data(self):
        n_triple = len(self.__triple_train)
        triple_set = set(self.__triple_train)
        rand_idx = np.random.permutation(n_triple)
        train_triple_positive = np.asarray([self.__triple_train[x] for x in rand_idx])
        train_triple_negative = []
        for t in train_triple_positive:
            if self.__negative_sampling == 'unif':
                replace_head_probability = 0.5
            elif self.__negative_sampling == 'bern':
                replace_head_probability = self.__relation_property[t[1]]
            else:
                raise Exception("Invalid negative_sampling : {}".format(self.__negative_sampling))

            while True:
                replace_entity_id = np.random.randint(self.__num_entity)
                random_num = np.random.random()

                if random_num < replace_head_probability:
                    triple = (replace_entity_id, t[1], t[2])
                else:
                    triple = (t[0], t[1], replace_entity_id)

                if triple not in triple_set:
                    train_triple_negative.append(list(triple))
                    break

        train_triple_negative = np.asarray(train_triple_negative)

        return train_triple_positive, train_triple_negative

    def get_test_data(self):
        return self.__triple_test

    def __load_triple(self, filename):
        triples = list()
        with open(os.path.join(self.__data_dir, filename)) as f:
            for line in f.readlines():
                elements = line.strip().split('\t')
                assert len(elements) == 3
                h = self.__entity2id[elements[0]]
                r = self.__relation2id[elements[2]]
                t = self.__entity2id[elements[1]]
                triples.append((h, r, t))
                self.__hr_t[(h, r)].add(t)
                self.__tr_h[(t, r)].add(h)
        return triples

    def __load_iddef(self, filename):
        name2id = dict()
        id2name = dict()
        with open(os.path.join(self.__data_dir, filename)) as f:
            for line in f.readlines():
                elements = line.strip().split('\t')
                assert len(elements) == 2
                name = elements[0]
                id = int(elements[1])
                name2id[name] = id
                id2name[id] = name
        return name2id, id2name

    def __load_data(self):
        self.__entity2id, self.__id2entity = self.__load_iddef('entity2id.txt')
        self.__relation2id, self.__id2relation = self.__load_iddef('relation2id.txt')

        self.__triple_train = self.__load_triple('train.txt')
        self.__triple_test = self.__load_triple('test.txt')
        self.__triple_valid = self.__load_triple('valid.txt')
        self.__triple = np.concatenate([self.__triple_train, self.__triple_test, self.__triple_valid], axis=0)

        self.__num_relation = len(self.__relation2id)
        self.__num_entity = len(self.__entity2id)
        self.__num_triple_train = len(self.__triple_train)
        self.__num_triple_test = len(self.__triple_test)
        self.__num_triple_valid = len(self.__triple_valid)

        if self.__negative_sampling == 'bern':
            self.__relation_property_head = {x: [] for x in
                                             range(self.__num_relation)}
            self.__relation_property_tail = {x: [] for x in
                                             range(self.__num_relation)}
            for (h, r, t) in self.__triple_train:
                self.__relation_property_head[r].append(h)
                self.__relation_property_tail[r].append(t)
            self.__relation_property = dict()
            for r in self.__relation_property_head.keys():
                num_heads = len(set(self.__relation_property_head[r]))
                num_tails = (len(set(self.__relation_property_tail[r])))
                ratio = num_tails / (num_heads + num_tails)
                self.__relation_property[r] = ratio


text:
#!/usr/local/greenplum-db-6.10.0/ext/python/bin/python
# coding=utf-8

from .gnn import FullGCN, SupervisedSAGE, SampledGCN, UnsupervisedSAGE
from .graph_embedding import DeepWalk, LINE
from multiprocessing import Process
from multiprocessing import Queue
from .utils import build_graph
import numpy as np

class SeedProcess(Process):
    def __init__(self,
                 name,
                 task_queue,
                 nodes_def,
                 edges_def,
                 node_type,
                 redis_host,
                 redis_port,
                 redis_passwd=None,
                 batch_size=32,
                 epochs=1,
                 **kwargs):
        super(SeedProcess, self).__init__(name=name)
        self._task_queue = task_queue
        self._node_type = node_type
        self._batch_size = batch_size
        self._epochs = epochs
        self._graph = build_graph(nodes_def, edges_def, redis_host, redis_port, redis_passwd, **kwargs)

    def run(self):
        gen = self._graph.V(self._node_type).shuffle(traverse=True).batch(self._batch_size).values()
        current_epoch = 0
        while True:
            try:
                seed = gen.next()
                ids, labels = seed.ids, seed.labels
                if labels is None:
                    labels = ids
                self._task_queue.put((ids, labels))
            except:
                if current_epoch < self._epochs:
                    current_epoch += 1
                else:
                    break

class SamplerProcess(Process):
    def __init__(self,
                 name,
                 task_queue,
                 result_queue,
                 nodes_def,
                 edges_def,
                 node_type,
                 redis_host,
                 redis_port,
                 redis_passwd=None,
                 timeout=1,
                 **kwargs):
        super(SamplerProcess, self).__init__(name=name)
        self._task_queue = task_queue
        self._result_queue = result_queue
        self._timeout = timeout
        self._graph = build_graph(nodes_def, edges_def, redis_host, redis_port, redis_passwd, **kwargs)
        self._node_type = node_type

    def _build_model(self):
        raise NotImplementedError("model build method function `_build_model` not implemented yet")

    def run(self):
        model = self._build_model()
        while True:
            try:
                ids, labels = self._task_queue.get(timeout=self._timeout)
            except Exception as e:
                break
            src_recept = model.receptive_fn(self._node_type, ids).flatten(spec=model.src_ego_spec)
            if model.mode == "predict":
                self._result_queue.put(((tuple(src_recept), ids), ids.astype(np.int32)))
            else:
                self._result_queue.put((tuple(src_recept), labels))

class FullGCNSampler(SamplerProcess):
    def __init__(self,
                 name,
                 task_queue,
                 result_queue,
                 nodes_def,
                 edges_def,
                 redis_host,
                 redis_port,
                 redis_passwd,
                 output_dim,
                 features_num,
                 node_type,
                 sample_paths,
                 categorical_attrs_desc=None,
                 hidden_dim=16,
                 hidden_act='relu',
                 in_drop_rate=.0,
                 use_input_bn=True,
                 need_dense=True,
                 dense_act=None,
                 mode="train",
                 timeout=1,
                 **kwargs
                 ):
        super(FullGCNSampler, self).__init__(
            name,
            task_queue,
            result_queue,
            nodes_def,
            edges_def,
            node_type,
            redis_host,
            redis_port,
            redis_passwd,
            timeout,
            **kwargs
        )
        self._nodes_def = nodes_def
        self._edges_def = edges_def
        self._output_dim = output_dim
        self._features_num = features_num
        self._node_type = node_type
        self._sample_paths = sample_paths
        self._categorical_attrs_desc = categorical_attrs_desc
        self._hidden_dim = hidden_dim
        self._hidden_act = hidden_act
        self._in_drop_rate = in_drop_rate
        self._use_input_bn = use_input_bn
        self._need_dense = need_dense
        self._dense_act = dense_act
        self._mode = mode

    def _build_model(self):
        return FullGCN(
            graph=self._graph,
            output_dim=self._output_dim,
            features_num=self._features_num,
            node_type=self._node_type,
            sample_paths=self._sample_paths,
            categorical_attrs_desc=self._categorical_attrs_desc,
            hidden_dim=self._hidden_dim,
            hidden_act=self._hidden_act,
            in_drop_rate=self._in_drop_rate,
            use_input_bn=self._use_input_bn,
            need_dense=self._need_dense,
            dense_act=self._dense_act,
            mode=self._mode
        )

class SampledGCNSampler(SamplerProcess):
    def __init__(self,
                 name,
                 task_queue,
                 result_queue,
                 nodes_def,
                 edges_def,
                 redis_host,
                 redis_port,
                 redis_passwd,
                 output_dim,
                 features_num,
                 node_type,
                 sample_paths,
                 neighs_num,
                 categorical_attrs_desc=None,
                 hidden_dim=16,
                 hidden_act='relu',
                 in_drop_rate=.0,
                 use_input_bn=True,
                 need_dense=True,
                 dense_act=None,
                 mode="train",
                 timeout=1,
                 **kwargs):
        super(SampledGCNSampler, self).__init__(
            name,
            task_queue,
            result_queue,
            nodes_def,
            edges_def,
            node_type,
            redis_host,
            redis_port,
            redis_passwd,
            timeout,
            **kwargs
        )
        self._nodes_def = nodes_def
        self._edges_def = edges_def
        self._output_dim = output_dim
        self._features_num = features_num
        self._node_type = node_type
        self._sample_paths = sample_paths
        self._neighs_num = neighs_num
        self._categorical_attrs_desc = categorical_attrs_desc
        self._hidden_dim = hidden_dim
        self._hidden_act = hidden_act
        self._in_drop_rate = in_drop_rate
        self._use_input_bn = use_input_bn
        self._need_dense = need_dense
        self._dense_act = dense_act
        self._mode = mode

    def _build_model(self):
        return SampledGCN(
            graph=self._graph,
            output_dim=self._output_dim,
            features_num=self._features_num,
            node_type=self._node_type,
            sample_paths=self._sample_paths,
            neighs_num=self._neighs_num,
            categorical_attrs_desc=self._categorical_attrs_desc,
            hidden_dim=self._hidden_dim,
            hidden_act=self._hidden_act,
            in_drop_rate=self._in_drop_rate,
            use_input_bn=self._use_input_bn,
            need_dense=self._need_dense,
            dense_act=self._dense_act,
            mode=self._mode
        )

class SupervisedSAGESampler(SamplerProcess):
    def __init__(self,
                 name,
                 task_queue,
                 result_queue,
                 nodes_def,
                 edges_def,
                 redis_host,
                 redis_port,
                 redis_passwd,
                 output_dim,
                 features_num,
                 node_type,
                 sample_paths,
                 neighs_num,
                 agg_type="gcn",
                 categorical_attrs_desc=None,
                 hidden_dim=16,
                 hidden_act='relu',
                 in_drop_rate=.0,
                 use_input_bn=True,
                 need_dense=True,
                 dense_act=None,
                 mode="train",
                 timeout=1,
                 **kwargs
                 ):
        super(SupervisedSAGESampler, self).__init__(
            name,
            task_queue,
            result_queue,
            nodes_def,
            edges_def,
            node_type,
            redis_host,
            redis_port,
            redis_passwd,
            timeout,
            **kwargs
        )
        self._nodes_def = nodes_def
        self._edges_def = edges_def
        self._output_dim = output_dim
        self._features_num = features_num
        self._node_type = node_type
        self._sample_paths = sample_paths
        self._neighs_num = neighs_num
        self._agg_type = agg_type
        self._categorical_attrs_desc = categorical_attrs_desc
        self._hidden_dim = hidden_dim
        self._hidden_act = hidden_act
        self._in_drop_rate = in_drop_rate
        self._use_input_bn = use_input_bn
        self._need_dense = need_dense
        self._dense_act = dense_act
        self._mode = mode

    def _build_model(self):
        return SupervisedSAGE(
            graph=self._graph,
            output_dim=self._output_dim,
            features_num=self._features_num,
            node_type=self._node_type,
            sample_paths=self._sample_paths,
            neighs_num=self._neighs_num,
            agg_type=self._agg_type,
            categorical_attrs_desc=self._categorical_attrs_desc,
            hidden_dim=self._hidden_dim,
            hidden_act=self._hidden_act,
            in_drop_rate=self._in_drop_rate,
            use_input_bn=self._use_input_bn,
            need_dense=self._need_dense,
            dense_act=self._dense_act,
            mode=self._mode
        )

class UnsupervisedSAGESampler(SamplerProcess):
    def __init__(self,
                 name,
                 task_queue,
                 result_queue,
                 nodes_def,
                 edges_def,
                 redis_host,
                 redis_port,
                 redis_passwd,
                 features_num,
                 node_type,
                 sample_paths,
                 positive_sample_path,
                 neighs_num,
                 neg_num=10,
                 agg_type="gcn",
                 categorical_attrs_desc=None,
                 hidden_dim=16,
                 hidden_act='relu',
                 in_drop_rate=.0,
                 use_input_bn=True,
                 need_dense=True,
                 dense_act=None,
                 mode="train",
                 timeout=1,
                 **kwargs
                 ):
        super(UnsupervisedSAGESampler, self).__init__(
            name,
            task_queue,
            result_queue,
            nodes_def,
            edges_def,
            node_type,
            redis_host,
            redis_port,
            redis_passwd,
            timeout,
            **kwargs
        )
        self._node_def = nodes_def
        self._edge_def = edges_def
        self._features_num = features_num
        self._node_type = node_type
        self._sample_paths = sample_paths
        self._positive_sample_path = positive_sample_path
        self._neighs_num = neighs_num
        self._neg_num = neg_num
        self._agg_type = agg_type
        self._categorical_attrs_desc = categorical_attrs_desc
        self._hidden_dim = hidden_dim
        self._hidden_act = hidden_act
        self._in_drop_rate = in_drop_rate
        self._use_input_bn = use_input_bn
        self._need_dense = need_dense
        self._dense_act = dense_act
        self._mode = mode

    def _build_model(self):
        return UnsupervisedSAGE(
            graph=self._graph,
            features_num=self._features_num,
            node_type=self._node_type,
            sample_paths=self._sample_paths,
            positive_sample_path=self._positive_sample_path,
            neighs_num=self._neighs_num,
            neg_num=self._neg_num,
            agg_type=self._agg_type,
            categorical_attrs_desc=self._categorical_attrs_desc,
            hidden_dim=self._hidden_dim,
            hidden_act=self._hidden_act,
            in_drop_rate=self._in_drop_rate,
            use_input_bn=self._use_input_bn,
            need_dense=self._need_dense,
            dense_act=self._dense_act,
            mode=self._mode
        )

    def run(self):
        model = self._build_model()
        while True:
            try:
                ids, _ = self._task_queue.get(timeout=self._timeout)
            except Exception as e:
                break
            src_recept = model.receptive_fn(self._node_type, ids).flatten(spec=model.src_ego_spec)
            if model.mode == "predict":
                self._result_queue.put(((tuple(src_recept), ids), ids.astype(np.int32)))
            else:
                p_nodes = model.positive_sample(ids)
                pos_recept = model.receptive_fn(p_nodes.type, p_nodes.ids).flatten(spec=model.src_ego_spec)
                n_nodes = model.negative_sample(ids)
                neg_recept = model.receptive_fn(n_nodes.type, n_nodes.ids).flatten(spec=model.src_ego_spec)
                self._result_queue.put(((tuple(src_recept), tuple(pos_recept), tuple(neg_recept)), ids.astype(np.int32)))

class LineSampler(SamplerProcess):

    def __init__(self,
                 name,
                 task_queue,
                 result_queue,
                 nodes_def,
                 edges_def,
                 redis_host,
                 redis_port,
                 redis_passwd,
                 node_type,
                 node_count,
                 edge_type,
                 embedding_dim=64,
                 neg_num=5,
                 proximity='first_order',
                 temperature=1.0,
                 str2hash=False,
                 mode="train",
                 timeout=1,
                 **kwargs
                 ):
        super(LineSampler, self).__init__(
            name,
            task_queue,
            result_queue,
            nodes_def,
            edges_def,
            node_type,
            redis_host,
            redis_port,
            redis_passwd,
            timeout,
            **kwargs
        )
        self._node_type = node_type
        self._node_count = node_count
        self._edge_type = edge_type
        self._embedding_dim = embedding_dim
        self._neg_num = neg_num
        self._proximity = proximity
        self._temperature = temperature
        self._str2hash = str2hash
        self._mode = mode

    def _build_model(self):
        return LINE(
            graph=self._graph,
            node_type=self._node_type,
            node_count=self._node_count,
            edge_type=self._edge_type,
            embedding_dim=self._embedding_dim,
            neg_num=self._neg_num,
            proximity=self._proximity,
            temperature=self._temperature,
            str2hash=self._str2hash,
            mode=self._mode
        )

    def run(self):
        model = self._build_model()
        while True:
            try:
                ids, _ = self._task_queue.get(timeout=self._timeout)
            except Exception as e:
                break
            if self._mode == "predict":
                self._result_queue.put((ids, ids.astype(np.int32)))
            else:
                if self._proximity == "second_order":
                    pos_dst_ids = model.positive_sample(ids)
                else:
                    pos_dst_ids = ids
                neg_dst_ids = model.negative_sample(ids)
                self._result_queue.put(((ids, pos_dst_ids, neg_dst_ids), ids.astype(np.int32)))

class DeepWalkSampler(SamplerProcess):

    def __init__(self,
                 name,
                 task_queue,
                 result_queue,
                 nodes_def,
                 edges_def,
                 redis_host,
                 redis_port,
                 redis_passwd,
                 node_type,
                 node_count,
                 edge_type,
                 walk_len=10,
                 window_size=5,
                 embedding_dim=64,
                 neg_num=5,
                 temperature=1.0,
                 str2hash=False,
                 mode="train",
                 timeout=1,
                 **kwargs
                 ):
        super(DeepWalkSampler, self).__init__(
            name,
            task_queue,
            result_queue,
            nodes_def,
            edges_def,
            node_type,
            redis_host,
            redis_port,
            redis_passwd,
            timeout,
            **kwargs
        )
        self._node_type = node_type
        self._node_count = node_count
        self._edge_type = edge_type
        self._embedding_dim = embedding_dim
        self._neg_num = neg_num
        self._walk_len = walk_len
        self._window_size = window_size
        self._temperature = temperature
        self._str2hash = str2hash
        self._mode = mode

    def _build_model(self):
        return DeepWalk(
            graph=self._graph,
            node_type=self._node_type,
            node_count=self._node_count,
            edge_type=self._edge_type,
            embedding_dim=self._embedding_dim,
            neg_num=self._neg_num,
            walk_len=self._walk_len,
            window_size=self._window_size,
            temperature=self._temperature,
            str2hash=self._str2hash,
            mode=self._mode
        )

    def run(self):
        model = self._build_model()
        while True:
            try:
                ids, _ = self._task_queue.get(timeout=self._timeout)
            except Exception as e:
                break
            if self._mode == "predict":
                self._result_queue.put((ids, ids.astype(np.int32)))
            else:
                src_ids, dst_ids = model.positive_sample(ids)
                self._result_queue.put(((src_ids, dst_ids), ids.astype(np.int32)))

class Consumer(Process):
    def __init__(self, name, q):
        super(Consumer, self).__init__(name=name)
        self._q = q

    def run(self):
        while True:
            i = self._q.get(timeout=10)
            print("process: %s, i: %s" % (self.name, i))

if __name__ == "__main__":
    import json, time
    redis_host_ = "192.168.8.138"
    redis_port_ = 16379

    nodes_def_ = json.dumps(
        [
            {"node_type": "1", "labeled": True, "label_column": "label", "attr_types": ["int", "string", "int", ],
             "attr_columns": ["limit", "level", "bill"]},
            {"node_type": "2", "attr_types": ["string", "string"], "attr_columns": ["type", "city"]},
            {"node_type": "3", "attr_types": ["string"], "attr_columns": ["type"]},
        ]
    )
    edges_def_ = json.dumps(
        [
            {"edge_type": ("1", "1", "1"), "weighted": True, "weight_column": "weight", "directed": False},
            {"edge_type": ("1", "2", "2"), "weighted": True, "weight_column": "weight", "directed": False},
            {"edge_type": ("1", "3", "3"), "weighted": True, "weight_column": "weight", "directed": False},
        ]
    )
    sample_paths_ = [
        "(1)-[1]->(1)-[1]->(1)-[1]->(1)",
        "(1)-[2]->(2)<-[2]-(1)-[2]->(2)",
        "(1)-[3]->(3)<-[3]-(1)-[3]->(3)"
    ]

    categorical_attrs_desc_ = {
        "1": {0: ["card_level", 3, 16]},
        "2": {0: ["ip_type", 4, 16], 1: ["ip_city", 10, 16]},
        "3": {0: ["device_type", 3, 16]}
    }
    task_queue_ = Queue()
    result_queue_ = Queue()
    seed_p = SeedProcess(
        name="seed_process",
        task_queue=task_queue_,
        nodes_def=nodes_def_,
        edges_def=edges_def_,
        node_type="1",
        redis_host=redis_host_,
        redis_port=redis_port_,
        init_redis=True,
        epochs=1
    )
    seed_p.start()

    p_list = []
    for i in range(10):
        p = FullGCNSampler(
            name="sampler_%d" % i,
            task_queue=task_queue_,
            result_queue=result_queue_,
            nodes_def=nodes_def_,
            edges_def=edges_def_,
            redis_host=redis_host_,
            redis_port=redis_port_,
            redis_passwd=None,
            features_num={"1": 3, "2": 2, "3": 1},
            output_dim=None,
            node_type="1",
            sample_paths=sample_paths_,
        )
        p_list.append(p)

    for p in p_list:
        p.start()

    while result_queue_.empty():
        print("result queue is empty ......")
        time.sleep(0.1)

    p1 = Consumer("consumer", result_queue_)

    p1.start()

    seed_p.join()
    for p in p_list:
        p.join()
    p1.join()
    print("ZZZZZ")

max_stars_repo_name: deepest-stack/backend

max_issues_repo_name: deepest-stack/backend

max_forks_repo_name: deepest-stack/backend


text:
# -*- coding: utf-8 -*-
from collections import Counter
import os
import pickle

import numpy as np
import torch
from torchtext.legacy import vocab

from ..common.torchtext_test_case import TorchtextTestCase

def conditional_remove(f):
    if os.path.isfile(f):
        os.remove(f)

class TestVocab(TorchtextTestCase):

    def test_vocab_basic(self):
        c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
        v = vocab.Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'])

        expected_itos = ['<unk>', '<pad>', '<bos>',
                         'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T', 'hello', 'world']
        expected_stoi = {x: index for index, x in enumerate(expected_itos)}
        self.assertEqual(v.itos, expected_itos)
        self.assertEqual(dict(v.stoi), expected_stoi)

    def test_vocab_specials_first(self):
        c = Counter("a a b b c c".split())

        # add specials into vocabulary at first
        v = vocab.Vocab(c, max_size=2, specials=['<pad>', '<eos>'])
        expected_itos = ['<pad>', '<eos>', 'a', 'b']
        expected_stoi = {x: index for index, x in enumerate(expected_itos)}
        self.assertEqual(v.itos, expected_itos)
        self.assertEqual(dict(v.stoi), expected_stoi)

        # add specials into vocabulary at last
        v = vocab.Vocab(c, max_size=2, specials=['<pad>', '<eos>'], specials_first=False)
        expected_itos = ['a', 'b', '<pad>', '<eos>']
        expected_stoi = {x: index for index, x in enumerate(expected_itos)}
        self.assertEqual(v.itos, expected_itos)
        self.assertEqual(dict(v.stoi), expected_stoi)

    def test_vocab_without_unk(self):
        c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
        oov_word = 'OOVWORD'
        self.assertNotIn(oov_word, c)

        # tests for specials_first=True
        v_first = vocab.Vocab(c, min_freq=3, specials=['<pad>'], specials_first=True)
        expected_itos_first = ['<pad>', 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T', 'hello', 'world']
        expected_stoi_first = {x: index for index, x in enumerate(expected_itos_first)}
        self.assertEqual(v_first.itos, expected_itos_first)
        self.assertEqual(dict(v_first.stoi), expected_stoi_first)
        self.assertNotIn(oov_word, v_first.itos)
        self.assertNotIn(oov_word, v_first.stoi)

        # tests for specials_first=False
        v_last = vocab.Vocab(c, min_freq=3, specials=['<pad>'], specials_first=False)
        expected_itos_last = ['ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T', 'hello', 'world', '<pad>']
        expected_stoi_last = {x: index for index, x in enumerate(expected_itos_last)}
        self.assertEqual(v_last.itos, expected_itos_last)
        self.assertEqual(dict(v_last.stoi), expected_stoi_last)
        self.assertNotIn(oov_word, v_last.itos)
        self.assertNotIn(oov_word, v_last.stoi)

        # check if pad is mapped to the first index
        self.assertEqual(v_first.stoi['<pad>'], 0)
        # check if pad is mapped to the last index
        self.assertEqual(v_last.stoi['<pad>'], max(v_last.stoi.values()))

        # check if an oovword is not in vocab and a default unk_id is not assigned to it
        self.assertRaises(KeyError, v_first.stoi.__getitem__, oov_word)
        self.assertRaises(KeyError, v_last.stoi.__getitem__, oov_word)

    def test_vocab_set_vectors(self):
        c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5,
                     'ｔｅｓｔ': 4, 'freq_too_low': 2})
        v = vocab.Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'])
        stoi = {"hello": 0, "world": 1, "ｔｅｓｔ": 2}
        vectors = torch.FloatTensor([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]])
        dim = 2
        v.set_vectors(stoi, vectors, dim)
        expected_vectors = np.array([[0.0, 0.0], [0.0, 0.0], [0.0, 0.0],
                                     [0.0, 0.0], [0.1, 0.2], [0.5, 0.6],
                                     [0.3, 0.4]])
        self.assertEqual(v.vectors, expected_vectors)

    def test_errors(self):
        c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
        with self.assertRaises(ValueError):
            # Test proper error raised when using unknown string alias
            vocab.Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
                        vectors=["fasttext.english.300d"])
            vocab.Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
                        vectors="fasttext.english.300d")
        with self.assertRaises(ValueError):
            # Test proper error is raised when vectors argument is
            # non-string or non-Vectors
            vocab.Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'],
                        vectors={"word": [1, 2, 3]})

    def test_serialization(self):
        c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
        v = vocab.Vocab(c, min_freq=3, specials=['<unk>', '<pad>', '<bos>'])
        pickle_path = os.path.join(self.test_dir, "vocab.pkl")
        pickle.dump(v, open(pickle_path, "wb"))
        v_loaded = pickle.load(open(pickle_path, "rb"))
        assert v == v_loaded

    def test_serialization_backcompat(self):
        # Test whether loading works on models saved in which
        #  the state was not required to have an "unk_index".
        c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
        v = vocab.Vocab(c, min_freq=3, specials=['<pad>', '<bos>'])  # no unk special
        # Mock old vocabulary
        del v.__dict__["unk_index"]

        pickle_path = os.path.join(self.test_dir, "vocab.pkl")
        pickle.dump(v, open(pickle_path, "wb"))
        v_loaded = pickle.load(open(pickle_path, "rb"))
        assert v == v_loaded

    def test_has_unk(self):
        c = Counter({'hello': 4, 'world': 3, 'ᑌᑎIᑕOᗪᕮ_Tᕮ᙭T': 5, 'freq_too_low': 2})
        v = vocab.Vocab(c)
        self.assertEqual(v['not_in_it'], 0)


text:
import tensorflow as tf
import numpy as np
import tensorflow.contrib.slim as slim

def conv2d(inputs, rate_field, num_outputs, kernel_size, scope, stride=1, rate=1,
           is_train=True, bias=True, norm=True, activation=True, d_format='NHWC'):
    ### bias ###
    if bias:
        outputs = tf.contrib.layers.conv2d(inputs, num_outputs, kernel_size, stride=stride,
                                           data_format=d_format, rate=rate, activation_fn=None, scope=scope)
    else:
        outputs = tf.contrib.layers.conv2d(inputs, num_outputs, kernel_size, stride=stride,
                                           data_format=d_format, rate=rate, activation_fn=None, biases_initializer=None,
                                           scope=scope)

    ### BN ###
    if norm:
        outputs = tf.contrib.layers.batch_norm(outputs, decay=0.9, center=True, scale=True, activation_fn=None,
                                               epsilon=1e-5, is_training=is_train, scope=scope + '/batch_norm',
                                               data_format=d_format)

    if activation:
        outputs = tf.nn.relu(outputs, name=scope + '/relu')

    return outputs

# ———————————————————————————————————————————————————————————#

#######################################################################################################################
def bn(inputs, scope, is_train=True, d_format='NHWC'):
    outputs = tf.contrib.layers.batch_norm(outputs, decay=0.9, center=True, scale=True, activation_fn=None,
                                           epsilon=1e-5, is_training=is_train, scope='nonlocal/batch_norm',
                                           data_format=d_format)

    return outputs

def _relu(inputs, scope):
    outputs = tf.nn.relu(inputs, name=scope + '/relu')

    return outputs

def _tanh(inputs, scope):
    outputs = tf.nn.tanh(inputs, name=scope + '/tanh')

    return outputs

def _leaky_relu(inputs, scope):
    outputs = tf.nn.leaky_relu(inputs, name=scope + '/leaky_relu')

    return outputs

def _sigmoid(inputs, scope):
    outputs = tf.nn.sigmoid(inputs, name=scope + '/sigmoid')

    return outputs

def _max_pool2d(inputs, kernel_size, scope, stride=2, padding='SAME', data_format='NHWC'):
    outputs = tf.contrib.layers.max_pool2d(inputs, kernel_size, stride=stride,
                                           scope=scope + '/max_pool', padding=padding, data_format=data_format)

    return outputs

def _avg_pool2d(inputs, kernel_size, scope, stride=2, padding='SAME', data_format='NHWC'):
    outputs = tf.contrib.layers.avg_pool2d(inputs, kernel_size, stride=stride,
                                           scope=scope + '/avg_pool', padding=padding, data_format=data_format)

    return outputs

def deconv(inputs, num_outputs, kernel_size, scope, new_height=None, new_width=None, stride=2, is_train=True,
           d_format='NHWC'):
    stride_new = [stride, stride]
    outputs = tf.contrib.layers.conv2d_transpose(inputs, num_outputs, kernel_size, scope=scope + '/deconv',
                                                 stride=stride_new,
                                                 padding='SAME', data_format=d_format, activation_fn=None,
                                                 biases_initializer=None)

    return outputs

def bilinear(inputs, num_outputs, kernel_size, scope, new_height=None, new_width=None, stride=2, is_train=True,
             d_format='NHWC'):
    size_new = (new_height, new_width)
    outputs = tf.image.resize_bilinear(inputs, size=size_new, align_corners=True, name=scope + '/bilinear')

    return outputs

def deconv_unit(inputs, num_outputs, kernel_size, scope, new_height=None, new_width=None, stride=2, is_train=True,
                d_format='NHWC'):
    stride_new = [stride, stride]
    outputs = tf.contrib.layers.conv2d_transpose(inputs, num_outputs, kernel_size, scope=scope + '/deconv',
                                                 stride=stride_new,
                                                 padding='SAME', data_format=d_format, activation_fn=None,
                                                 biases_initializer=None)

    outputs = tf.contrib.layers.batch_norm(outputs, decay=0.9, center=True, scale=True, activation_fn=tf.nn.relu,
                                           epsilon=1e-5, is_training=is_train, scope=scope + '/batch_norm',
                                           data_format=d_format)

    return outputs


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
aps: APS Journals API in Python for Humans.

@author: ishxiao
~Email~: me@ishxiao.com

"""
DOCLINES = __doc__.split('\n')

CLASSIFIERS = """\
Development Status :: 0 - alpha
Intended Audience :: Science/Research
License :: OSI Approved :: MIT License
Programming Language :: Python
Programming Language :: Python :: 3
Topic :: Scientific/Engineering
Operating System :: MacOS
Operating System :: POSIX
Operating System :: Unix
Operating System :: Microsoft :: Windows
"""

# import statements
import os
import sys
# The following is required to get unit tests up and running.
# If the user doesn't have, then that's OK, we'll just skip unit tests.
try:
    from setuptools import setup, Extension
    TEST_SUITE = 'nose.collector'
    TESTS_REQUIRE = ['nose']
    EXTRA_KWARGS = {
        'test_suite': TEST_SUITE,
        'tests_require': TESTS_REQUIRE
    }
except:
    from distutils.core import setup
    from distutils.extension import Extension
    EXTRA_KWARGS = {}

try:
    import numpy as np
except:
    np = None

from Cython.Build import cythonize
from Cython.Distutils import build_ext

# all information about aps goes here
MAJOR = 0
MINOR = 0
MICRO = 0
ISRELEASED = False
VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)
REQUIRES = ['numpy (>=1.8)', 'scipy (>=0.15)', 'cython (>=0.21)']
INSTALL_REQUIRES = ['numpy>=1.8', 'scipy>=0.15', 'cython>=0.21']
PACKAGES = ['aps', 'aps/tests']
PACKAGE_DATA = {
    'aps': ['configspec.ini'],
    'aps/tests': ['*.ini']
}
# If we're missing numpy, exclude import directories until we can
# figure them out properly.
INCLUDE_DIRS = [np.get_include()] if np is not None else []

#from setuptools import setup
HEADERS = []
NAME = "aps"
AUTHOR = ("Xiao Shang")
AUTHOR_EMAIL = ("me@ishxiao.com")
LICENSE = "MIT"
DESCRIPTION = DOCLINES[0]
LONG_DESCRIPTION = "\n".join(DOCLINES[2:])
KEYWORDS = "physics"
URL = "https://github.com/ishxiao/aps"
CLASSIFIERS = [_f for _f in CLASSIFIERS.split('\n') if _f]
PLATFORMS = ["Linux", "Mac OSX", "Unix", "Windows"]

def git_short_hash():
    try:
        git_str = "+" + os.popen('git log -1 --format="%h"').read().strip()
    except:
        git_str = ""
    else:
        if git_str == '+': #fixes setuptools PEP issues with versioning
            git_str = ''
    return git_str

FULLVERSION = VERSION

# NumPy's distutils reads in versions differently than
# our fallback. To make sure that versions are added to
# egg-info correctly, we need to add FULLVERSION to
# EXTRA_KWARGS if NumPy wasn't imported correctly.
if np is None:
    EXTRA_KWARGS['version'] = FULLVERSION

def write_version_py(filename='aps/version.py'):
    cnt = """\
# THIS FILE IS GENERATED FROM ASP SETUP.PY
short_version = '%(version)s'
version = '%(fullversion)s'
release = %(isrelease)s
"""
    a = open(filename, 'w')
    try:
        a.write(cnt % {'version': VERSION, 'fullversion':
                FULLVERSION, 'isrelease': str(ISRELEASED)})
    finally:
        a.close()

local_path = os.path.dirname(os.path.abspath(sys.argv[0]))
os.chdir(local_path)
sys.path.insert(0, local_path)
sys.path.insert(0, os.path.join(local_path, 'aps'))  # to retrive _version

# always rewrite _version
if os.path.exists('asp/version.py'):
    os.remove('asp/version.py')

write_version_py()

EXT_MODULES =[]

if not ISRELEASED:
    FULLVERSION += '.dev'+str(MICRO)+git_short_hash()

# Setup commands go here
setup(name = NAME,
      version = FULLVERSION,
      packages = PACKAGES,
      include_package_data = True,
      include_dirs = INCLUDE_DIRS,
      headers = HEADERS,
      ext_modules = cythonize(EXT_MODULES),
      cmdclass = {'build_ext': build_ext},
      author = AUTHOR,
      author_email = AUTHOR_EMAIL,
      license = LICENSE,
      description = DESCRIPTION,
      long_description = LONG_DESCRIPTION,
      keywords = KEYWORDS,
      url = URL,
      classifiers = CLASSIFIERS,
      platforms = PLATFORMS,
      requires = REQUIRES,
      package_data = PACKAGE_DATA,
      zip_safe = False,
      install_requires=INSTALL_REQUIRES,
      **EXTRA_KWARGS
)

max_stars_repo_name: ishxiao/aps

max_issues_repo_name: ishxiao/aps

max_forks_repo_name: ishxiao/aps


text:
import sys
sys.path.append("..")
import os
import numpy as np
from pathlib import Path

def stop_calibration():
    root = Path(os.path.abspath(__file__)).parents[1]
    index_files_directory = os.path.join( root, 'Intellux', 'mechanical_module', 'calibration_info')
    if os.path.isdir(index_files_directory) == False:
        os.mkdir( index_files_directory)
    calibration_status_file = os.path.join(index_files_directory, 'calibration_status.npy')
    update_calibration_status(calibration_status_file)

def update_calibration_status(calibration_status_file):
    new_calibration_status = 0
    with open(calibration_status_file, 'wb') as f:
        np.save(f, new_calibration_status)  # Stop calibration

if __name__=='__main__':
    stop_calibration()

max_stars_repo_name: abdul-gendy/Intellux

max_issues_repo_name: abdul-gendy/Intellux

max_forks_repo_name: abdul-gendy/Intellux


text:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.model_selection import learning_curve

def analyzeDigits (features, patterns):
    """
    Computes mean and variance values for all pixels (features)
    of all digits.
    """
    for i in range (10):
        if i == 0:
            continue
        feats_val = features[patterns == i]
        mean_val = feats_val.mean (axis = 0)
        var_val = feats_val.var (axis = 0)
        if i == 1:
            mean_all = mean_val
            var_all = var_val
        else:
            mean_all = np.vstack((mean_all, mean_val))
            var_all = np.vstack((var_all, var_val))
    return mean_all, var_all

def probGen (patterns):
    """
    Returns a vector of probabilities, based on the frequency
    of each pattern in the data.
    """
    probz = np.zeros((9,), dtype = float)
    for i in range (10):
        if i == 0:
            continue
        probz[i - 1] = (patterns == i).sum () / patterns.size
    return probz

def bayesHelp (means, vars, priors, features, smooth_fac):
    """
    Performs naive Gaussian Bayes inference on a set of features, given
    their respective means, vars and priors.
    """
    init_preds = np.zeros ((features.shape[0], 9))
    #Based on scikit-learn's GaussianNB implementation.
    if smooth_fac != 0:
        vars += smooth_fac * vars.max ()
    for i in range (10):
        if i == 0:
            continue
        dig_cov  = vars[i - 1, :]
        #Keep only the non-zero variance features, to avoid
        #NaN's when calculating Gaussian pdf.
        idx = dig_cov != 0
        dig_mean = means[i - 1, :]
        prob = - (features[:, idx] - dig_mean[idx]) ** 2 /     \
               (2 * dig_cov[idx])
        #Choose logarithm notation; consecutive multiplications
        #of small numbers harm accuracy.
        prob -= 0.5 * np.log (2 * np.pi * dig_cov[idx])
        #Add the prior!
        init_preds [:, i - 1] = prob.sum (1) +              \
                            np.log (priors[i - 1])
    return init_preds.argmax (axis = 1) + 1


text:
from rllab.envs.gym_mujoco.mujoco_env import MujocoEnv
from rllab.core.serializable import Serializable
from rllab.envs.base import Step
from rllab.misc.overrides import overrides
from rllab.misc import logger

from rllab.envs.mujoco.mujoco_env import q_mult, q_inv
import numpy as np
import math

class AntEnv(MujocoEnv, Serializable):

    FILE = 'ant.xml'
    ORI_IND = 3

    def __init__(self, *args, target_velocity=None, **kwargs):
        super(AntEnv, self).__init__(*args, **kwargs)
        Serializable.__init__(self, *args, **kwargs)
        self.frame_skip = 2
        self.target_velocity = None

    def get_current_obs(self):
        return np.concatenate([
            self.model.data.qpos.flat[2:],
            self.model.data.qvel.flat,
        ]).reshape(-1)

    def step(self, action):
        self.forward_dynamics(action)
        velocity = self.model.data.qvel[0, 0] # velocity in x direction
        if self.target_velocity:
            forward_reward = np.abs(velocity - self.target_velocity)
        else:
            forward_reward = velocity
        lb, ub = self.action_bounds
        scaling = (ub - lb) * 0.5
        ctrl_cost = .5 * 1e-2 * np.square(action/scaling).sum()
        contact_cost = 0.5 * 1e-3 * np.sum(np.square(np.clip(self.model.data.cfrc_ext, -1, 1)))
        survive_reward = 1.0
        reward = forward_reward - ctrl_cost + survive_reward
        ob = self.get_current_obs()
        notdone = np.isfinite(ob).all() and ob[0] <= 1.0 and ob[0] >= 0.2
        done = not notdone

        reward = np.minimum(np.maximum(-1000.0, reward), 1000.0)

        self.time_step += 1
        if self.max_path_length and self.time_step > self.max_path_length:
            done = True

        return ob, reward, done, {}

    @overrides
    def get_ori(self):
        ori = [0, 1, 0, 0]
        rot = self.model.data.qpos[self.__class__.ORI_IND:self.__class__.ORI_IND + 4]  # take the quaternion
        ori = q_mult(q_mult(rot, ori), q_inv(rot))[1:3]  # project onto x-y plane
        ori = math.atan2(ori[1], ori[0])
        return ori

    @overrides
    def log_diagnostics(self, paths):
        progs = [
            path["observations"][-1][-3] - path["observations"][0][-3]
            for path in paths
        ]
        logger.record_tabular('AverageForwardProgress', np.mean(progs))
        logger.record_tabular('MaxForwardProgress', np.max(progs))
        logger.record_tabular('MinForwardProgress', np.min(progs))
        logger.record_tabular('StdForwardProgress', np.std(progs))