def generate_trial(rng, dt, params): T = 1000 signal_time = rng.uniform(100, T - 800) output_delay = 100 width = 100 magnitude = 4 epochs = {} epochs['T'] = T t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial trial['info'] = {} signal_time /= dt width /= dt output_delay /= dt input_type = rng.randint(0, TypeCount) #input_type = 8 # Input matrix X = np.zeros((len(t), Nin)) for tt in range(len(t)): if tt >= signal_time: X[tt][0] = (input_type + 1) * 2 * np.exp(-(tt - signal_time) / (output_delay * 4)) #X = np.flip(X) trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.ones((len(t), Nout)) # Mask matrix for i in range(NoutSplit): for tt in range(len(t)): Y[tt][i + NoutSplit * input_type] = \ np.exp( -(tt - (signal_time + output_delay * (i + 2))) \ **2 / (2 * width**2)) * magnitude trial['outputs'] = Y return trial
def generate_trial(rng, dt, params): T = 1000 # signal_time = rng.uniform(100, T - 600) signal_time = rng.uniform(100, T - 800) # delay = 500 delay = 800 # delay1 = 500 # width = 20 width = 20 magnitude = 4 epochs = {} epochs['T'] = T t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial trial['info'] = {} signal_time /= dt delay /= dt width /= dt X = np.zeros((len(t), Nin)) for tt in range(len(t)): if tt > signal_time: X[tt][0] = np.exp(-(tt - signal_time) / delay) * magnitude trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.ones((len(t), Nout)) # Mask matrix for i in range(Nout): for tt in range(len(t)): Y[tt][i] = np.exp(-(tt - (signal_time + delay / Nout * (i + 1)))**2 / (2 * width**2)) * magnitude * 3 # Y[tt][i] = np.exp( -(tt - (signal_time + delay1 / Nout * (i + 1)))**2 / (2 * width**2)) * magnitude trial['outputs'] = Y return trial
def generate_trial(rng, dt, params): #------------------------------------------------------------------------------------- # Select task condition #------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: # Context context = params.get('context', rng.choice(contexts)) # Coherences coh_m = params.get('coh_m', rng.choice(cohs)) coh_c = params.get('coh_c', rng.choice(cohs)) # Left/right left_right_m = params.get('left_right_m', rng.choice(left_rights)) left_right_c = params.get('left_right_c', rng.choice(left_rights)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k = tasktools.unravel_index(b - 1, (len(contexts), len(cohs), len(cohs), len(left_rights), len(left_rights))) context = contexts[k[0]] coh_m = cohs[k[1]] coh_c = cohs[k[2]] left_right_m = left_rights[k[3]] left_right_c = left_rights[k[4]] else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2000} else: if params['name'] == 'test': fixation = 400 else: fixation = 100 stimulus = 800 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #------------------------------------------------------------------------------------- # Trial info #------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: if context == 'm': left_right = left_right_m else: left_right = left_right_c # Correct choice if left_right > 0: choice = 0 else: choice = 1 # Trial info trial['info'] = { 'coh_m': coh_m, 'left_right_m': left_right_m, 'coh_c': coh_c, 'left_right_c': left_right_c, 'context': context, 'choice': choice } #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Context if context == 'm': X[e['stimulus'], 0] = 1 else: X[e['stimulus'], 1] = 1 # Motion stimulus if left_right_m > 0: choice_m = 0 else: choice_m = 1 X[e['stimulus'], 2 + choice_m] = scale(+coh_m) X[e['stimulus'], 2 + (1 - choice_m)] = scale(-coh_m) # Colour stimulus if left_right_c > 0: choice_c = 0 else: choice_c = 1 X[e['stimulus'], 4 + choice_c] = scale(+coh_c) X[e['stimulus'], 4 + (1 - choice_c)] = scale(-coh_c) trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'], :] = lo # Decision Y[e['decision'], choice] = hi Y[e['decision'], 1 - choice] = lo # Only care about fixation and decision periods M[e['fixation'] + e['decision'], :] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #--------------------------------------------------------------------------- # Select task condition #--------------------------------------------------------------------------- if params.get('catch', rng.rand() < pcatch): catch_trial = True else: catch_trial = False coh = params.get('coh', rng.choice(cohs)) left_right = params.get('left_right', rng.choice(left_rights)) #--------------------------------------------------------------------------- # Epochs #--------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2000} else: fixation = 100 stimulus = 800 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #--------------------------------------------------------------------------- # Trial info #--------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs #print epochs #print t #print e trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if left_right > 0: choice = 0 else: choice = 1 # Trial info trial['info'] = {'coh': coh, 'left_right': left_right, 'choice': choice} #print(left_rights) #print(left_right) #print(choice) #--------------------------------------------------------------------------- # Inputs #--------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) X_old = np.copy(X) if not catch_trial: X[e['stimulus'],choice] = scale(+coh) X[e['stimulus'],1-choice] = scale(-coh) trial['inputs'] = X #plt.figure() #plt.subplot(211) #plt.plot(X) #--------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output M = np.zeros_like(Y) # Mask if catch_trial: Y[:] = 0.2 M[:] = 1 else: # Fixation Y[e['fixation'],:] = 0.2 # Decision Y[e['decision'],choice] = 1 Y[e['decision'],1-choice] = 0.2 # Only care about fixation and decision periods M[e['fixation']+e['decision'],:] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #--------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #--------------------------------------------------------------------------------- # Select task condition #--------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: fpair = params.get('fpair', fpairs[rng.choice(len(fpairs))]) gt_lt = params.get('gt_lt', rng.choice(gt_lts)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k0, k1 = tasktools.unravel_index(b - 1, (len(fpairs), len(gt_lts))) fpair = fpairs[k0] gt_lt = gt_lts[k1] else: raise ValueError("Unknown trial type.") #--------------------------------------------------------------------------------- # Epochs #--------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2500} else: if params['name'] == 'test': fixation = 500 else: fixation = 100 f1 = 500 if params['name'] == 'test': delay = 3000 else: delay = tasktools.uniform(rng, dt, 2500, 3500) f2 = 500 decision = 300 T = fixation + f1 + delay + f2 + decision epochs = { 'fixation': (0, fixation), 'f1': (fixation, fixation + f1), 'delay': (fixation + f1, fixation + f1 + delay), 'f2': (fixation + f1 + delay, fixation + f1 + delay + f2), 'decision': (fixation + f1 + delay + f2, T) } epochs['T'] = T #--------------------------------------------------------------------------------- # Trial info #--------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if gt_lt == '>': f1, f2 = fpair choice = 0 else: f2, f1 = fpair choice = 1 # Info trial['info'] = {'f1': f1, 'f2': f2, 'choice': choice} #--------------------------------------------------------------------------------- # Inputs #--------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Stimulus 1 X[e['f1'], POS] = scale_p(f1) X[e['f1'], NEG] = scale_n(f1) # Stimulus 2 X[e['f2'], POS] = scale_p(f2) X[e['f2'], NEG] = scale_n(f2) trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'], :] = lo # Decision Y[e['decision'], choice] = hi Y[e['decision'], 1 - choice] = lo # Mask M[e['fixation'] + e['decision'], :] = 1 trial['outputs'] = Y trial['mask'] = M #--------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #------------------------------------------------------------------------------------- # Select task condition #------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: # Context context = params.get('context', rng.choice(contexts)) # Coherences coh_m = params.get('coh_m', rng.choice(cohs)) coh_c = params.get('coh_c', rng.choice(cohs)) # Left/right left_right_m = params.get('left_right_m', rng.choice(left_rights)) left_right_c = params.get('left_right_c', rng.choice(left_rights)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k = tasktools.unravel_index(b-1, (len(contexts), len(cohs), len(cohs), len(left_rights), len(left_rights))) context = contexts[k[0]] coh_m = cohs[k[1]] coh_c = cohs[k[2]] left_right_m = left_rights[k[3]] left_right_c = left_rights[k[4]] else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2000} else: if params['name'] == 'test': fixation = 400 else: fixation = 100 stimulus = 800 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #------------------------------------------------------------------------------------- # Trial info #------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: if context == 'm': left_right = left_right_m else: left_right = left_right_c # Correct choice if left_right > 0: choice = 0 else: choice = 1 # Trial info trial['info'] = { 'coh_m': coh_m, 'left_right_m': left_right_m, 'coh_c': coh_c, 'left_right_c': left_right_c, 'context': context, 'choice': choice } #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Context if context == 'm': X[e['stimulus'],0] = 1 else: X[e['stimulus'],1] = 1 # Motion stimulus if left_right_m > 0: choice_m = 0 else: choice_m = 1 X[e['stimulus'],2+choice_m] = scale(+coh_m) X[e['stimulus'],2+(1-choice_m)] = scale(-coh_m) # Colour stimulus if left_right_c > 0: choice_c = 0 else: choice_c = 1 X[e['stimulus'],4+choice_c] = scale(+coh_c) X[e['stimulus'],4+(1-choice_c)] = scale(-coh_c) trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'],:] = lo # Decision Y[e['decision'],choice] = hi Y[e['decision'],1-choice] = lo # Only care about fixation and decision periods M[e['fixation']+e['decision'],:] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #------------------------------------------------------------------------------------- # Select task condition #------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: coh = params.get('coh', rng.choice(cohs)) in_out = params.get('in_out', rng.choice(in_outs)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k0, k1 = tasktools.unravel_index(b - 1, (len(cohs), len(in_outs))) coh = cohs[k0] in_out = in_outs[k1] else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2000} else: if params['name'] == 'test': fixation = 300 else: fixation = 100 stimulus = 800 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #------------------------------------------------------------------------------------- # Trial info #------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if in_out > 0: choice = 0 else: choice = 1 # Trial info trial['info'] = {'coh': coh, 'in_out': in_out, 'choice': choice} #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: X[e['stimulus'], choice] = scale(+coh) X[e['stimulus'], 1 - choice] = scale(-coh) trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output M = np.zeros_like(Y) # Mask # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'], :] = lo # Decision Y[e['decision'], choice] = hi Y[e['decision'], 1 - choice] = lo # Only care about fixation and decision periods M[e['fixation'] + e['decision'], :] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #------------------------------------------------------------------------------------- # Select task condition #------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: modality = params.get('modality', rng.choice(modalities)) freq = params.get('freq', rng.choice(freqs)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k1, k2 = tasktools.unravel_index(b - 1, (len(modalities), len(freqs))) modality = modalities[k1] freq = freqs[k2] else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2500} else: if params['name'] == 'test': fixation = 500 else: fixation = 100 stimulus = 1000 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #------------------------------------------------------------------------------------- # Trial info #------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if freq > boundary: choice = 0 else: choice = 1 # Trial info trial['info'] = {'modality': modality, 'freq': freq, 'choice': choice} #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: if 'v' in modality: X[e['stimulus'], VISUAL_P] = scale_v_p(freq) X[e['stimulus'], VISUAL_N] = scale_v_n(freq) if 'a' in modality: X[e['stimulus'], AUDITORY_P] = scale_a_p(freq) X[e['stimulus'], AUDITORY_N] = scale_a_n(freq) X[e['stimulus'] + e['decision'], START] = 1 trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'], :] = lo # Decision Y[e['decision'], choice] = hi Y[e['decision'], 1 - choice] = lo # Only care about fixation and decision periods M[e['fixation'] + e['decision'], :] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #--------------------------------------------------------------------------- # Select task condition #--------------------------------------------------------------------------- if params.get('catch', rng.rand() < pcatch): catch_trial = True else: catch_trial = False coh = params.get('coh', rng.choice(cohs)) left_right = params.get('left_right', rng.choice(left_rights)) #--------------------------------------------------------------------------- # Epochs #--------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2000} else: fixation = 100 stimulus = 800 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #--------------------------------------------------------------------------- # Trial info #--------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if left_right > 0: choice = 0 else: choice = 1 # Trial info trial['info'] = {'coh': coh, 'left_right': left_right, 'choice': choice} #--------------------------------------------------------------------------- # Inputs #--------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: X[e['stimulus'],choice] = scale(+coh) X[e['stimulus'],1-choice] = scale(-coh) trial['inputs'] = X #--------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output M = np.zeros_like(Y) # Mask if catch_trial: Y[:] = 0.2 M[:] = 1 else: # Fixation Y[e['fixation'],:] = 0.2 # Decision Y[e['decision'],choice] = 1 Y[e['decision'],1-choice] = 0.2 # Only care about fixation and decision periods M[e['fixation']+e['decision'],:] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #--------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): # ------------------------------------------------------------------------------------- # Select task condition # ------------------------------------------------------------------------------------- catch_trial = False if params["name"] in ["gradient", "test"]: if params.get("catch", rng.rand() < pcatch): catch_trial = True else: coh = params.get("coh", rng.choice(cohs)) in_out = params.get("in_out", rng.choice(in_outs)) elif params["name"] == "validation": b = params["minibatch_index"] % (nconditions + 1) if b == 0: catch_trial = True else: k0, k1 = tasktools.unravel_index(b - 1, (len(cohs), len(in_outs))) coh = cohs[k0] in_out = in_outs[k1] else: raise ValueError("Unknown trial type.") # ------------------------------------------------------------------------------------- # Epochs # ------------------------------------------------------------------------------------- if catch_trial: epochs = {"T": 2000} else: if params["name"] == "test": fixation = 300 stimulus = 1500 else: fixation = 100 stimulus = 800 no_reward = 300 T = fixation + stimulus epochs = {"fixation": (0, fixation), "stimulus": (fixation, T), "decision": (fixation + no_reward, T)} epochs["T"] = T # ------------------------------------------------------------------------------------- # Trial info # ------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {"t": t, "epochs": epochs} # Trial if catch_trial: trial["info"] = {} else: # Correct choice if in_out > 0: choice = 0 else: choice = 1 # Trial info trial["info"] = {"coh": coh, "in_out": in_out, "choice": choice} # ------------------------------------------------------------------------------------- # Inputs # ------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Stimulus X[e["stimulus"], choice] = scale(+coh) X[e["stimulus"], 1 - choice] = scale(-coh) # Start cue X[e["stimulus"], START] = 1 trial["inputs"] = X # ------------------------------------------------------------------------------------- # Target output # ------------------------------------------------------------------------------------- if params.get("target_output", False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1.2 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e["fixation"], :] = lo # Decision Y[e["decision"], choice] = hi Y[e["decision"], 1 - choice] = lo # Only care about fixation and decision periods M[e["fixation"] + e["decision"], :] = 1 # Outputs and mask trial["outputs"] = Y trial["mask"] = M # ------------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #------------------------------------------------------------------------------------- # Select task condition #------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: modality = params.get('modality', rng.choice(modalities)) freq = params.get('freq', rng.choice(freqs)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k1, k2 = tasktools.unravel_index(b-1, (len(modalities), len(freqs))) modality = modalities[k1] freq = freqs[k2] else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2500} else: if params['name'] == 'test': fixation = 500 else: fixation = 100 stimulus = 1000 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #------------------------------------------------------------------------------------- # Trial info #------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if freq > boundary: choice = 0 else: choice = 1 # Trial info trial['info'] = {'modality': modality, 'freq': freq, 'choice': choice} #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: if 'v' in modality: X[e['stimulus'],VISUAL_P] = scale_v_p(freq) X[e['stimulus'],VISUAL_N] = scale_v_n(freq) if 'a' in modality: X[e['stimulus'],AUDITORY_P] = scale_a_p(freq) X[e['stimulus'],AUDITORY_N] = scale_a_n(freq) X[e['stimulus'] + e['decision'],START] = 1 trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'],:] = lo # Decision Y[e['decision'],choice] = hi Y[e['decision'],1-choice] = lo # Only care about fixation and decision periods M[e['fixation']+e['decision'],:] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
#----------------------------------------------------------------------------------------- var_rec = 0.05**2 #----------------------------------------------------------------------------------------- # Task structure #----------------------------------------------------------------------------------------- # Period of the sine wave period = 8*tau # Sample duration epochs = {'T': 2*period} # Trial info t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial # Target output trial['outputs'] = 0.9*np.sin(2*np.pi*t/period)[:,None] def generate_trial(rng, dt, params): #Note all trial needs is t, epochs, outputs return trial # Target error min_error = 0.05 # Online training mode = 'continuous' n_validation = 50
def generate_trial(rng, dt, params): # ------------------------------------------------------------------------------------- # Select task condition # ------------------------------------------------------------------------------------- catch_trial = False if params["name"] in ["gradient", "test"]: if params.get("catch", rng.rand() < pcatch): catch_trial = True else: # Context context = params.get("context", rng.choice(contexts)) # Coherences coh_m = params.get("coh_m", rng.choice(cohs)) coh_c = params.get("coh_c", rng.choice(cohs)) # Left/right left_right_m = params.get("left_right_m", rng.choice(left_rights)) left_right_c = params.get("left_right_c", rng.choice(left_rights)) elif params["name"] == "validation": b = params["minibatch_index"] % (nconditions + 1) if b == 0: catch_trial = True else: k = tasktools.unravel_index( b - 1, (len(contexts), len(cohs), len(cohs), len(left_rights), len(left_rights)) ) context = contexts[k[0]] coh_m = cohs[k[1]] coh_c = cohs[k[2]] left_right_m = left_rights[k[3]] left_right_c = left_rights[k[4]] else: raise ValueError("Unknown trial type.") # ------------------------------------------------------------------------------------- # Epochs # ------------------------------------------------------------------------------------- if catch_trial: epochs = {"T": 2000} else: if params["name"] == "test": fixation = 400 else: fixation = 100 stimulus = 800 decision = 300 T = fixation + stimulus + decision epochs = { "fixation": (0, fixation), "stimulus": (fixation, fixation + stimulus), "decision": (fixation + stimulus, T), } epochs["T"] = T # ------------------------------------------------------------------------------------- # Trial info # ------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {"t": t, "epochs": epochs} # Trial if catch_trial: trial["info"] = {} else: if context == "m": left_right = left_right_m else: left_right = left_right_c # Correct choice if left_right > 0: choice = 0 else: choice = 1 # Trial info trial["info"] = { "coh_m": coh_m, "left_right_m": left_right_m, "coh_c": coh_c, "left_right_c": left_right_c, "context": context, "choice": choice, } # ------------------------------------------------------------------------------------- # Inputs # ------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Context if context == "m": X[e["stimulus"], 0] = 1 else: X[e["stimulus"], 1] = 1 # Motion stimulus if left_right_m > 0: choice_m = 0 else: choice_m = 1 X[e["stimulus"], 2 + choice_m] = scale(+coh_m) X[e["stimulus"], 2 + (1 - choice_m)] = scale(-coh_m) # Colour stimulus if left_right_c > 0: choice_c = 0 else: choice_c = 1 X[e["stimulus"], 4 + choice_c] = scale(+coh_c) X[e["stimulus"], 4 + (1 - choice_c)] = scale(-coh_c) trial["inputs"] = X # ------------------------------------------------------------------------------------- # Target output # ------------------------------------------------------------------------------------- if params.get("target_output", False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e["fixation"], :] = lo # Decision Y[e["decision"], choice] = hi Y[e["decision"], 1 - choice] = lo # Only care about fixation and decision periods M[e["fixation"] + e["decision"], :] = 1 # Outputs and mask trial["outputs"] = Y trial["mask"] = M # ------------------------------------------------------------------------------------- return trial
from __future__ import division import numpy as np from pycog import tasktools from pycog import RNN from pycog.figtools import Figure tau = 100 dt = 20 period = 8 * tau epochs = {'T': 2 * period} t, e = tasktools.get_epochs_idx(dt, epochs) rnn = RNN('savefile.pkl', {'dt': 0.5, 'var_rec': 0.01**2}) info = rnn.run(T=2 * period) fig = Figure() plot = fig.add() plot.plot(rnn.t / tau, rnn.z[0], color=Figure.colors('blue')) plot.xlim(rnn.t[0] / tau, rnn.t[-1] / tau) plot.ylim(0, 2) print rnn.t[0] print rnn.t[-1] plot.plot((rnn.t / tau)[:], (0.9 * np.power(rnn.t / (2 * period), 2))[:], color=Figure.colors('orange'))
def generate_trial(rng, dt, params): #--------------------------------------------------------------------------------- # Select task condition #--------------------------------------------------------------------------------- if params['name'] in ['gradient', 'test']: seq = params.get('seq', rng.choice(sequences.keys())) elif params['name'] == 'validation': b = params['minibatch_index'] % nseq if b == 0: generate_trial.seqs = rng.permutation(nseq) seq = generate_trial.seqs[b] + 1 else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- iti = 1000 fixation = 1000 M1 = 500 M2 = 500 M3 = 500 T = iti + fixation + M1 + M2 + M3 epochs = { 'iti': (0, iti), 'fixation': (iti, iti + fixation), 'M1': (iti + fixation, iti + fixation + M1), 'M2': (iti + fixation + M1, iti + fixation + M1 + M2), 'M3': (iti + fixation + M1 + M2, iti + fixation + M1 + M2 + M3), } epochs['T'] = T #--------------------------------------------------------------------------------- # Trial info #--------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial trial['info'] = {'seq': seq} #--------------------------------------------------------------------------------- # Inputs #--------------------------------------------------------------------------------- # Input matrix X = np.zeros((len(t), Nin)) # Which sequence? X[:,SEQUENCE[seq-1]] = 1 # Sequence sequence = sequences[seq] # Options X[e['fixation'],sequence[0]] = 1 for I, J in zip([e['M1'], e['M2'], e['M3']], [[sequence[0]] + options[sequence[0]], [sequence[1]] + options[sequence[1]], [sequence[2]] + options[sequence[2]]]): for j in J: X[I,j] = 1 # Inputs trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros((len(t), Nout)) # Mask matrix # Hold gaze Y[e['fixation'],:] = target_position(sequence[0]) Y[e['M1'],:] = target_position(sequence[1]) Y[e['M2'],:] = target_position(sequence[2]) Y[e['M3'],:] = target_position(sequence[3]) # We don't constrain the intertrial interval M[e['fixation']+e['M1']+e['M2']+e['M3'],:] = 1 # Output and mask trial['outputs'] = Y trial['mask'] = M #--------------------------------------------------------------------------------- return trial
delay = 800 # delay1 = 500 width = 20 # width = 5 # when N=1000 & Nout=50 magnitude = 4 dt = 0.5 var_rec = 0.01**2 # T = 100 # signal_time = rng.uniform(10, T - 60) # delay = 50 # width = 2 # magnitude = 4 epochs = {} epochs['T'] = T t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial trial['info'] = {} signal_time /= dt delay /= dt width /= dt # print "signal_time is: ",signal_time # Input matrix X = np.zeros((len(t), Nin)) for tt in range(len(t)): if tt > signal_time: X[tt][0] = np.exp(-(tt - signal_time) / delay) * magnitude
def generate_trial(rng, dt, params): T = 1000 # signal_time = rng.uniform(100, T - 600) signal_time = rng.uniform(100, T - 800) # delay = 500 delay = 800 # delay1 = 500 width = 20 # width = 5 # when N=1000 & Nout=50 magnitude = 4 # T = 100 # signal_time = rng.uniform(10, T - 60) # delay = 50 # width = 2 # magnitude = 4 epochs = {} epochs['T'] = T t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial trial['info'] = {} signal_time /= dt delay /= dt width /= dt # print "signal_time is: ",signal_time # Input matrix X = np.zeros((len(t), Nin)) for tt in range(len(t)): if tt > signal_time: X[tt][0] = np.exp(-(tt - signal_time) / delay) * magnitude trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.ones((len(t), Nout)) # Mask matrix for i in range(Nout): for tt in range(len(t)): Y[tt][i] = np.exp(-(tt - (signal_time + delay / Nout * (i + 1)))**2 / (2 * width**2)) * magnitude * 3 # Y[tt][i] = np.exp( -(tt - (signal_time + delay1 / Nout * (i + 1)))**2 / (2 * width**2)) * magnitude trial['outputs'] = Y '''heat_map = sb.heatmap(Y) plt.title('Heat map of target sequential activation of neurons') plt.ylabel('Target output neural nodes') plt.xlabel('Time') plt.show() plt.plot(t/tau, X[0]) # plt.plot(rnn.t/tau, rnn.r[3]) # plt.plot(rnn.t/tau, rnn.r[4]) # for j in range(Nout): legend = ['Input'] for j in range(Nout): plt.plot(t/tau, Y[i]) legend.append('Output {}'.format(j+1)) # plt.plot(rnn.t/tau, rnn.r[j]) plt.title('Target sequential activation of neurons') plt.ylabel('Target output neural nodes') plt.xlabel('Time') plt.legend(legend) plt.show()''' return trial
def generate_trial(rng, dt, params): #--------------------------------------------------------------------------------- # Select task condition #--------------------------------------------------------------------------------- if params['name'] in ['gradient', 'test']: seq = params.get('seq', rng.choice(sequences.keys())) elif params['name'] == 'validation': b = params['minibatch_index'] % nseq if b == 0: generate_trial.seqs = rng.permutation(nseq) seq = generate_trial.seqs[b] + 1 else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- iti = 1000 fixation = 1000 M1 = 500 M2 = 500 M3 = 500 T = iti + fixation + M1 + M2 + M3 epochs = { 'iti': (0, iti), 'fixation': (iti, iti + fixation), 'M1': (iti + fixation, iti + fixation + M1), 'M2': (iti + fixation + M1, iti + fixation + M1 + M2), 'M3': (iti + fixation + M1 + M2, iti + fixation + M1 + M2 + M3), } epochs['T'] = T #--------------------------------------------------------------------------------- # Trial info #--------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx( dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial trial['info'] = {'seq': seq} #--------------------------------------------------------------------------------- # Inputs #--------------------------------------------------------------------------------- # Input matrix X = np.zeros((len(t), Nin)) # Which sequence? X[:, SEQUENCE[seq - 1]] = 1 # Sequence sequence = sequences[seq] # Options X[e['fixation'], sequence[0]] = 1 for I, J in zip([e['M1'], e['M2'], e['M3']], [[sequence[0]] + options[sequence[0]], [sequence[1]] + options[sequence[1]], [sequence[2]] + options[sequence[2]]]): for j in J: X[I, j] = 1 # Inputs trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros((len(t), Nout)) # Mask matrix # Hold gaze Y[e['fixation'], :] = target_position(sequence[0]) Y[e['M1'], :] = target_position(sequence[1]) Y[e['M2'], :] = target_position(sequence[2]) Y[e['M3'], :] = target_position(sequence[3]) # We don't constrain the intertrial interval M[e['fixation'] + e['M1'] + e['M2'] + e['M3'], :] = 1 # Output and mask trial['outputs'] = Y trial['mask'] = M #--------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): # ------------------------------------------------------------------------------------- # Select task condition # ------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: intensity = params.get('intensity', rng.choice(intensity_range)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: intensity = params.get('intensity', rng.choice(intensity_range)) else: raise ValueError("Unknown trial type.") # ------------------------------------------------------------------------------------- # Epochs # ------------------------------------------------------------------------------------- t_foward_min = 80 t_foward_avg = 800 t_foward_max = 1500 t_stimulus = 300 t_reversal_min = 80 t_reversal_avg = 500 t_reversal_max = 1500 if catch_trial: epochs = {'T': tasktools.truncated_exponential(rng, dt, t_foward_avg, xmin=t_foward_min, xmax=t_foward_max)} else: if params['name'] == 'test': forward = tasktools.truncated_exponential(rng, dt, t_foward_avg, xmin=t_foward_min, xmax=t_foward_max) else: forward = tasktools.truncated_exponential(rng, dt, t_foward_avg, xmin=t_foward_min, xmax=t_foward_max) stimulus = t_stimulus reversal = tasktools.truncated_exponential(rng, dt, t_reversal_avg, xmin=t_reversal_min, xmax=t_reversal_max) T = forward + stimulus + reversal epochs = {'forward': (0, forward), 'stimulus': (forward, forward + stimulus), 'reversal': (forward + stimulus, T), 'T': T} # ------------------------------------------------------------------------------------- # Trial info # ------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial # Save e info trial['e'] = e if catch_trial: trial['info'] = {} else: # Trial info trial['info'] = {'intensity': intensity} #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Stimulus X[e['stimulus']] = intensity trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:, 0] = hi # forward module output Y[:, 1] = lo # reversal module output M[:] = 1 else: # forward period Y[e['forward'], 0] = hi # forward module output Y[e['forward'], 1] = lo # reversal module output # reversal periods Y[e['reversal'], 0] = lo # forward module output Y[e['reversal'], 1] = hi # reversal module output # Only care about forward and reversal periods M[e['forward']+e['reversal'],:] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #--------------------------------------------------------------------------------- # Select task condition #--------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: fpair = params.get('fpair', fpairs[rng.choice(len(fpairs))]) gt_lt = params.get('gt_lt', rng.choice(gt_lts)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k0, k1 = tasktools.unravel_index(b-1, (len(fpairs), len(gt_lts))) fpair = fpairs[k0] gt_lt = gt_lts[k1] else: raise ValueError("Unknown trial type.") #--------------------------------------------------------------------------------- # Epochs #--------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2500} else: if params['name'] == 'test': fixation = 500 else: fixation = 100 f1 = 500 if params['name'] == 'test': delay = 3000 else: delay = tasktools.uniform(rng, dt, 2500, 3500) f2 = 500 decision = 300 T = fixation + f1 + delay + f2 + decision epochs = { 'fixation': (0, fixation), 'f1': (fixation, fixation + f1), 'delay': (fixation + f1, fixation + f1 + delay), 'f2': (fixation + f1 + delay, fixation + f1 + delay + f2), 'decision': (fixation + f1 + delay + f2, T) } epochs['T'] = T #--------------------------------------------------------------------------------- # Trial info #--------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if gt_lt == '>': f1, f2 = fpair choice = 0 else: f2, f1 = fpair choice = 1 # Info trial['info'] = {'f1': f1, 'f2': f2, 'choice': choice} #--------------------------------------------------------------------------------- # Inputs #--------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Stimulus 1 X[e['f1'],POS] = scale_p(f1) X[e['f1'],NEG] = scale_n(f1) # Stimulus 2 X[e['f2'],POS] = scale_p(f2) X[e['f2'],NEG] = scale_n(f2) trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix M = np.zeros_like(Y) # Mask matrix # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'],:] = lo # Decision Y[e['decision'],choice] = hi Y[e['decision'],1-choice] = lo # Mask M[e['fixation']+e['decision'],:] = 1 trial['outputs'] = Y trial['mask'] = M #--------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #------------------------------------------------------------------------------------- # Select task condition #------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): #pcatch chance of a catch trial catch_trial = True else: cond = params.get('conditions', conditions[rng.choice(len(conditions))]) #Revert back to cohs if this brings up an issue in_out = params.get('in_out', rng.choice(in_outs)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k0, k1 = tasktools.unravel_index(b - 1, (len(conditions), len(in_outs))) cond = conditions[k0] in_out = in_outs[k1] else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2500} else: if params['name'] == 'test': fixation = 300 else: fixation = 100 f1 = 100 delay = 200 f2 = 100 decision = 200 # else: # if params['name'] == 'test': # firstInterval = 300 # else: # firstInterval = 100 # secondInterval = 200 # decision = 200 T = fixation + f1 + delay + f2 + decision epochs = { 'fixation': (0, fixation), 'f1': (fixation, fixation + f1), 'delay': (fixation + f1, fixation + f1 + delay), 'f2': (fixation + f1 + delay, fixation + f1 + delay + f2), 'decision': (fixation + f1 + delay + f2, T) } epochs['T'] = T #------------------------------------------------------------------------------------- # Trial info #------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx( dt, epochs) # Time, e is array of epochs w/ points trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if cond in [(yes, yes), (no, no)]: choice = 0 else: choice = 1 #if in_out == 'equal': # choice = 0 #else: # choice = 1 # Trial info trial['info'] = {'cond': cond, 'in_out': in_out, 'choice': choice} #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Stimulus 1 X[e['f1'], POS] = cond[0] X[e['f1'], NEG] = yes + no - cond[0] #flipped frequency 1->0.2, 0.2 -> 1 # Stimulus 2 X[e['f2'], POS] = cond[1] X[e['f2'], NEG] = yes + no - cond[1] #flipped frequency 1->0.2, 0.2 -> 1 trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output M = np.zeros_like(Y) # Mask # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'], :] = lo # Decision Y[e['decision'], choice] = hi # One neuron spikes high Y[e['decision'], 1 - choice] = lo # The other spikes low # Only care about fixation and decision periods M[e['fixation'] + e['decision'], :] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #--------------------------------------------------------------------------------- # Select task condition #--------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): #pcatch probability of catch trial catch_trial = True else: pair = params.get('pair', pairs[rng.choice(len(pairs))]) #random pair #gt_lt = params.get('gt_lt', rng.choice(gt_lts)) #random order elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) #0 to 4 if b == 0: catch_trial = True else: #b-1 is 0 to 3, len(pairs) = 0 to 1 k0, k1 = tasktools.unravel_index(b-1, (len(pairs),1))#len(gt_lts))) pair = pairs[b-1] #gt_lt = gt_lts[k1] else: raise ValueError("Unknown trial type.") #--------------------------------------------------------------------------------- # Epochs #--------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2500} else: if params['name'] == 'test': fixation = 500 else: fixation = 100 f1 = 500 if params['name'] == 'test': delay = 3000 else: delay = 3000 f2 = 500 decision = 300 T = fixation + f1 + delay + f2 + decision epochs = { 'fixation': (0, fixation), 'f1': (fixation, fixation + f1), 'delay': (fixation + f1, fixation + f1 + delay), 'f2': (fixation + f1 + delay, fixation + f1 + delay + f2), 'decision': (fixation + f1 + delay + f2, T) } epochs['T'] = T #--------------------------------------------------------------------------------- # Trial info #--------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: a1, a2 = pair # Correct choice if a1 == a2: choice = 0 else: choice = 1 # Info trial['info'] = {'f1': a1, 'f2': a2, 'choice': choice} #--------------------------------------------------------------------------------- # Inputs #--------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: # Stimulus 1 X[e['f1'],POS] = scale_p(a1) #X[e['f1'],NEG] = scale_n(f1) # Stimulus 2 X[e['f2'],POS] = scale_p(a2) #X[e['f2'],NEG] = scale_n(f2) trial['inputs'] = X #--------------------------------------------------------------------------------- # Target output #--------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output matrix by time M = np.zeros_like(Y) # Mask matrix by time # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'],:] = lo #while fixating it should not be spiking # Decision Y[e['decision'],choice] = hi #output corresponding to choice = hi Y[e['decision'],1-choice] = lo #other one goes to lo # Mask M[e['fixation']+e['decision'],:] = 1 trial['outputs'] = Y trial['mask'] = M #--------------------------------------------------------------------------------- return trial
def generate_trial(rng, dt, params): #------------------------------------------------------------------------------------- # Select task condition #------------------------------------------------------------------------------------- catch_trial = False if params['name'] in ['gradient', 'test']: if params.get('catch', rng.rand() < pcatch): catch_trial = True else: coh = params.get('coh', rng.choice(cohs)) in_out = params.get('in_out', rng.choice(in_outs)) elif params['name'] == 'validation': b = params['minibatch_index'] % (nconditions + 1) if b == 0: catch_trial = True else: k0, k1 = tasktools.unravel_index(b-1, (len(cohs), len(in_outs))) coh = cohs[k0] in_out = in_outs[k1] else: raise ValueError("Unknown trial type.") #------------------------------------------------------------------------------------- # Epochs #------------------------------------------------------------------------------------- if catch_trial: epochs = {'T': 2000} else: if params['name'] == 'test': fixation = 300 else: fixation = 100 stimulus = 800 decision = 300 T = fixation + stimulus + decision epochs = { 'fixation': (0, fixation), 'stimulus': (fixation, fixation + stimulus), 'decision': (fixation + stimulus, T) } epochs['T'] = T #------------------------------------------------------------------------------------- # Trial info #------------------------------------------------------------------------------------- t, e = tasktools.get_epochs_idx(dt, epochs) # Time, task epochs in discrete time trial = {'t': t, 'epochs': epochs} # Trial if catch_trial: trial['info'] = {} else: # Correct choice if in_out > 0: choice = 0 else: choice = 1 # Trial info trial['info'] = {'coh': coh, 'in_out': in_out, 'choice': choice} #------------------------------------------------------------------------------------- # Inputs #------------------------------------------------------------------------------------- X = np.zeros((len(t), Nin)) if not catch_trial: X[e['stimulus'],choice] = scale(+coh) X[e['stimulus'],1-choice] = scale(-coh) trial['inputs'] = X #------------------------------------------------------------------------------------- # Target output #------------------------------------------------------------------------------------- if params.get('target_output', False): Y = np.zeros((len(t), Nout)) # Output M = np.zeros_like(Y) # Mask # Hold values hi = 1 lo = 0.2 if catch_trial: Y[:] = lo M[:] = 1 else: # Fixation Y[e['fixation'],:] = lo # Decision Y[e['decision'],choice] = hi Y[e['decision'],1-choice] = lo # Only care about fixation and decision periods M[e['fixation']+e['decision'],:] = 1 # Outputs and mask trial['outputs'] = Y trial['mask'] = M #------------------------------------------------------------------------------------- return trial
from __future__ import division import numpy as np from pycog import tasktools from pycog import RNN from pycog.figtools import Figure tau = 100 dt = 20 period = 8*tau epochs = {'T': 2*period} t, e = tasktools.get_epochs_idx(dt, epochs) rnn = RNN('savefile.pkl', {'dt': 0.5, 'var_rec': 0.01**2}) info = rnn.run(T=2*period) fig = Figure() plot = fig.add() plot.plot(rnn.t/tau, rnn.z[0], color=Figure.colors('blue')) plot.xlim(rnn.t[0]/tau, rnn.t[-1]/tau) plot.ylim(0, 2) print rnn.t[0] print rnn.t[-1] plot.plot((rnn.t/tau)[:], (0.9*np.power(rnn.t/(2*period),2))[:],color=Figure.colors('orange')) plot.xlabel(r'$t/\tau$')