def create_thalamic_input(self):
        """ This function creates the thalamic neuronal population if this
        is specified in stimulus_params.py.

        """
        if self.stim_dict['thalamic_input']:
            if nest.Rank() == 0:
                print('Thalamic input provided')
            self.thalamic_population = nest.Create('parrot_neuron',
                                                   self.stim_dict['n_thal'])
            self.thalamic_weight = get_weight(self.stim_dict['PSP_th'],
                                              self.net_dict)
            self.stop_th = (self.stim_dict['th_start'] +
                            self.stim_dict['th_duration'])
            self.poisson_th = nest.Create('poisson_generator')
            self.poisson_th.set({
                'rate': self.stim_dict['th_rate'],
                'start': self.stim_dict['th_start'],
                'stop': self.stop_th
            })
            nest.Connect(self.poisson_th, self.thalamic_population)
            self.nr_synapses_th = synapses_th_matrix(self.net_dict,
                                                     self.stim_dict)
            if self.K_scaling != 1:
                self.thalamic_weight = self.thalamic_weight / (self.K_scaling**
                                                               0.5)
                self.nr_synapses_th = (self.nr_synapses_th * self.K_scaling)
        else:
            if nest.Rank() == 0:
                print('Thalamic input not provided')
    def create_thalamic_input(self):
        """ This function creates the thalamic neuronal population if this
        is specified in stimulus_params.py. 

        """
        if self.stim_dict['thalamic_input']:

            if nest.Rank() == 0:
                print('Thalamic input provided')

            # Get thalamic weights (using the get_weight() function)
            self.thalamic_weight = get_weight(self.stim_dict['PSP_th'],
                                              self.net_dict)

            # State when the thalamic input will stop
            self.stop_th = (self.stim_dict['th_start'] +
                            self.stim_dict['th_duration'])

            # Define thalamus as a population of parrot neurons
            self.thalamic_population = nest.Create('parrot_neuron',
                                                   self.stim_dict['n_thal'])

            # Create a population of Poisson neurons
            self.poisson_th = nest.Create('poisson_generator')
            nest.SetStatus(
                self.poisson_th, {
                    'rate': self.stim_dict['th_rate'],
                    'start': self.stim_dict['th_start'],
                    'stop': self.stop_th
                })

            # Connect the Poisson neurons to the thalamus
            nest.Connect(self.poisson_th, self.thalamic_population)

            # Calculate the number of synapses between thalamus and cortex
            self.nr_synapses_th = synapses_th_matrix(self.net_dict,
                                                     self.stim_dict)

            # Scale the synaptic weights and numbers (if needed)
            if self.K_scaling != 1:
                self.thalamic_weight = self.thalamic_weight / (self.K_scaling**
                                                               0.5)
                self.nr_synapses_th = (self.nr_synapses_th * self.K_scaling)
        else:
            if nest.Rank() == 0:
                print('Thalamic input not provided')
    def create_thalamic_input(self):
        """ This function creates the thalamic neuronal population if this
        is specified in stimulus_params.py.

        """
        if self.stim_dict['thalamic_input']:
            if nest.Rank() == 0:
                print('Thalamic input provided')
            self.thalamic_population = nest.Create(
                'parrot_neuron', self.stim_dict['n_thal']
                )
            self.thalamic_weight = get_weight(
                self.stim_dict['PSP_th'], self.net_dict
                )
            self.stop_th = (
                self.stim_dict['th_start'] + self.stim_dict['th_duration']
                )
            self.poisson_th = nest.Create('poisson_generator')
            nest.SetStatus(
                self.poisson_th, {
                    'rate': self.stim_dict['th_rate'],
                    'start': self.stim_dict['th_start'],
                    'stop': self.stop_th
                    }
                )
            nest.Connect(self.poisson_th, self.thalamic_population)
            self.nr_synapses_th = synapses_th_matrix(
                self.net_dict, self.stim_dict
                )
            if self.K_scaling != 1:
                self.thalamic_weight = self.thalamic_weight / (
                    self.K_scaling ** 0.5)
                self.nr_synapses_th = (self.nr_synapses_th * self.K_scaling)
        else:
            if nest.Rank() == 0:
                print('Thalamic input not provided')
Exemple #4
0
    def connect_stimulus_vector(self):
        """ This function connects the stimulus vector
        as specified in stimulus_params.py.

        """
        # indices where stimulus vector is not zero
        nonzero_stim = np.nonzero(self.stim_dict['PSP_stim'])
        PSPs = self.stim_dict['PSP_stim'][nonzero_stim]
        stimulus_weight = get_weight(PSPs, self.net_dict)
        all_neurons = self.pops[0] + self.pops[1] + self.pops[2]
        neurons = [all_neurons[nzs] for nzs in nonzero_stim[0]]
        conn_dict = {'rule': 'one_to_one'}
        syn_dict = {'weight': stimulus_weight}

        rate_vec = np.zeros(np.sum(self.net_dict['N_full']))
        if self.stim_dict['stimulus_type'] != 'fixed':
            rate_mean = self.stim_dict['stim_rate']
            stim_vec = []
            for i, stim in enumerate(self.stim_dict['PSP_stim']):
                if stim != 0:
                    rate = np.random.exponential(rate_mean)
                    rate_vec[i] = rate
                    poisson_stim = nest.Create('poisson_generator',
                                               params={'rate': rate})
                    stim_vec.append(poisson_stim[0])
            # save stimulus vector
            h5.add_to_h5(self.sim_dict['data_path'] + '/results.h5',
                         {'stim_rate_vec': rate_vec},
                         'a',
                         overwrite_dataset=True)
        else:
            poisson_stim = nest.Create(
                'poisson_generator',
                params={'rate': self.stim_dict['stim_rate']})
            stim_vec = [poisson_stim[0] for i in range(len(neurons))]
        nest.Connect(stim_vec, neurons, conn_dict, syn_dict)
Exemple #5
0
    def create_populations(self):
        """ Creates the neuronal populations.

        The neuronal populations are created and the parameters are assigned
        to them. The initial membrane potential of the neurons is drawn from a
        normal distribution. Scaling of the number of neurons and of the
        synapses is performed. If scaling is performed extra DC input is added
        to the neuronal populations.

        """
        self.N_full = self.net_dict['N_full']
        self.N_scaling = self.net_dict['N_scaling']
        self.K_scaling = self.net_dict['K_scaling']
        self.synapses = get_total_number_of_synapses(self.net_dict)
        self.synapses_scaled = self.synapses * self.K_scaling
        self.nr_neurons = self.N_full * self.N_scaling
        self.K_ext = self.net_dict['K_ext'] * self.K_scaling
        self.w_from_PSP = get_weight(self.net_dict['PSP_e'], self.net_dict)
        self.weight_mat = get_weight(
            self.net_dict['PSP_mean_matrix'], self.net_dict
            )
        self.weight_mat_std = self.net_dict['PSP_std_matrix']
        self.w_ext = self.w_from_PSP
        if self.net_dict['poisson_input']:
            self.DC_amp_e = np.zeros(len(self.net_dict['populations']))
        else:
            if nest.Rank() == 0:
                print(
                    '''
                    no poisson input provided
                    calculating dc input to compensate
                    '''
                    )
            self.DC_amp_e = compute_DC(self.net_dict, self.w_ext)

        if nest.Rank() == 0:
            print(
                'The number of neurons is scaled by a factor of: %.2f'
                % self.N_scaling
                )
            print(
                'The number of synapses is scaled by a factor of: %.2f'
                % self.K_scaling
                )

        # Scaling of the synapses.
        if self.K_scaling != 1:
            synapses_indegree = self.synapses / (
                self.N_full.reshape(len(self.N_full), 1) * self.N_scaling)
            self.weight_mat, self.w_ext, self.DC_amp_e = adj_w_ext_to_K(
                synapses_indegree, self.K_scaling, self.weight_mat,
                self.w_from_PSP, self.DC_amp_e, self.net_dict, self.stim_dict
                )

        # Create cortical populations.
        self.pops = []
        pop_file = open(
            os.path.join(self.data_path, 'population_GIDs.dat'), 'w+'
            )
        for i, pop in enumerate(self.net_dict['populations']):
            population = nest.Create(
                self.net_dict['neuron_model'], int(self.nr_neurons[i])
                )
            nest.SetStatus(
                population, {
                    'tau_syn_ex': self.net_dict['neuron_params']['tau_syn_ex'],
                    'tau_syn_in': self.net_dict['neuron_params']['tau_syn_in'],
                    'E_L': self.net_dict['neuron_params']['E_L'],
                    'V_th': self.net_dict['neuron_params']['V_th'],
                    'V_reset':  self.net_dict['neuron_params']['V_reset'],
                    't_ref': self.net_dict['neuron_params']['t_ref'],
                    'I_e': self.DC_amp_e[i]
                    }
                )
            self.pops.append(population)
            pop_file.write('%d  %d \n' % (population[0], population[-1]))
        pop_file.close()
        for thread in np.arange(nest.GetKernelStatus('local_num_threads')):
            # Using GetNodes is a work-around until NEST 3.0 is released. It
            # will issue a deprecation warning.
            local_nodes = nest.GetNodes(
                [0], {
                    'model': self.net_dict['neuron_model'],
                    'thread': thread
                    }, local_only=True
                )[0]
            vp = nest.GetStatus(local_nodes)[0]['vp']
            # vp is the same for all local nodes on the same thread
            nest.SetStatus(
                local_nodes, 'V_m', self.pyrngs[vp].normal(
                    self.net_dict['neuron_params']['V0_mean'],
                    self.net_dict['neuron_params']['V0_sd'],
                    len(local_nodes))
                    )
    def create_populations(self):
        """ Creates the neuronal populations.

        The neuronal populations are created and the parameters are assigned
        to them. The initial membrane potential of the neurons is drawn from a
        normal distribution. Scaling of the number of neurons and of the
        synapses is performed. If scaling is performed extra DC input is added
        to the neuronal populations.

        """
        self.N_full = self.net_dict['N_full']
        self.N_scaling = self.net_dict['N_scaling']
        self.K_scaling = self.net_dict['K_scaling']
        self.synapses = get_total_number_of_synapses(self.net_dict)
        self.synapses_scaled = self.synapses * self.K_scaling
        self.nr_neurons = self.N_full * self.N_scaling
        self.K_ext = self.net_dict['K_ext'] * self.K_scaling
        self.w_from_PSP = get_weight(self.net_dict['PSP_e'], self.net_dict)
        self.weight_mat = get_weight(self.net_dict['PSP_mean_matrix'],
                                     self.net_dict)
        self.weight_mat_std = self.net_dict['PSP_std_matrix']
        self.w_ext = self.w_from_PSP
        if self.net_dict['poisson_input']:
            self.DC_amp_e = np.zeros(len(self.net_dict['populations']))
        else:
            if nest.Rank() == 0:
                print("""
                    no poisson input provided
                    calculating dc input to compensate
                    """)
            self.DC_amp_e = compute_DC(self.net_dict, self.w_ext)

        v0_type_options = ['original', 'optimized']
        if self.net_dict['V0_type'] not in v0_type_options:
            print('''
                '{0}' is not a valid option, replacing it with '{1}'
                Valid options are {2}
                '''.format(self.net_dict['V0_type'], v0_type_options[0],
                           v0_type_options))
            self.net_dict['V0_type'] = v0_type_options[0]
        if nest.Rank() == 0:
            print('The number of neurons is scaled by a factor of: %.2f' %
                  self.N_scaling)
            print('The number of synapses is scaled by a factor of: %.2f' %
                  self.K_scaling)

        # Scaling of the synapses.
        if self.K_scaling != 1:
            synapses_indegree = self.synapses / (
                self.N_full.reshape(len(self.N_full), 1) * self.N_scaling)
            self.weight_mat, self.w_ext, self.DC_amp_e = adj_w_ext_to_K(
                synapses_indegree, self.K_scaling, self.weight_mat,
                self.w_from_PSP, self.DC_amp_e, self.net_dict, self.stim_dict)

        # Create cortical populations.
        self.pops = []
        pop_file = open(os.path.join(self.data_path, 'population_nodeids.dat'),
                        'w+')
        for i, pop in enumerate(self.net_dict['populations']):
            population = nest.Create(self.net_dict['neuron_model'],
                                     int(self.nr_neurons[i]))
            population.set({
                'tau_syn_ex':
                self.net_dict['neuron_params']['tau_syn_ex'],
                'tau_syn_in':
                self.net_dict['neuron_params']['tau_syn_in'],
                'E_L':
                self.net_dict['neuron_params']['E_L'],
                'V_th':
                self.net_dict['neuron_params']['V_th'],
                'V_reset':
                self.net_dict['neuron_params']['V_reset'],
                't_ref':
                self.net_dict['neuron_params']['t_ref'],
                'I_e':
                self.DC_amp_e[i]
            })
            if self.net_dict['V0_type'] == 'optimized':
                population.set(
                    V_m=nest.random.normal(mean=self.net_dict['neuron_params']
                                           ['V0_mean']['optimized'][i],
                                           std=self.net_dict['neuron_params']
                                           ['V0_sd']['optimized'][i]))
            elif self.net_dict['V0_type'] == 'original':
                population.set(V_m=nest.random.normal(
                    mean=self.net_dict['neuron_params']['V0_mean']['original'],
                    std=self.net_dict['neuron_params']['V0_sd']['original']))
            self.pops.append(population)
            pop_file.write('%d  %d \n' % (population[0].get('global_id'),
                                          population[-1].get('global_id')))
        pop_file.close()
Exemple #7
0
train_leaky = df_train.loc[:, ['q1_q2_intersect', 'q1_freq', 'q2_freq']]

stops = set(stopwords.words("english"))

df_train['question1'] = df_train['question1'].map(
    lambda x: str(x).lower().split())
df_train['question2'] = df_train['question2'].map(
    lambda x: str(x).lower().split())

train_qs = pd.Series(df_train['question1'].tolist() +
                     df_train['question2'].tolist())

words = [x for y in train_qs for x in y]
counts = Counter(words)
weights = {word: get_weight(count) for word, count in counts.items()}

print('\nLoading :: training leaky features...')
df_train_ = pd.read_csv('./data/train_features.csv', encoding="ISO-8859-1")
X_train_ab = df_train_.iloc[:, 2:-1]
X_train_ab = X_train_ab.drop('euclidean_distance', axis=1)
X_train_ab = X_train_ab.drop('jaccard_distance', axis=1)

print('Building :: other training features...')
X_train = build_features(df_train, stops, weights)
X_train = pd.concat((X_train, X_train_ab, train_leaky), axis=1)
y_train = df_train['is_duplicate'].values

try:
    print('Saving :: final train features...')
    X_train.to_csv('./final_train_features.csv')
    def create_populations(self):
        """ Creates the neuronal populations.

        The neuronal populations are created and the parameters are assigned
        to them. The initial membrane potential of the neurons is drawn from a
        normal distribution. Scaling of the number of neurons and of the
        synapses is performed. If scaling is performed extra DC input is added
        to the neuronal populations.

        """

        # -------------------------------------------
        '''Find model parameters'''
        # -------------------------------------------

        # Full model parameters
        self.N_full = self.net_dict['N_full']  # Full number of neurons
        self.synapses = get_total_number_of_synapses(
            self.net_dict)  # Full number of synapses

        # Scaling parameters
        self.N_scaling = self.net_dict['N_scaling']  # Neuron number scaling
        self.K_scaling = self.net_dict['K_scaling']  # Synapse number scaling

        # Scaled parameters
        self.nr_neurons = self.N_full * self.N_scaling  # Scaled number of neurons
        self.synapses_scaled = self.synapses * self.K_scaling  # Scaled (internal) synapses
        self.K_ext = self.net_dict[
            'K_ext'] * self.K_scaling  # Scaled (external) synapses

        # Calculated weights (to achieve a given change in the membrane potential)
        self.w_from_PSP = get_weight(self.net_dict['PSP_e'], self.net_dict)
        self.weight_mat = get_weight(self.net_dict['PSP_mean_matrix'],
                                     self.net_dict)
        self.weight_mat_std = self.net_dict['PSP_std_matrix']
        self.w_ext = self.w_from_PSP

        # Network simulated with Poisson input, or DC current?
        if self.net_dict['poisson_input']:
            self.DC_amp_e = np.zeros(len(self.net_dict['populations']))
        else:
            if nest.Rank() == 0:
                print("""
                    no poisson input provided
                    calculating dc input to compensate
                    """)
            self.DC_amp_e = compute_DC(self.net_dict, self.w_ext)

        # Scaling of the synapses.
        if nest.Rank() == 0:
            print('The number of neurons is scaled by a factor of: %.2f' %
                  self.N_scaling)
            print('The number of synapses is scaled by a factor of: %.2f' %
                  self.K_scaling)
        if self.K_scaling != 1:
            synapses_indegree = self.synapses / (
                self.N_full.reshape(len(self.N_full), 1) * self.N_scaling)
            self.weight_mat, self.w_ext, self.DC_amp_e = adj_w_ext_to_K(
                synapses_indegree, self.K_scaling, self.weight_mat,
                self.w_from_PSP, self.DC_amp_e, self.net_dict, self.stim_dict)

        # -------------------------------------------
        '''Create cortical population'''
        # -------------------------------------------

        # Initialise data stores
        self.pops = []
        pop_file = open(os.path.join(self.data_path, 'population_GIDs.dat'),
                        'w+')

        # Loop over populations
        for i, pop in enumerate(self.net_dict['populations']):

            # Create a population of a given size
            population = nest.Create(self.net_dict['neuron_model'],
                                     int(self.nr_neurons[i]))

            # Set the parameters for this population
            '''Note that no tau_m is stated here (meaning that all neurons have a default
            of 10ms. It is interesting to note that, in the paper by Mejias et al. (2016),
            the following tau_m values were used:
                For superficial neurons: τE = 6 ms, τI = 15 ms
                For infragranular neurons: τE = 30 ms, τI = 75 ms
            '''

            nest.SetStatus(
                population,
                {
                    'tau_syn_ex': self.net_dict['neuron_params']['tau_syn_ex'],
                    'tau_syn_in': self.net_dict['neuron_params']['tau_syn_in'],
                    'E_L': self.net_dict['neuron_params']['E_L'],
                    'V_th': self.net_dict['neuron_params']['V_th'],
                    'V_reset': self.net_dict['neuron_params']['V_reset'],
                    't_ref': self.net_dict['neuron_params']['t_ref'],
                    'I_e': self.DC_amp_e[i]  #,
                    # 'tau_m': self.net_dict['time_constants'][i]
                })

            # Save population to file
            self.pops.append(population)
            pop_file.write('%d  %d \n' % (population[0], population[-1]))
        pop_file.close()

        # Something about processing threads
        for thread in np.arange(nest.GetKernelStatus('local_num_threads')):
            # Using GetNodes is a work-around until NEST 3.0 is released. It
            # will issue a deprecation warning.
            local_nodes = nest.GetNodes([0], {
                'model': self.net_dict['neuron_model'],
                'thread': thread
            },
                                        local_only=True)[0]
            vp = nest.GetStatus(local_nodes)[0]['vp']
            # vp is the same for all local nodes on the same thread
            nest.SetStatus(
                local_nodes, 'V_m', self.pyrngs[vp].normal(
                    self.net_dict['neuron_params']['V0_mean'],
                    self.net_dict['neuron_params']['V0_sd'], len(local_nodes)))