Exemple #1
0
def run_hyperpolarizing_learning_subjects(subj_ids, stim_types, coherence_levels, trials_per_condition):
    (p_dcs, i_dcs) = (-stim_intensity_max, 0.5*stim_intensity_max)
    conditions= OrderedDict({'training': (simulation_params(ntrials=trials_per_condition, plasticity=True, p_dcs=p_dcs, i_dcs=i_dcs, dcs_start_time=0*second, dcs_end_time=4*second), True, coherence_levels)})
    conditions['testing'] = (simulation_params(ntrials=trials_per_condition), True, coherence_levels)
    print conditions.keys()
    print p_dcs, i_dcs
    # Run subjects
    run_virtual_subjects(subj_ids, conditions,
        '/home/jeff/projects/pySBI/data/stdp_maxstim_2_plasticity_14_40_fixed/final/1/hyperpolarizing/final_test_final',
        '/home/jeff/projects/pySBI/data/rerw/fitted_behavioral_params.h5')
Exemple #2
0
    def __init__(self, subj_id, wta_params=default_params(), pyr_params=pyr_params(), inh_params=inh_params(),
                 plasticity_params=plasticity_params(), sim_params=simulation_params()):
        self.subj_id = subj_id
        self.wta_params = wta_params
        self.pyr_params = pyr_params
        self.inh_params = inh_params
        self.plasticity_params = plasticity_params
        self.sim_params = sim_params

        self.simulation_clock = Clock(dt=self.sim_params.dt)
        self.input_update_clock = Clock(dt=1 / (self.wta_params.refresh_rate / Hz) * second)

        self.background_input = PoissonGroup(self.wta_params.background_input_size,
            rates=self.wta_params.background_freq, clock=self.simulation_clock)
        self.task_inputs = []
        for i in range(self.wta_params.num_groups):
            self.task_inputs.append(PoissonGroup(self.wta_params.task_input_size,
                rates=self.wta_params.task_input_resting_rate, clock=self.simulation_clock))

        # Create WTA network
        self.wta_network = WTANetworkGroup(params=self.wta_params, background_input=self.background_input,
            task_inputs=self.task_inputs, pyr_params=self.pyr_params, inh_params=self.inh_params,
            plasticity_params=self.plasticity_params, clock=self.simulation_clock)


        # Create network monitor
        self.wta_monitor = WTAMonitor(self.wta_network, None, None, self.sim_params, record_lfp=False,
                                      record_voxel=False, record_neuron_state=False, record_spikes=False,
                                      record_firing_rate=True, record_inputs=True, record_connections=None,
                                      save_summary_only=False, clock=self.simulation_clock)


        # Create Brian network and reset clock
        self.net = Network(self.background_input, self.task_inputs, self.wta_network,
            self.wta_network.connections.values(), self.wta_monitor.monitors.values())
Exemple #3
0
def post_wta_jobs(nodes,
                  p_b_e_range,
                  p_x_e_range,
                  p_e_e_range,
                  p_e_i_range,
                  p_i_i_range,
                  p_i_e_range,
                  num_trials,
                  muscimol_amount=0 * nS,
                  injection_site=0,
                  start_nodes=True):
    sim_params = simulation_params()
    sim_params.muscimol_amount = muscimol_amount
    sim_params.injection_site = injection_site

    input_sum = 40.0
    launcher = Launcher(nodes)
    if start_nodes:
        launcher.set_application_script(
            os.path.join(SRC_DIR, 'sh/ezrcluster-application-script.sh'))
        launcher.start_nodes()

    contrast_range = [0.0, 0.0625, 0.125, 0.25, 0.5, 1.0]

    for p_b_e in p_b_e_range:
        for p_x_e in p_x_e_range:
            for p_e_e in p_e_e_range:
                for p_e_i in p_e_i_range:
                    for p_i_i in p_i_i_range:
                        for p_i_e in p_i_e_range:
                            wta_params = default_params()
                            wta_params.p_b_e = p_b_e
                            wta_params.p_x_e = p_x_e
                            wta_params.p_e_e = p_e_e
                            wta_params.p_e_i = p_e_i
                            wta_params.p_i_i = p_i_i
                            wta_params.p_i_e = p_i_e
                            for i, contrast in enumerate(contrast_range):
                                inputs = np.zeros(2)
                                inputs[0] = (input_sum * (contrast + 1.0) /
                                             2.0)
                                inputs[1] = input_sum - inputs[0]
                                for t in range(num_trials):
                                    np.random.shuffle(inputs)
                                    cmds, log_file_template, out_file = get_wta_cmds(
                                        wta_params,
                                        inputs,
                                        sim_params,
                                        contrast,
                                        t,
                                        record_lfp=True,
                                        record_voxel=True,
                                        record_neuron_state=False,
                                        record_firing_rate=True,
                                        record_spikes=True)
                                    launcher.add_job(
                                        cmds,
                                        log_file_template=log_file_template,
                                        output_file=out_file)
Exemple #4
0
def launch_virtual_subject_processes(nodes, mu_0, virtual_subj_ids, behavioral_param_file, trials, stim_conditions,
                                     start_nodes=True):
    """
    nodes = nodes to run simulation on
    data_dir = directory containing subject data
    num_real_subjects = number of real subjects
    num_virtual_subjects = number of virtual subjects to run
    behavioral_param_file = file containing subject fitted behavioral parameters
    start_nodes = whether or not to start nodes
    """

    # Setup launcher
    launcher=Launcher(nodes)

    wta_params=default_params()
    wta_params.mu_0=mu_0
    wta_params.p_a=wta_params.mu_0/100.0
    wta_params.p_b=wta_params.p_a

    # Get subject alpha and beta values
    f = h5py.File(behavioral_param_file)
    control_group=f['control']
    alpha_vals=np.array(control_group['alpha'])
    beta_vals=np.array(control_group['beta'])

    # For each virtual subject
    for virtual_subj_id in virtual_subj_ids:

        # Sample beta from subject distribution - don't use subjects with high alpha
        beta_hist,beta_bins=np.histogram(beta_vals[np.where(alpha_vals<.99)[0]], density=True)
        bin_width=beta_bins[1]-beta_bins[0]
        beta_bin=np.random.choice(beta_bins[:-1], p=beta_hist*bin_width)
        beta=beta_bin+np.random.rand()*bin_width
        wta_params.background_freq=(beta-161.08)/-.17

        contrast_range=[0.0, .032, .064, .096, .128, .256, .512]
        for i,contrast in enumerate(contrast_range):
            inputs=np.array([wta_params.mu_0+wta_params.p_a*contrast*100.0,
                             wta_params.mu_0-wta_params.p_b*contrast*100.0])
            for t in range(trials):
                np.random.shuffle(inputs)
                for stim_condition,stim_values in stim_conditions.iteritems():
                    sim_params=simulation_params()
                    sim_params.p_dcs=stim_values[0]
                    sim_params.i_dcs=stim_values[1]
                    cmds,log_file_template,out_file=get_wta_cmds(wta_params, inputs, sim_params, contrast, t,
                        record_lfp=True, record_voxel=True, record_neuron_state=False, record_firing_rate=True,
                        record_spikes=True, save_summary_only=False,
                        e_desc='virtual_subject.%d.%s' % (virtual_subj_id,stim_condition))
                    launcher.add_batch_job(cmds, log_file_template=log_file_template, output_file=out_file)

    launcher.post_jobs()

    if start_nodes:
        launcher.set_application_script(os.path.join(SRC_DIR, 'sh/ezrcluster-application-script.sh'))
        launcher.start_nodes()
Exemple #5
0
def run_nostim_training_subjects(subj_ids, stim_types, coherence_levels, trials_per_condition):
    """
    Run subjects with no stimulation during training - runs baseline without stim, training without stim, and test without stim
    nsubjects = number of subjects to simulation
    coherence_levels = number of coherence levels to use
    """
    conditions = OrderedDict({'baseline': (simulation_params(ntrials=trials_per_condition), True, coherence_levels)})

    # conditions = OrderedDict({'training': (simulation_params(ntrials=trials_per_condition, plasticity=True), True, coherence_levels)})
    conditions['training']= (simulation_params(ntrials=trials_per_condition, plasticity=True), True, coherence_levels)

    # conditions = OrderedDict({'training': (simulation_params(ntrials=trials_per_condition, plasticity=True), True, coherence_levels)})
    conditions['testing'] = (simulation_params(ntrials=trials_per_condition), True, coherence_levels)

    # run baseline with stim
    # for stim in stim_types:
    #      p_dcs, i_dcs = stim_types.get(stim)
    #      conditions['baseline_%s' % stim] = (simulation_params(ntrials=trials_per_condition, plasticity=False, p_dcs=p_dcs, i_dcs=i_dcs, dcs_start_time=0*second, dcs_end_time=4*second), True, coherence_levels)


    print conditions.keys()
    # Run subject
    run_virtual_subjects(subj_ids, conditions, '/home/jeff/projects/pySBI/data/stdp_maxstim_2_plasticity_14_40_fixed/final/6/control/final_test_final',
        '/home/jeff/projects/pySBI/data/rerw/fitted_behavioral_params.h5')
Exemple #6
0
def post_wta_jobs(nodes, p_b_e_range, p_x_e_range, p_e_e_range, p_e_i_range, p_i_i_range, p_i_e_range, num_trials,
                   muscimol_amount=0*nS, injection_site=0, start_nodes=True):
    sim_params=simulation_params()
    sim_params.muscimol_amount=muscimol_amount
    sim_params.injection_site=injection_site

    input_sum=40.0
    launcher=Launcher(nodes)
    if start_nodes:
        launcher.set_application_script(os.path.join(SRC_DIR, 'sh/ezrcluster-application-script.sh'))
        launcher.start_nodes()

    contrast_range=[0.0, 0.0625, 0.125, 0.25, 0.5, 1.0]

    for p_b_e in p_b_e_range:
        for p_x_e in p_x_e_range:
            for p_e_e in p_e_e_range:
                for p_e_i in p_e_i_range:
                    for p_i_i in p_i_i_range:
                        for p_i_e in p_i_e_range:
                            wta_params=default_params()
                            wta_params.p_b_e=p_b_e
                            wta_params.p_x_e=p_x_e
                            wta_params.p_e_e=p_e_e
                            wta_params.p_e_i=p_e_i
                            wta_params.p_i_i=p_i_i
                            wta_params.p_i_e=p_i_e
                            for i,contrast in enumerate(contrast_range):
                                inputs=np.zeros(2)
                                inputs[0]=(input_sum*(contrast+1.0)/2.0)
                                inputs[1]=input_sum-inputs[0]
                                for t in range(num_trials):
                                    np.random.shuffle(inputs)
                                    cmds,log_file_template,out_file=get_wta_cmds(wta_params, inputs, sim_params,
                                        contrast, t, record_lfp=True, record_voxel=True, record_neuron_state=False,
                                        record_firing_rate=True, record_spikes=True)
                                    launcher.add_job(cmds, log_file_template=log_file_template, output_file=out_file)
Exemple #7
0
def run_rl_simulation(mat_file, alpha=0.4, beta=5.0, background_freq=None, p_dcs=0*pA, i_dcs=0*pA, dcs_start_time=0*ms,
                      output_file=None):
    mat = scipy.io.loadmat(mat_file)
    prob_idx=-1
    mags_idx=-1
    for idx,(dtype,o) in enumerate(mat['store']['dat'][0][0].dtype.descr):
        if dtype=='probswalk':
            prob_idx=idx
        elif dtype=='mags':
            mags_idx=idx
    prob_walk=mat['store']['dat'][0][0][0][0][prob_idx]
    mags=mat['store']['dat'][0][0][0][0][mags_idx]
    prob_walk=prob_walk.astype(np.float32, copy=False)
    mags=mags.astype(np.float32, copy=False)
    mags /= 100.0

    wta_params=default_params()
    wta_params.input_var=0*Hz

    sim_params=simulation_params()
    sim_params.p_dcs=p_dcs
    sim_params.i_dcs=i_dcs
    sim_params.dcs_start_time=dcs_start_time

    exp_rew=np.array([0.5, 0.5])
    if background_freq is None:
        background_freq=(beta-161.08)/-.17
    wta_params.background_freq=background_freq


    trials=prob_walk.shape[1]
    sim_params.ntrials=trials

    vals=np.zeros(prob_walk.shape)
    choice=np.zeros(trials)
    rew=np.zeros(trials)
    rts=np.zeros(trials)
    inputs=np.zeros(prob_walk.shape)

    if output_file is not None:
        f = h5py.File(output_file, 'w')

        f.attrs['alpha']=alpha
        f.attrs['beta']=beta
        f.attrs['mat_file']=mat_file

        f_sim_params=f.create_group('sim_params')
        for attr, value in sim_params.iteritems():
            f_sim_params.attrs[attr] = value

        f_network_params=f.create_group('network_params')
        for attr, value in wta_params.iteritems():
            f_network_params.attrs[attr] = value

        f_pyr_params=f.create_group('pyr_params')
        for attr, value in pyr_params.iteritems():
            f_pyr_params.attrs[attr] = value

        f_inh_params=f.create_group('inh_params')
        for attr, value in inh_params.iteritems():
            f_inh_params.attrs[attr] = value

    for trial in range(sim_params.ntrials):
        print('Trial %d' % trial)
        vals[:,trial]=exp_rew
        ev=vals[:,trial]*mags[:,trial]
        inputs[0,trial]=ev[0]
        inputs[1,trial]=ev[1]
        inputs[:,trial]=40.0+40.0*inputs[:,trial]

        trial_monitor=run_wta(wta_params, inputs[:,trial], sim_params, record_lfp=False, record_voxel=False,
            record_neuron_state=False, record_spikes=True, record_firing_rate=True, record_inputs=False,
            plot_output=False)

        e_rates = []
        for i in range(wta_params.num_groups):
            e_rates.append(trial_monitor.monitors['excitatory_rate_%d' % i].smooth_rate(width=5 * ms, filter='gaussian'))
        i_rates = [trial_monitor.monitors['inhibitory_rate'].smooth_rate(width=5 * ms, filter='gaussian')]

        if output_file is not None:
            trial_group=f.create_group('trial %d' % trial)
            trial_group['e_rates'] = np.array(e_rates)

            trial_group['i_rates'] = np.array(i_rates)

        rt,decision_idx=get_response_time(e_rates, sim_params.stim_start_time, sim_params.stim_end_time,
            upper_threshold=wta_params.resp_threshold, lower_threshold=None, dt=sim_params.dt)

        reward=0.0
        if decision_idx>=0 and np.random.random()<=prob_walk[decision_idx,trial]:
            reward=1.0

        exp_rew[decision_idx]=(1.0-alpha)*exp_rew[decision_idx]+alpha*reward
        choice[trial]=decision_idx
        rts[trial]=rt
        rew[trial]=reward

    param_ests,prop_correct=fit_behavior(prob_walk, mags, rew, choice)

    if output_file is not None:
        f.attrs['est_alpha']=param_ests[0]
        f.attrs['est_beta']=param_ests[1]
        f.attrs['prop_correct']=prop_correct

        f['prob_walk']=prob_walk
        f['mags']=mags
        f['rew']=rew
        f['choice']=choice
        f['vals']=vals
        f['inputs']=inputs
        f['rts']=rts
        f.close()
Exemple #8
0
            session_monitor.plot()
        else:
            subject.wta_monitor.plot()
        plt.show()


if __name__ == "__main__":
    # Trials per condition
    trials_per_condition = 100
    # Max stimulation intensity
    stim_intensity_max = 0.75 * pA
    # Stimulation conditions
    conditions = {
        "control": simulation_params(
            ntrials=trials_per_condition,
            trial_duration=3 * second,
            stim_start_time=1 * second,
            stim_end_time=2 * second,
        ),
        "depolarizing": simulation_params(
            ntrials=trials_per_condition,
            trial_duration=3 * second,
            stim_start_time=1 * second,
            stim_end_time=2 * second,
            p_dcs=stim_intensity_max,
            i_dcs=-0.5 * stim_intensity_max,
            dcs_start_time=0 * second,
            dcs_end_time=3 * second,
        ),
        "hyperpolarizing": simulation_params(
            ntrials=trials_per_condition,
            trial_duration=3 * second,
Exemple #9
0
def launch_control_virtual_subject_processes(nodes, mu_0, virtual_subj_ids, behavioral_param_file, trials,
                                             stim_gains=[8,6,4,2,1,0.5,0.25], start_nodes=True):
    """
    Launch stimulation intensity simulations with DCS applied only to pyramidal population
    nodes = nodes to run simulation on
    data_dir = directory containing subject data
    num_real_subjects = number of real subjects
    num_virtual_subjects = number of virtual subjects to run
    behavioral_param_file = file containing subject fitted behavioral parameters
    start_nodes = whether or not to start nodes
    """

    # Setup launcher
    launcher=Launcher(nodes)

    # Get subject alpha and beta values
    f = h5py.File(behavioral_param_file)
    control_group=f['control']
    alpha_vals=np.array(control_group['alpha'])
    beta_vals=np.array(control_group['beta'])

    # For each virtual subject
    for virtual_subj_id in virtual_subj_ids:

        wta_params=default_params()
        wta_params.mu_0=mu_0
        wta_params.p_a=mu_0/100.0
        wta_params.p_b=wta_params.p_a

        # Sample beta from subject distribution - don't use subjects with high alpha
        beta_hist,beta_bins=np.histogram(beta_vals[np.where(alpha_vals<.99)[0]], density=True)
        bin_width=beta_bins[1]-beta_bins[0]
        beta_bin=np.random.choice(beta_bins[:-1], p=beta_hist*bin_width)
        beta=beta_bin+np.random.rand()*bin_width
        wta_params.background_freq=(beta-161.08)/-.17

        contrast_range=[0.0, .032, .064, .128, .256, .512]
        for i,contrast in enumerate(contrast_range):
            inputs=np.zeros(2)
            inputs[0]=wta_params.mu_0+wta_params.p_a*contrast*100.0
            inputs[1]=wta_params.mu_0-wta_params.p_b*contrast*100.0
            for t in range(trials):
                np.random.shuffle(inputs)

                for idx, stim_gain in enumerate(stim_gains):
                    sim_params=simulation_params()
                    sim_params.p_dcs=stim_gain*pA
                    cmds,log_file_template,out_file=get_wta_cmds(wta_params, inputs, sim_params, contrast, t,
                        record_lfp=True, record_voxel=True, record_neuron_state=False, record_firing_rate=True,
                        record_spikes=True, save_summary_only=False,
                        e_desc='virtual_subject.%d.anode' % virtual_subj_id)
                    launcher.add_batch_job(cmds, log_file_template=log_file_template, output_file=out_file)

                    sim_params=simulation_params()
                    sim_params.p_dcs=-stim_gain*pA
                    cmds,log_file_template,out_file=get_wta_cmds(wta_params, inputs, sim_params, contrast, t,
                        record_lfp=True, record_voxel=True, record_neuron_state=False, record_firing_rate=True,
                        record_spikes=True, save_summary_only=False,
                        e_desc='virtual_subject.%d.cathode' % virtual_subj_id)
                    launcher.add_batch_job(cmds, log_file_template=log_file_template, output_file=out_file)

                    if idx==0:
                        sim_params=simulation_params()
                        cmds,log_file_template,out_file=get_wta_cmds(wta_params, inputs, sim_params, contrast, t,
                            record_lfp=True, record_voxel=True, record_neuron_state=False, record_firing_rate=True,
                            record_spikes=True, save_summary_only=False,
                            e_desc='virtual_subject.%d.control' % virtual_subj_id)
                        launcher.add_batch_job(cmds, log_file_template=log_file_template, output_file=out_file)

    launcher.post_jobs()

    if start_nodes:
        launcher.set_application_script(os.path.join(SRC_DIR, 'sh/ezrcluster-application-script.sh'))
        launcher.start_nodes()